Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "pyarena.h" |
| 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. |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +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 |
Martin v. Löwis | da69041 | 2006-04-13 19:16:13 +0000 | [diff] [blame^] | 15 | #define ALIGNMENT 8 |
| 16 | #define ALIGNMENT_SHIFT 3 |
| 17 | #define ALIGNMENT_MASK (ALIGNMENT - 1) |
| 18 | #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
| 19 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 20 | typedef struct _block { |
Tim Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 21 | /* Total number of bytes owned by this block available to pass out. |
| 22 | * Read-only after initialization. The first such byte starts at |
| 23 | * ab_mem. |
| 24 | */ |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 25 | size_t ab_size; |
Tim Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 26 | |
| 27 | /* Total number of bytes already passed out. The next byte available |
| 28 | * to pass out starts at ab_mem + ab_offset. |
| 29 | */ |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 30 | size_t ab_offset; |
Tim Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 31 | |
| 32 | /* An arena maintains a singly-linked, NULL-terminated list of |
| 33 | * all blocks owned by the arena. These are linked via the |
| 34 | * ab_next member. |
| 35 | */ |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 36 | struct _block *ab_next; |
Tim Peters | f638630 | 2006-03-02 21:41:18 +0000 | [diff] [blame] | 37 | |
| 38 | /* Pointer to the first allocatable byte owned by this block. Read- |
| 39 | * only after initialization. |
| 40 | */ |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 41 | void *ab_mem; |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 42 | } block; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 43 | |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 44 | /* The arena manages two kinds of memory, blocks of raw memory |
| 45 | and a list of PyObject* pointers. PyObjects are decrefed |
| 46 | when the arena is freed. |
| 47 | */ |
Tim Peters | 8cfaa0e | 2006-03-02 20:37:32 +0000 | [diff] [blame] | 48 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 49 | struct _arena { |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +0000 | [diff] [blame] | 50 | /* Pointer to the first block allocated for the arena, never NULL. |
| 51 | It is used only to find the first block when the arena is |
| 52 | being freed. |
| 53 | */ |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 54 | block *a_head; |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +0000 | [diff] [blame] | 55 | |
| 56 | /* Pointer to the block currently used for allocation. It's |
| 57 | ab_next field should be NULL. If it is not-null after a |
| 58 | call to block_alloc(), it means a new block has been allocated |
| 59 | and a_cur should be reset to point it. |
| 60 | */ |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 61 | block *a_cur; |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +0000 | [diff] [blame] | 62 | |
| 63 | /* A Python list object containing references to all the PyObject |
| 64 | pointers associated with this area. They will be DECREFed |
| 65 | when the arena is freed. |
| 66 | */ |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 67 | PyObject *a_objects; |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +0000 | [diff] [blame] | 68 | |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 69 | #if defined(Py_DEBUG) |
| 70 | /* Debug output */ |
| 71 | size_t total_allocs; |
| 72 | size_t total_size; |
| 73 | size_t total_blocks; |
| 74 | size_t total_block_size; |
| 75 | size_t total_big_blocks; |
| 76 | #endif |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 79 | static block * |
| 80 | block_new(size_t size) |
| 81 | { |
Tim Peters | 8cfaa0e | 2006-03-02 20:37:32 +0000 | [diff] [blame] | 82 | /* Allocate header and block as one unit. |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 83 | ab_mem points just past header. */ |
| 84 | block *b = (block *)malloc(sizeof(block) + size); |
| 85 | if (!b) |
| 86 | return NULL; |
| 87 | b->ab_size = size; |
| 88 | b->ab_mem = (void *)(b + 1); |
| 89 | b->ab_next = NULL; |
Martin v. Löwis | da69041 | 2006-04-13 19:16:13 +0000 | [diff] [blame^] | 90 | b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) - |
| 91 | (Py_uintptr_t)(b->ab_mem); |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 92 | return b; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 96 | block_free(block *b) { |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 97 | while (b) { |
| 98 | block *next = b->ab_next; |
| 99 | free(b); |
| 100 | b = next; |
| 101 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 104 | static void * |
| 105 | block_alloc(block *b, size_t size) |
| 106 | { |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 107 | void *p; |
| 108 | assert(b); |
Martin v. Löwis | da69041 | 2006-04-13 19:16:13 +0000 | [diff] [blame^] | 109 | size = ROUNDUP(size); |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 110 | if (b->ab_offset + size > b->ab_size) { |
| 111 | /* If we need to allocate more memory than will fit in |
| 112 | the default block, allocate a one-off block that is |
| 113 | exactly the right size. */ |
| 114 | /* TODO(jhylton): Think about space waste at end of block */ |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 115 | block *newbl = block_new( |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 116 | size < DEFAULT_BLOCK_SIZE ? |
| 117 | DEFAULT_BLOCK_SIZE : size); |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 118 | if (!newbl) |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 119 | return NULL; |
| 120 | assert(!b->ab_next); |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 121 | b->ab_next = newbl; |
| 122 | b = newbl; |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 123 | } |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 124 | |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 125 | assert(b->ab_offset + size <= b->ab_size); |
| 126 | p = (void *)(((char *)b->ab_mem) + b->ab_offset); |
| 127 | b->ab_offset += size; |
| 128 | return p; |
Jeremy Hylton | 77f1bb2 | 2006-02-28 17:53:04 +0000 | [diff] [blame] | 129 | } |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 130 | |
| 131 | PyArena * |
| 132 | PyArena_New() |
| 133 | { |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 134 | PyArena* arena = (PyArena *)malloc(sizeof(PyArena)); |
| 135 | if (!arena) |
| 136 | return NULL; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 137 | |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 138 | arena->a_head = block_new(DEFAULT_BLOCK_SIZE); |
| 139 | arena->a_cur = arena->a_head; |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 140 | if (!arena->a_head) { |
| 141 | free((void *)arena); |
| 142 | return NULL; |
| 143 | } |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 144 | arena->a_objects = PyList_New(0); |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 145 | if (!arena->a_objects) { |
| 146 | block_free(arena->a_head); |
| 147 | free((void *)arena); |
| 148 | return NULL; |
| 149 | } |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 150 | #if defined(Py_DEBUG) |
| 151 | arena->total_allocs = 0; |
| 152 | arena->total_size = 0; |
| 153 | arena->total_blocks = 1; |
| 154 | arena->total_block_size = DEFAULT_BLOCK_SIZE; |
| 155 | arena->total_big_blocks = 0; |
| 156 | #endif |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 157 | return arena; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
| 161 | PyArena_Free(PyArena *arena) |
| 162 | { |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +0000 | [diff] [blame] | 163 | int r; |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 164 | assert(arena); |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 165 | #if defined(Py_DEBUG) |
| 166 | /* |
Tim Peters | 8cfaa0e | 2006-03-02 20:37:32 +0000 | [diff] [blame] | 167 | fprintf(stderr, |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 168 | "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n", |
| 169 | arena->total_allocs, arena->total_size, arena->total_blocks, |
| 170 | arena->total_block_size, arena->total_big_blocks, |
| 171 | PyList_Size(arena->a_objects)); |
| 172 | */ |
| 173 | #endif |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 174 | block_free(arena->a_head); |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 175 | assert(arena->a_objects->ob_refcnt == 1); |
Jeremy Hylton | 296aef8 | 2006-03-31 16:41:22 +0000 | [diff] [blame] | 176 | |
| 177 | /* Clear all the elements from the list. This is necessary |
| 178 | to guarantee that they will be DECREFed. */ |
| 179 | r = PyList_SetSlice(arena->a_objects, |
| 180 | 0, PyList_GET_SIZE(arena->a_objects), NULL); |
| 181 | assert(r == 0); |
| 182 | assert(PyList_GET_SIZE(arena->a_objects) == 0); |
Jeremy Hylton | 99b4ee6 | 2006-02-28 18:52:28 +0000 | [diff] [blame] | 183 | Py_DECREF(arena->a_objects); |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 184 | free(arena); |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void * |
Tim Peters | 8cfaa0e | 2006-03-02 20:37:32 +0000 | [diff] [blame] | 188 | PyArena_Malloc(PyArena *arena, size_t size) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 189 | { |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 190 | void *p = block_alloc(arena->a_cur, size); |
| 191 | if (!p) |
| 192 | return NULL; |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 193 | #if defined(Py_DEBUG) |
| 194 | arena->total_allocs++; |
| 195 | arena->total_size += size; |
| 196 | #endif |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 197 | /* Reset cur if we allocated a new block. */ |
| 198 | if (arena->a_cur->ab_next) { |
| 199 | arena->a_cur = arena->a_cur->ab_next; |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 200 | #if defined(Py_DEBUG) |
| 201 | arena->total_blocks++; |
| 202 | arena->total_block_size += arena->a_cur->ab_size; |
| 203 | if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE) |
Tim Peters | 6fd92dc | 2006-03-02 21:14:45 +0000 | [diff] [blame] | 204 | ++arena->total_big_blocks; |
Jeremy Hylton | 56820c2 | 2006-02-28 19:57:06 +0000 | [diff] [blame] | 205 | #endif |
Jeremy Hylton | 08533fd | 2006-02-28 18:29:00 +0000 | [diff] [blame] | 206 | } |
| 207 | return p; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | int |
Tim Peters | 8cfaa0e | 2006-03-02 20:37:32 +0000 | [diff] [blame] | 211 | PyArena_AddPyObject(PyArena *arena, PyObject *obj) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 212 | { |
Jeremy Hylton | 224003b | 2006-03-01 15:02:24 +0000 | [diff] [blame] | 213 | int r = PyList_Append(arena->a_objects, obj); |
| 214 | if (r >= 0) { |
| 215 | Py_DECREF(obj); |
| 216 | } |
| 217 | return r; |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 218 | } |