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