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