pycore_pyarena.h 2.7 KB

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