blob: 012c46bcea1021bb76a19af4d24e0bd634daa0c5 [file] [log] [blame]
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001#include "Python.h"
2#include "pyarena.h"
3
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.
Jeremy Hylton296aef82006-03-31 16:41:22 +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
15typedef struct _block {
Tim Petersf6386302006-03-02 21:41:18 +000016 /* 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 Hylton08533fd2006-02-28 18:29:00 +000020 size_t ab_size;
Tim Petersf6386302006-03-02 21:41:18 +000021
22 /* Total number of bytes already passed out. The next byte available
23 * to pass out starts at ab_mem + ab_offset.
24 */
Jeremy Hylton08533fd2006-02-28 18:29:00 +000025 size_t ab_offset;
Tim Petersf6386302006-03-02 21:41:18 +000026
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 Hylton08533fd2006-02-28 18:29:00 +000031 struct _block *ab_next;
Tim Petersf6386302006-03-02 21:41:18 +000032
33 /* Pointer to the first allocatable byte owned by this block. Read-
34 * only after initialization.
35 */
Jeremy Hylton08533fd2006-02-28 18:29:00 +000036 void *ab_mem;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000037} block;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000038
Jeremy Hylton99b4ee62006-02-28 18:52:28 +000039/* 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 Peters8cfaa0e2006-03-02 20:37:32 +000043
Neal Norwitzadb69fc2005-12-17 20:54:49 +000044struct _arena {
Jeremy Hylton296aef82006-03-31 16:41:22 +000045 /* 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 Hylton08533fd2006-02-28 18:29:00 +000049 block *a_head;
Jeremy Hylton296aef82006-03-31 16:41:22 +000050
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 Hylton08533fd2006-02-28 18:29:00 +000056 block *a_cur;
Jeremy Hylton296aef82006-03-31 16:41:22 +000057
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 Hylton99b4ee62006-02-28 18:52:28 +000062 PyObject *a_objects;
Jeremy Hylton296aef82006-03-31 16:41:22 +000063
Jeremy Hylton56820c22006-02-28 19:57:06 +000064#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 Norwitzadb69fc2005-12-17 20:54:49 +000072};
73
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000074static block *
75block_new(size_t size)
76{
Tim Peters8cfaa0e2006-03-02 20:37:32 +000077 /* Allocate header and block as one unit.
Jeremy Hylton08533fd2006-02-28 18:29:00 +000078 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 Norwitzadb69fc2005-12-17 20:54:49 +000087}
88
89static void
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000090block_free(block *b) {
Jeremy Hylton08533fd2006-02-28 18:29:00 +000091 while (b) {
92 block *next = b->ab_next;
93 free(b);
94 b = next;
95 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +000096}
97
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000098static void *
99block_alloc(block *b, size_t size)
100{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000101 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 Hylton77f1bb22006-02-28 17:53:04 +0000117
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000118 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 Hylton77f1bb22006-02-28 17:53:04 +0000122}
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000123
124PyArena *
125PyArena_New()
126{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000127 PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
128 if (!arena)
129 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000130
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000131 arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
132 arena->a_cur = arena->a_head;
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000133 if (!arena->a_head) {
134 free((void *)arena);
135 return NULL;
136 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000137 arena->a_objects = PyList_New(0);
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000138 if (!arena->a_objects) {
139 block_free(arena->a_head);
140 free((void *)arena);
141 return NULL;
142 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000143#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 Hylton08533fd2006-02-28 18:29:00 +0000150 return arena;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000151}
152
153void
154PyArena_Free(PyArena *arena)
155{
Jeremy Hylton296aef82006-03-31 16:41:22 +0000156 int r;
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000157 assert(arena);
Jeremy Hylton56820c22006-02-28 19:57:06 +0000158#if defined(Py_DEBUG)
159 /*
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000160 fprintf(stderr,
Jeremy Hylton56820c22006-02-28 19:57:06 +0000161 "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 Hylton08533fd2006-02-28 18:29:00 +0000167 block_free(arena->a_head);
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000168 assert(arena->a_objects->ob_refcnt == 1);
Jeremy Hylton296aef82006-03-31 16:41:22 +0000169
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 Hylton99b4ee62006-02-28 18:52:28 +0000176 Py_DECREF(arena->a_objects);
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000177 free(arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178}
179
180void *
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000181PyArena_Malloc(PyArena *arena, size_t size)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000182{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000183 void *p = block_alloc(arena->a_cur, size);
184 if (!p)
185 return NULL;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000186#if defined(Py_DEBUG)
187 arena->total_allocs++;
188 arena->total_size += size;
189#endif
Jeremy Hylton08533fd2006-02-28 18:29:00 +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)
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 Peters6fd92dc2006-03-02 21:14:45 +0000197 ++arena->total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000198#endif
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000199 }
200 return p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000201}
202
203int
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000204PyArena_AddPyObject(PyArena *arena, PyObject *obj)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000205{
Jeremy Hylton224003b2006-03-01 15:02:24 +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}