blob: ead03370d153c35fa464959a36d7961fc64b18a6 [file] [log] [blame]
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001#include "Python.h"
Victor Stinner8370e072021-03-24 02:23:01 +01002#include "pycore_pyarena.h" // PyArena
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003
Tim Petersf6386302006-03-02 21:41:18 +00004/* A simple arena block structure.
Jeremy Hylton56820c22006-02-28 19:57:06 +00005
6 Measurements with standard library modules suggest the average
7 allocation is about 20 bytes and that most compiles use a single
8 block.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009
10 TODO(jhylton): Think about a realloc API, maybe just for the last
11 allocation?
Jeremy Hylton56820c22006-02-28 19:57:06 +000012*/
Neal Norwitzadb69fc2005-12-17 20:54:49 +000013
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000014#define DEFAULT_BLOCK_SIZE 8192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015#define ALIGNMENT 8
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000016
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000017typedef struct _block {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 /* Total number of bytes owned by this block available to pass out.
19 * Read-only after initialization. The first such byte starts at
20 * ab_mem.
21 */
22 size_t ab_size;
Tim Petersf6386302006-03-02 21:41:18 +000023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 /* Total number of bytes already passed out. The next byte available
25 * to pass out starts at ab_mem + ab_offset.
26 */
27 size_t ab_offset;
Tim Petersf6386302006-03-02 21:41:18 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 /* An arena maintains a singly-linked, NULL-terminated list of
30 * all blocks owned by the arena. These are linked via the
31 * ab_next member.
32 */
33 struct _block *ab_next;
Tim Petersf6386302006-03-02 21:41:18 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 /* Pointer to the first allocatable byte owned by this block. Read-
36 * only after initialization.
37 */
38 void *ab_mem;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000039} block;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000040
Jeremy Hylton99b4ee62006-02-28 18:52:28 +000041/* The arena manages two kinds of memory, blocks of raw memory
42 and a list of PyObject* pointers. PyObjects are decrefed
43 when the arena is freed.
44*/
Tim Peters8cfaa0e2006-03-02 20:37:32 +000045
Neal Norwitzadb69fc2005-12-17 20:54:49 +000046struct _arena {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 /* Pointer to the first block allocated for the arena, never NULL.
48 It is used only to find the first block when the arena is
49 being freed.
50 */
51 block *a_head;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
Quan Tian3bd0d622018-10-20 05:30:03 +080053 /* Pointer to the block currently used for allocation. Its
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 ab_next field should be NULL. If it is not-null after a
55 call to block_alloc(), it means a new block has been allocated
56 and a_cur should be reset to point it.
57 */
58 block *a_cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 /* A Python list object containing references to all the PyObject
Quan Tian3bd0d622018-10-20 05:30:03 +080061 pointers associated with this arena. They will be DECREFed
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 when the arena is freed.
63 */
64 PyObject *a_objects;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065
Jeremy Hylton56820c22006-02-28 19:57:06 +000066#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 /* Debug output */
68 size_t total_allocs;
69 size_t total_size;
70 size_t total_blocks;
71 size_t total_block_size;
72 size_t total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +000073#endif
Neal Norwitzadb69fc2005-12-17 20:54:49 +000074};
75
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000076static block *
77block_new(size_t size)
78{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 /* Allocate header and block as one unit.
80 ab_mem points just past header. */
Victor Stinnerc6632e72013-07-07 17:18:53 +020081 block *b = (block *)PyMem_Malloc(sizeof(block) + size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (!b)
83 return NULL;
84 b->ab_size = size;
85 b->ab_mem = (void *)(b + 1);
86 b->ab_next = NULL;
Antoine Pitrouca8aa4a2012-09-20 20:56:47 +020087 b->ab_offset = (char *)_Py_ALIGN_UP(b->ab_mem, ALIGNMENT) -
88 (char *)(b->ab_mem);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 return b;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000090}
91
92static void
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000093block_free(block *b) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 while (b) {
95 block *next = b->ab_next;
Victor Stinnerc6632e72013-07-07 17:18:53 +020096 PyMem_Free(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 b = next;
98 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +000099}
100
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000101static void *
102block_alloc(block *b, size_t size)
103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 void *p;
105 assert(b);
Antoine Pitrouca8aa4a2012-09-20 20:56:47 +0200106 size = _Py_SIZE_ROUND_UP(size, ALIGNMENT);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (b->ab_offset + size > b->ab_size) {
108 /* If we need to allocate more memory than will fit in
109 the default block, allocate a one-off block that is
110 exactly the right size. */
111 /* TODO(jhylton): Think about space waste at end of block */
112 block *newbl = block_new(
113 size < DEFAULT_BLOCK_SIZE ?
114 DEFAULT_BLOCK_SIZE : size);
115 if (!newbl)
116 return NULL;
117 assert(!b->ab_next);
118 b->ab_next = newbl;
119 b = newbl;
120 }
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 assert(b->ab_offset + size <= b->ab_size);
123 p = (void *)(((char *)b->ab_mem) + b->ab_offset);
124 b->ab_offset += size;
125 return p;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000126}
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000127
128PyArena *
Victor Stinner8370e072021-03-24 02:23:01 +0100129_PyArena_New(void)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000130{
Victor Stinnerc6632e72013-07-07 17:18:53 +0200131 PyArena* arena = (PyArena *)PyMem_Malloc(sizeof(PyArena));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 if (!arena)
133 return (PyArena*)PyErr_NoMemory();
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
136 arena->a_cur = arena->a_head;
137 if (!arena->a_head) {
Victor Stinnerc6632e72013-07-07 17:18:53 +0200138 PyMem_Free((void *)arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return (PyArena*)PyErr_NoMemory();
140 }
141 arena->a_objects = PyList_New(0);
142 if (!arena->a_objects) {
143 block_free(arena->a_head);
Victor Stinnerc6632e72013-07-07 17:18:53 +0200144 PyMem_Free((void *)arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return (PyArena*)PyErr_NoMemory();
146 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000147#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 arena->total_allocs = 0;
149 arena->total_size = 0;
150 arena->total_blocks = 1;
151 arena->total_block_size = DEFAULT_BLOCK_SIZE;
152 arena->total_big_blocks = 0;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000153#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return arena;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000155}
156
157void
Victor Stinner8370e072021-03-24 02:23:01 +0100158_PyArena_Free(PyArena *arena)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 assert(arena);
Jeremy Hylton56820c22006-02-28 19:57:06 +0000161#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 /*
163 fprintf(stderr,
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +0200164 "alloc=%zu size=%zu blocks=%zu block_size=%zu big=%zu objects=%zu\n",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 arena->total_allocs, arena->total_size, arena->total_blocks,
166 arena->total_block_size, arena->total_big_blocks,
167 PyList_Size(arena->a_objects));
168 */
Jeremy Hylton56820c22006-02-28 19:57:06 +0000169#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 block_free(arena->a_head);
171 /* This property normally holds, except when the code being compiled
172 is sys.getobjects(0), in which case there will be two references.
173 assert(arena->a_objects->ob_refcnt == 1);
174 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 Py_DECREF(arena->a_objects);
Victor Stinnerc6632e72013-07-07 17:18:53 +0200177 PyMem_Free(arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178}
179
180void *
Victor Stinner8370e072021-03-24 02:23:01 +0100181_PyArena_Malloc(PyArena *arena, size_t size)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 void *p = block_alloc(arena->a_cur, size);
184 if (!p)
185 return PyErr_NoMemory();
Jeremy Hylton56820c22006-02-28 19:57:06 +0000186#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 arena->total_allocs++;
188 arena->total_size += size;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000189#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 /* Reset cur if we allocated a new block. */
191 if (arena->a_cur->ab_next) {
192 arena->a_cur = arena->a_cur->ab_next;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000193#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 arena->total_blocks++;
195 arena->total_block_size += arena->a_cur->ab_size;
196 if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
197 ++arena->total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 }
200 return p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000201}
202
203int
Victor Stinner8370e072021-03-24 02:23:01 +0100204_PyArena_AddPyObject(PyArena *arena, PyObject *obj)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 int r = PyList_Append(arena->a_objects, obj);
207 if (r >= 0) {
208 Py_DECREF(obj);
209 }
210 return r;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000211}