|
| 1 | +/* An arena-like memory interface for the compiler. |
| 2 | + */ |
| 3 | + |
| 4 | +#ifndef Py_INTERNAL_PYARENA_H |
| 5 | +#define Py_INTERNAL_PYARENA_H |
| 6 | +#ifdef __cplusplus |
| 7 | +extern "C" { |
| 8 | +#endif |
| 9 | + |
| 10 | +#ifndef Py_BUILD_CORE |
| 11 | +# error "this header requires Py_BUILD_CORE define" |
| 12 | +#endif |
| 13 | + |
| 14 | +typedef struct _arena PyArena; |
| 15 | + |
| 16 | +/* _PyArena_New() and _PyArena_Free() create a new arena and free it, |
| 17 | + respectively. Once an arena has been created, it can be used |
| 18 | + to allocate memory via _PyArena_Malloc(). Pointers to PyObject can |
| 19 | + also be registered with the arena via _PyArena_AddPyObject(), and the |
| 20 | + arena will ensure that the PyObjects stay alive at least until |
| 21 | + _PyArena_Free() is called. When an arena is freed, all the memory it |
| 22 | + allocated is freed, the arena releases internal references to registered |
| 23 | + PyObject*, and none of its pointers are valid. |
| 24 | + XXX (tim) What does "none of its pointers are valid" mean? Does it |
| 25 | + XXX mean that pointers previously obtained via _PyArena_Malloc() are |
| 26 | + XXX no longer valid? (That's clearly true, but not sure that's what |
| 27 | + XXX the text is trying to say.) |
| 28 | +
|
| 29 | + _PyArena_New() returns an arena pointer. On error, it |
| 30 | + returns a negative number and sets an exception. |
| 31 | + XXX (tim): Not true. On error, _PyArena_New() actually returns NULL, |
| 32 | + XXX and looks like it may or may not set an exception (e.g., if the |
| 33 | + XXX internal PyList_New(0) returns NULL, _PyArena_New() passes that on |
| 34 | + XXX and an exception is set; OTOH, if the internal |
| 35 | + XXX block_new(DEFAULT_BLOCK_SIZE) returns NULL, that's passed on but |
| 36 | + XXX an exception is not set in that case). |
| 37 | +*/ |
| 38 | +PyAPI_FUNC(PyArena*) _PyArena_New(void); |
| 39 | +PyAPI_FUNC(void) _PyArena_Free(PyArena *); |
| 40 | + |
| 41 | +/* Mostly like malloc(), return the address of a block of memory spanning |
| 42 | + * `size` bytes, or return NULL (without setting an exception) if enough |
| 43 | + * new memory can't be obtained. Unlike malloc(0), _PyArena_Malloc() with |
| 44 | + * size=0 does not guarantee to return a unique pointer (the pointer |
| 45 | + * returned may equal one or more other pointers obtained from |
| 46 | + * _PyArena_Malloc()). |
| 47 | + * Note that pointers obtained via _PyArena_Malloc() must never be passed to |
| 48 | + * the system free() or realloc(), or to any of Python's similar memory- |
| 49 | + * management functions. _PyArena_Malloc()-obtained pointers remain valid |
| 50 | + * until _PyArena_Free(ar) is called, at which point all pointers obtained |
| 51 | + * from the arena `ar` become invalid simultaneously. |
| 52 | + */ |
| 53 | +PyAPI_FUNC(void*) _PyArena_Malloc(PyArena *, size_t size); |
| 54 | + |
| 55 | +/* This routine isn't a proper arena allocation routine. It takes |
| 56 | + * a PyObject* and records it so that it can be DECREFed when the |
| 57 | + * arena is freed. |
| 58 | + */ |
| 59 | +PyAPI_FUNC(int) _PyArena_AddPyObject(PyArena *, PyObject *); |
| 60 | + |
| 61 | +#ifdef __cplusplus |
| 62 | +} |
| 63 | +#endif |
| 64 | +#endif /* !Py_INTERNAL_PYARENA_H */ |
0 commit comments