blob: c5491e954a8e37700d4e1e51ad360f253e32157d [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
Martin v. Löwisda690412006-04-13 19:16:13 +000015#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 Hylton77f1bb22006-02-28 17:53:04 +000020typedef struct _block {
Tim Petersf6386302006-03-02 21:41:18 +000021 /* 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 Hylton08533fd2006-02-28 18:29:00 +000025 size_t ab_size;
Tim Petersf6386302006-03-02 21:41:18 +000026
27 /* Total number of bytes already passed out. The next byte available
28 * to pass out starts at ab_mem + ab_offset.
29 */
Jeremy Hylton08533fd2006-02-28 18:29:00 +000030 size_t ab_offset;
Tim Petersf6386302006-03-02 21:41:18 +000031
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 Hylton08533fd2006-02-28 18:29:00 +000036 struct _block *ab_next;
Tim Petersf6386302006-03-02 21:41:18 +000037
38 /* Pointer to the first allocatable byte owned by this block. Read-
39 * only after initialization.
40 */
Jeremy Hylton08533fd2006-02-28 18:29:00 +000041 void *ab_mem;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000042} block;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000043
Jeremy Hylton99b4ee62006-02-28 18:52:28 +000044/* 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 Peters8cfaa0e2006-03-02 20:37:32 +000048
Neal Norwitzadb69fc2005-12-17 20:54:49 +000049struct _arena {
Jeremy Hylton296aef82006-03-31 16:41:22 +000050 /* 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 Hylton08533fd2006-02-28 18:29:00 +000054 block *a_head;
Jeremy Hylton296aef82006-03-31 16:41:22 +000055
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 Hylton08533fd2006-02-28 18:29:00 +000061 block *a_cur;
Jeremy Hylton296aef82006-03-31 16:41:22 +000062
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 Hylton99b4ee62006-02-28 18:52:28 +000067 PyObject *a_objects;
Jeremy Hylton296aef82006-03-31 16:41:22 +000068
Jeremy Hylton56820c22006-02-28 19:57:06 +000069#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 Norwitzadb69fc2005-12-17 20:54:49 +000077};
78
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000079static block *
80block_new(size_t size)
81{
Tim Peters8cfaa0e2006-03-02 20:37:32 +000082 /* Allocate header and block as one unit.
Jeremy Hylton08533fd2006-02-28 18:29:00 +000083 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öwisda690412006-04-13 19:16:13 +000090 b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) -
91 (Py_uintptr_t)(b->ab_mem);
Jeremy Hylton08533fd2006-02-28 18:29:00 +000092 return b;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000093}
94
95static void
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000096block_free(block *b) {
Jeremy Hylton08533fd2006-02-28 18:29:00 +000097 while (b) {
98 block *next = b->ab_next;
99 free(b);
100 b = next;
101 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000102}
103
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000104static void *
105block_alloc(block *b, size_t size)
106{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000107 void *p;
108 assert(b);
Martin v. Löwisda690412006-04-13 19:16:13 +0000109 size = ROUNDUP(size);
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000110 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 Baxter7b782b62006-04-11 12:01:56 +0000115 block *newbl = block_new(
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000116 size < DEFAULT_BLOCK_SIZE ?
117 DEFAULT_BLOCK_SIZE : size);
Anthony Baxter7b782b62006-04-11 12:01:56 +0000118 if (!newbl)
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000119 return NULL;
120 assert(!b->ab_next);
Anthony Baxter7b782b62006-04-11 12:01:56 +0000121 b->ab_next = newbl;
122 b = newbl;
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000123 }
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000124
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000125 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 Hylton77f1bb22006-02-28 17:53:04 +0000129}
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000130
131PyArena *
132PyArena_New()
133{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000134 PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
135 if (!arena)
136 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000137
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000138 arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
139 arena->a_cur = arena->a_head;
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000140 if (!arena->a_head) {
141 free((void *)arena);
142 return NULL;
143 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000144 arena->a_objects = PyList_New(0);
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000145 if (!arena->a_objects) {
146 block_free(arena->a_head);
147 free((void *)arena);
148 return NULL;
149 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000150#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 Hylton08533fd2006-02-28 18:29:00 +0000157 return arena;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000158}
159
160void
161PyArena_Free(PyArena *arena)
162{
Jeremy Hylton296aef82006-03-31 16:41:22 +0000163 int r;
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000164 assert(arena);
Jeremy Hylton56820c22006-02-28 19:57:06 +0000165#if defined(Py_DEBUG)
166 /*
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000167 fprintf(stderr,
Jeremy Hylton56820c22006-02-28 19:57:06 +0000168 "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 Hylton08533fd2006-02-28 18:29:00 +0000174 block_free(arena->a_head);
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000175 assert(arena->a_objects->ob_refcnt == 1);
Jeremy Hylton296aef82006-03-31 16:41:22 +0000176
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 Hylton99b4ee62006-02-28 18:52:28 +0000183 Py_DECREF(arena->a_objects);
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000184 free(arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000185}
186
187void *
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000188PyArena_Malloc(PyArena *arena, size_t size)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000189{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000190 void *p = block_alloc(arena->a_cur, size);
191 if (!p)
192 return NULL;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000193#if defined(Py_DEBUG)
194 arena->total_allocs++;
195 arena->total_size += size;
196#endif
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000197 /* 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 Hylton56820c22006-02-28 19:57:06 +0000200#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 Peters6fd92dc2006-03-02 21:14:45 +0000204 ++arena->total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000205#endif
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000206 }
207 return p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000208}
209
210int
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000211PyArena_AddPyObject(PyArena *arena, PyObject *obj)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000212{
Jeremy Hylton224003b2006-03-01 15:02:24 +0000213 int r = PyList_Append(arena->a_objects, obj);
214 if (r >= 0) {
215 Py_DECREF(obj);
216 }
217 return r;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000218}