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