forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-112529: Implement GC for free-threaded builds
- Loading branch information
Showing
15 changed files
with
1,961 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef Py_INTERNAL_OBJECT_ALLOC_H | ||
#define Py_INTERNAL_OBJECT_ALLOC_H | ||
|
||
#include "pycore_object.h" // _PyType_HasFeature() | ||
#include "pycore_pystate.h" // _PyThreadState_GET() | ||
#include "pycore_tstate.h" // _PyThreadStateImpl | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
// Sets the heap used for PyObject_Malloc(), PyObject_Realloc(), etc. calls in | ||
// Py_GIL_DISABLED builds. We use different heaps depending on if the object | ||
// supports GC and if it has a pre-header. We smuggle the choice of heap | ||
// through the _mimalloc_thread_state. | ||
// This is a no-op outside of Py_GIL_DISABLED builds. | ||
static inline void * | ||
_PyObject_MallocWithType(PyTypeObject *tp, size_t size) | ||
{ | ||
#ifdef Py_GIL_DISABLED | ||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
struct _mimalloc_thread_state *m = &tstate->mimalloc; | ||
if (_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER)) { | ||
m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_GC_PRE]; | ||
} | ||
else if (_PyType_IS_GC(tp)) { | ||
m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_GC]; | ||
} | ||
#endif | ||
void *mem = PyObject_Malloc(size); | ||
#ifdef Py_GIL_DISABLED | ||
m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_OBJECT]; | ||
#endif | ||
return mem; | ||
} | ||
|
||
static inline void * | ||
_PyObject_ReallocWithType(PyTypeObject *tp, void *ptr, size_t size) | ||
{ | ||
#ifdef Py_GIL_DISABLED | ||
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); | ||
struct _mimalloc_thread_state *m = &tstate->mimalloc; | ||
if (_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER)) { | ||
m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_GC_PRE]; | ||
} | ||
else if (_PyType_IS_GC(tp)) { | ||
m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_GC]; | ||
} | ||
#endif | ||
void *mem = PyObject_Realloc(ptr, size); | ||
#ifdef Py_GIL_DISABLED | ||
m->current_object_heap = &m->heaps[_Py_MIMALLOC_HEAP_OBJECT]; | ||
#endif | ||
return mem; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif // !Py_INTERNAL_OBJECT_ALLOC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#ifndef Py_INTERNAL_OBJECT_QUEUE_H | ||
#define Py_INTERNAL_OBJECT_QUEUE_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef Py_BUILD_CORE | ||
# error "this header requires Py_BUILD_CORE define" | ||
#endif | ||
|
||
// _PyObjectQueue is a stack of Python objects implemented as a linked list of | ||
// fixed size buffers. | ||
|
||
// Chosen so that _PyObjectQueueBuffer is a power-of-two size. | ||
#define _Py_OBJECT_QUEUE_BUFFER_SIZE 254 | ||
|
||
typedef struct _PyObjectQueueBuffer { | ||
struct _PyObjectQueueBuffer *next; | ||
Py_ssize_t n; | ||
PyObject *objs[_Py_OBJECT_QUEUE_BUFFER_SIZE]; | ||
} _PyObjectQueueBuffer; | ||
|
||
typedef struct _PyObjectQueue { | ||
_PyObjectQueueBuffer *head; | ||
} _PyObjectQueue; | ||
|
||
|
||
#define _PyObjectQueue_FOR_EACH(q, obj) \ | ||
for (obj = _PyObjectQueue_Pop(q); obj != NULL; obj = _PyObjectQueue_Pop(q)) | ||
|
||
extern _PyObjectQueueBuffer *_PyObjectQueueBuffer_New(void); | ||
extern void _PyObjectQueueBuffer_Free(_PyObjectQueueBuffer *); | ||
|
||
static inline int | ||
_PyObjectQueue_Push(_PyObjectQueue *queue, PyObject *obj) | ||
{ | ||
if (queue->head == NULL || queue->head->n == _Py_OBJECT_QUEUE_BUFFER_SIZE) { | ||
_PyObjectQueueBuffer *next = _PyObjectQueueBuffer_New(); | ||
if (next == NULL) { | ||
return -1; | ||
} | ||
next->next = queue->head; | ||
next->n = 0; | ||
queue->head = next; | ||
} | ||
|
||
_PyObjectQueueBuffer *buf = queue->head; | ||
assert(buf->n >= 0 && buf->n < _Py_OBJECT_QUEUE_BUFFER_SIZE); | ||
buf->objs[buf->n] = obj; | ||
buf->n++; | ||
return 0; | ||
} | ||
|
||
static inline PyObject * | ||
_PyObjectQueue_Pop(_PyObjectQueue *queue) | ||
{ | ||
_PyObjectQueueBuffer *buf = queue->head; | ||
if (buf == NULL) { | ||
return NULL; | ||
} | ||
assert(buf->n > 0 && buf->n <= _Py_OBJECT_QUEUE_BUFFER_SIZE); | ||
buf->n--; | ||
PyObject *obj = buf->objs[buf->n]; | ||
if (buf->n == 0) { | ||
queue->head = buf->next; | ||
_PyObjectQueueBuffer_Free(buf); | ||
} | ||
return obj; | ||
} | ||
|
||
extern void _PyObjectQueue_Clear(_PyObjectQueue *queue); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif // !Py_INTERNAL_OBJECT_QUEUE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.