codekingpro/portable-devtools
115k
1#ifndef Py_INTERNAL_OBJECT_ALLOC_H2#define Py_INTERNAL_OBJECT_ALLOC_H3 4#include "pycore_object.h" // _PyType_HasFeature()5#include "pycore_pystate.h" // _PyThreadState_GET()6#include "pycore_tstate.h" // _PyThreadStateImpl7 8#ifdef __cplusplus9extern "C" {10#endif11 12#ifndef Py_BUILD_CORE13# error "this header requires Py_BUILD_CORE define"14#endif15 16#ifdef Py_GIL_DISABLED17static inline mi_heap_t *18_PyObject_GetAllocationHeap(_PyThreadStateImpl *tstate, PyTypeObject *tp)19{20 struct _mimalloc_thread_state *m = &tstate->mimalloc;21 if (_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER)) {22 return &m->heaps[_Py_MIMALLOC_HEAP_GC_PRE];23 }24 else if (_PyType_IS_GC(tp)) {25 return &m->heaps[_Py_MIMALLOC_HEAP_GC];26 }27 else {28 return &m->heaps[_Py_MIMALLOC_HEAP_OBJECT];29 }30}31#endif32 33// Sets the heap used for PyObject_Malloc(), PyObject_Realloc(), etc. calls in34// Py_GIL_DISABLED builds. We use different heaps depending on if the object35// supports GC and if it has a pre-header. We smuggle the choice of heap36// through the _mimalloc_thread_state. In the default build, this simply37// calls PyObject_Malloc().38static inline void *39_PyObject_MallocWithType(PyTypeObject *tp, size_t size)40{41#ifdef Py_GIL_DISABLED42 _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();43 struct _mimalloc_thread_state *m = &tstate->mimalloc;44 m->current_object_heap = _PyObject_GetAllocationHeap(tstate, tp);45#endif46 void *mem = PyObject_Malloc(size);47#ifdef Py_GIL_DISABLED48 m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_OBJECT];49#endif50 return mem;51}52 53static inline void *54_PyObject_ReallocWithType(PyTypeObject *tp, void *ptr, size_t size)55{56#ifdef Py_GIL_DISABLED57 _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();58 struct _mimalloc_thread_state *m = &tstate->mimalloc;59 m->current_object_heap = _PyObject_GetAllocationHeap(tstate, tp);60#endif61 void *mem = PyObject_Realloc(ptr, size);62#ifdef Py_GIL_DISABLED63 m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_OBJECT];64#endif65 return mem;66}67 68#ifdef __cplusplus69}70#endif71#endif // !Py_INTERNAL_OBJECT_ALLOC_H72 