17db96d56Sopenharmony_ci/* An arena-like memory interface for the compiler.
27db96d56Sopenharmony_ci */
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci#ifndef Py_INTERNAL_PYARENA_H
57db96d56Sopenharmony_ci#define Py_INTERNAL_PYARENA_H
67db96d56Sopenharmony_ci#ifdef __cplusplus
77db96d56Sopenharmony_ciextern "C" {
87db96d56Sopenharmony_ci#endif
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci#ifndef Py_BUILD_CORE
117db96d56Sopenharmony_ci#  error "this header requires Py_BUILD_CORE define"
127db96d56Sopenharmony_ci#endif
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_citypedef struct _arena PyArena;
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ci/* _PyArena_New() and _PyArena_Free() create a new arena and free it,
177db96d56Sopenharmony_ci   respectively.  Once an arena has been created, it can be used
187db96d56Sopenharmony_ci   to allocate memory via _PyArena_Malloc().  Pointers to PyObject can
197db96d56Sopenharmony_ci   also be registered with the arena via _PyArena_AddPyObject(), and the
207db96d56Sopenharmony_ci   arena will ensure that the PyObjects stay alive at least until
217db96d56Sopenharmony_ci   _PyArena_Free() is called.  When an arena is freed, all the memory it
227db96d56Sopenharmony_ci   allocated is freed, the arena releases internal references to registered
237db96d56Sopenharmony_ci   PyObject*, and none of its pointers are valid.
247db96d56Sopenharmony_ci   XXX (tim) What does "none of its pointers are valid" mean?  Does it
257db96d56Sopenharmony_ci   XXX mean that pointers previously obtained via _PyArena_Malloc() are
267db96d56Sopenharmony_ci   XXX no longer valid?  (That's clearly true, but not sure that's what
277db96d56Sopenharmony_ci   XXX the text is trying to say.)
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci   _PyArena_New() returns an arena pointer.  On error, it
307db96d56Sopenharmony_ci   returns a negative number and sets an exception.
317db96d56Sopenharmony_ci   XXX (tim):  Not true.  On error, _PyArena_New() actually returns NULL,
327db96d56Sopenharmony_ci   XXX and looks like it may or may not set an exception (e.g., if the
337db96d56Sopenharmony_ci   XXX internal PyList_New(0) returns NULL, _PyArena_New() passes that on
347db96d56Sopenharmony_ci   XXX and an exception is set; OTOH, if the internal
357db96d56Sopenharmony_ci   XXX block_new(DEFAULT_BLOCK_SIZE) returns NULL, that's passed on but
367db96d56Sopenharmony_ci   XXX an exception is not set in that case).
377db96d56Sopenharmony_ci*/
387db96d56Sopenharmony_ciPyAPI_FUNC(PyArena*) _PyArena_New(void);
397db96d56Sopenharmony_ciPyAPI_FUNC(void) _PyArena_Free(PyArena *);
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci/* Mostly like malloc(), return the address of a block of memory spanning
427db96d56Sopenharmony_ci * `size` bytes, or return NULL (without setting an exception) if enough
437db96d56Sopenharmony_ci * new memory can't be obtained.  Unlike malloc(0), _PyArena_Malloc() with
447db96d56Sopenharmony_ci * size=0 does not guarantee to return a unique pointer (the pointer
457db96d56Sopenharmony_ci * returned may equal one or more other pointers obtained from
467db96d56Sopenharmony_ci * _PyArena_Malloc()).
477db96d56Sopenharmony_ci * Note that pointers obtained via _PyArena_Malloc() must never be passed to
487db96d56Sopenharmony_ci * the system free() or realloc(), or to any of Python's similar memory-
497db96d56Sopenharmony_ci * management functions.  _PyArena_Malloc()-obtained pointers remain valid
507db96d56Sopenharmony_ci * until _PyArena_Free(ar) is called, at which point all pointers obtained
517db96d56Sopenharmony_ci * from the arena `ar` become invalid simultaneously.
527db96d56Sopenharmony_ci */
537db96d56Sopenharmony_ciPyAPI_FUNC(void*) _PyArena_Malloc(PyArena *, size_t size);
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci/* This routine isn't a proper arena allocation routine.  It takes
567db96d56Sopenharmony_ci * a PyObject* and records it so that it can be DECREFed when the
577db96d56Sopenharmony_ci * arena is freed.
587db96d56Sopenharmony_ci */
597db96d56Sopenharmony_ciPyAPI_FUNC(int) _PyArena_AddPyObject(PyArena *, PyObject *);
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci#ifdef __cplusplus
627db96d56Sopenharmony_ci}
637db96d56Sopenharmony_ci#endif
647db96d56Sopenharmony_ci#endif /* !Py_INTERNAL_PYARENA_H */
65