pyarena.h 2.7 KB

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