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