Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 2 | #include "pycore_pyarena.h" // PyArena |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 3 | |
Tim Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 4 | /* A simple arena block structure. |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 5 | |
| 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9 | |
| 10 | TODO(jhylton): Think about a realloc API, maybe just for the last |
| 11 | allocation? |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 12 | */ |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 14 | #define DEFAULT_BLOCK_SIZE 8192 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | #define ALIGNMENT 8 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 17 | typedef struct _block { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | /* 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 Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 23 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | /* 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 Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 28 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | /* 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 Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | /* Pointer to the first allocatable byte owned by this block. Read- |
| 36 | * only after initialization. |
| 37 | */ |
| 38 | void *ab_mem; |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 39 | } block; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 40 | |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 41 | /* 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 Peters | 8cfaa0e | 2006-03-02 20:37:32 +0000 | [diff] [blame] | 45 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 46 | struct _arena { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | /* 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 53 | /* Pointer to the block currently used for allocation. Its |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | /* A Python list object containing references to all the PyObject |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 61 | pointers associated with this arena. They will be DECREFed |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | when the arena is freed. |
| 63 | */ |
| 64 | PyObject *a_objects; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 66 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | /* 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 Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 73 | #endif |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 76 | static block * |
| 77 | block_new(size_t size) |
| 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | /* Allocate header and block as one unit. |
| 80 | ab_mem points just past header. */ |
Victor Stinner | c6632e7 | 2013-07-07 17:18:53 +0200 | [diff] [blame] | 81 | block *b = (block *)PyMem_Malloc(sizeof(block) + size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | if (!b) |
| 83 | return NULL; |
| 84 | b->ab_size = size; |
| 85 | b->ab_mem = (void *)(b + 1); |
| 86 | b->ab_next = NULL; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 87 | b->ab_offset = (char *)_Py_ALIGN_UP(b->ab_mem, ALIGNMENT) - |
| 88 | (char *)(b->ab_mem); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | return b; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 93 | block_free(block *b) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | while (b) { |
| 95 | block *next = b->ab_next; |
Victor Stinner | c6632e7 | 2013-07-07 17:18:53 +0200 | [diff] [blame] | 96 | PyMem_Free(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | b = next; |
| 98 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 101 | static void * |
| 102 | block_alloc(block *b, size_t size) |
| 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | void *p; |
| 105 | assert(b); |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 106 | size = _Py_SIZE_ROUND_UP(size, ALIGNMENT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | 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 Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | 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 Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 126 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 127 | |
| 128 | PyArena * |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 129 | _PyArena_New(void) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 130 | { |
Victor Stinner | c6632e7 | 2013-07-07 17:18:53 +0200 | [diff] [blame] | 131 | PyArena* arena = (PyArena *)PyMem_Malloc(sizeof(PyArena)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | if (!arena) |
| 133 | return (PyArena*)PyErr_NoMemory(); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | arena->a_head = block_new(DEFAULT_BLOCK_SIZE); |
| 136 | arena->a_cur = arena->a_head; |
| 137 | if (!arena->a_head) { |
Victor Stinner | c6632e7 | 2013-07-07 17:18:53 +0200 | [diff] [blame] | 138 | PyMem_Free((void *)arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | return (PyArena*)PyErr_NoMemory(); |
| 140 | } |
| 141 | arena->a_objects = PyList_New(0); |
| 142 | if (!arena->a_objects) { |
| 143 | block_free(arena->a_head); |
Victor Stinner | c6632e7 | 2013-07-07 17:18:53 +0200 | [diff] [blame] | 144 | PyMem_Free((void *)arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | return (PyArena*)PyErr_NoMemory(); |
| 146 | } |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 147 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | 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 Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 153 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | return arena; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 158 | _PyArena_Free(PyArena *arena) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | assert(arena); |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 161 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | /* |
| 163 | fprintf(stderr, |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 164 | "alloc=%zu size=%zu blocks=%zu block_size=%zu big=%zu objects=%zu\n", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | 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 Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 169 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | Py_DECREF(arena->a_objects); |
Victor Stinner | c6632e7 | 2013-07-07 17:18:53 +0200 | [diff] [blame] | 177 | PyMem_Free(arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void * |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 181 | _PyArena_Malloc(PyArena *arena, size_t size) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | void *p = block_alloc(arena->a_cur, size); |
| 184 | if (!p) |
| 185 | return PyErr_NoMemory(); |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 186 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | arena->total_allocs++; |
| 188 | arena->total_size += size; |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 189 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | /* 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 Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 193 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | 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 Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 198 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | } |
| 200 | return p; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 204 | _PyArena_AddPyObject(PyArena *arena, PyObject *obj) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | int r = PyList_Append(arena->a_objects, obj); |
| 207 | if (r >= 0) { |
| 208 | Py_DECREF(obj); |
| 209 | } |
| 210 | return r; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 211 | } |