Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:48

0001 #ifndef Py_INTERNAL_OBJECT_ALLOC_H
0002 #define Py_INTERNAL_OBJECT_ALLOC_H
0003 
0004 #include "pycore_object.h"      // _PyType_HasFeature()
0005 #include "pycore_pystate.h"     // _PyThreadState_GET()
0006 #include "pycore_tstate.h"      // _PyThreadStateImpl
0007 
0008 #ifdef __cplusplus
0009 extern "C" {
0010 #endif
0011 
0012 #ifndef Py_BUILD_CORE
0013 #  error "this header requires Py_BUILD_CORE define"
0014 #endif
0015 
0016 #ifdef Py_GIL_DISABLED
0017 static inline mi_heap_t *
0018 _PyObject_GetAllocationHeap(_PyThreadStateImpl *tstate, PyTypeObject *tp)
0019 {
0020     struct _mimalloc_thread_state *m = &tstate->mimalloc;
0021     if (_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER)) {
0022         return &m->heaps[_Py_MIMALLOC_HEAP_GC_PRE];
0023     }
0024     else if (_PyType_IS_GC(tp)) {
0025         return &m->heaps[_Py_MIMALLOC_HEAP_GC];
0026     }
0027     else {
0028         return &m->heaps[_Py_MIMALLOC_HEAP_OBJECT];
0029     }
0030 }
0031 #endif
0032 
0033 // Sets the heap used for PyObject_Malloc(), PyObject_Realloc(), etc. calls in
0034 // Py_GIL_DISABLED builds. We use different heaps depending on if the object
0035 // supports GC and if it has a pre-header. We smuggle the choice of heap
0036 // through the _mimalloc_thread_state. In the default build, this simply
0037 // calls PyObject_Malloc().
0038 static inline void *
0039 _PyObject_MallocWithType(PyTypeObject *tp, size_t size)
0040 {
0041 #ifdef Py_GIL_DISABLED
0042     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
0043     struct _mimalloc_thread_state *m = &tstate->mimalloc;
0044     m->current_object_heap = _PyObject_GetAllocationHeap(tstate, tp);
0045 #endif
0046     void *mem = PyObject_Malloc(size);
0047 #ifdef Py_GIL_DISABLED
0048     m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_OBJECT];
0049 #endif
0050     return mem;
0051 }
0052 
0053 static inline void *
0054 _PyObject_ReallocWithType(PyTypeObject *tp, void *ptr, size_t size)
0055 {
0056 #ifdef Py_GIL_DISABLED
0057     _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
0058     struct _mimalloc_thread_state *m = &tstate->mimalloc;
0059     m->current_object_heap = _PyObject_GetAllocationHeap(tstate, tp);
0060 #endif
0061     void *mem = PyObject_Realloc(ptr, size);
0062 #ifdef Py_GIL_DISABLED
0063     m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_OBJECT];
0064 #endif
0065     return mem;
0066 }
0067 
0068 #ifdef __cplusplus
0069 }
0070 #endif
0071 #endif  // !Py_INTERNAL_OBJECT_ALLOC_H