blob: 242ca1d67da604518c8c5a9d257b08fa47bd60c4 [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.
9*/
Neal Norwitzadb69fc2005-12-17 20:54:49 +000010
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000011#define DEFAULT_BLOCK_SIZE 8192
12typedef struct _block {
Tim Petersf6386302006-03-02 21:41:18 +000013 /* 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 Hylton08533fd2006-02-28 18:29:00 +000017 size_t ab_size;
Tim Petersf6386302006-03-02 21:41:18 +000018
19 /* Total number of bytes already passed out. The next byte available
20 * to pass out starts at ab_mem + ab_offset.
21 */
Jeremy Hylton08533fd2006-02-28 18:29:00 +000022 size_t ab_offset;
Tim Petersf6386302006-03-02 21:41:18 +000023
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 Hylton08533fd2006-02-28 18:29:00 +000028 struct _block *ab_next;
Tim Petersf6386302006-03-02 21:41:18 +000029
30 /* Pointer to the first allocatable byte owned by this block. Read-
31 * only after initialization.
32 */
Jeremy Hylton08533fd2006-02-28 18:29:00 +000033 void *ab_mem;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000034} block;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000035
Jeremy Hylton99b4ee62006-02-28 18:52:28 +000036/* 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 Peters8cfaa0e2006-03-02 20:37:32 +000040
Neal Norwitzadb69fc2005-12-17 20:54:49 +000041struct _arena {
Jeremy Hylton08533fd2006-02-28 18:29:00 +000042 block *a_head;
43 block *a_cur;
Jeremy Hylton99b4ee62006-02-28 18:52:28 +000044 PyObject *a_objects;
Jeremy Hylton56820c22006-02-28 19:57:06 +000045#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 Norwitzadb69fc2005-12-17 20:54:49 +000053};
54
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000055static block *
56block_new(size_t size)
57{
Tim Peters8cfaa0e2006-03-02 20:37:32 +000058 /* Allocate header and block as one unit.
Jeremy Hylton08533fd2006-02-28 18:29:00 +000059 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 Norwitzadb69fc2005-12-17 20:54:49 +000068}
69
70static void
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000071block_free(block *b) {
Jeremy Hylton08533fd2006-02-28 18:29:00 +000072 while (b) {
73 block *next = b->ab_next;
74 free(b);
75 b = next;
76 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +000077}
78
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000079static void *
80block_alloc(block *b, size_t size)
81{
Jeremy Hylton08533fd2006-02-28 18:29:00 +000082 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 Hylton77f1bb22006-02-28 17:53:04 +000098
Jeremy Hylton08533fd2006-02-28 18:29:00 +000099 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 Hylton77f1bb22006-02-28 17:53:04 +0000103}
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000104
105PyArena *
106PyArena_New()
107{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000108 PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
109 if (!arena)
110 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000111
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000112 arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
113 arena->a_cur = arena->a_head;
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000114 if (!arena->a_head) {
115 free((void *)arena);
116 return NULL;
117 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000118 arena->a_objects = PyList_New(0);
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000119 if (!arena->a_objects) {
120 block_free(arena->a_head);
121 free((void *)arena);
122 return NULL;
123 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000124#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 Hylton08533fd2006-02-28 18:29:00 +0000131 return arena;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000132}
133
134void
135PyArena_Free(PyArena *arena)
136{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000137 assert(arena);
Jeremy Hylton56820c22006-02-28 19:57:06 +0000138#if defined(Py_DEBUG)
139 /*
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000140 fprintf(stderr,
Jeremy Hylton56820c22006-02-28 19:57:06 +0000141 "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 Hylton08533fd2006-02-28 18:29:00 +0000147 block_free(arena->a_head);
Jeremy Hylton99b4ee62006-02-28 18:52:28 +0000148 assert(arena->a_objects->ob_refcnt == 1);
149 Py_DECREF(arena->a_objects);
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000150 free(arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000151}
152
153void *
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000154PyArena_Malloc(PyArena *arena, size_t size)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000155{
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000156 void *p = block_alloc(arena->a_cur, size);
157 if (!p)
158 return NULL;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000159#if defined(Py_DEBUG)
160 arena->total_allocs++;
161 arena->total_size += size;
162#endif
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000163 /* 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 Hylton56820c22006-02-28 19:57:06 +0000166#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 Peters6fd92dc2006-03-02 21:14:45 +0000170 ++arena->total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000171#endif
Jeremy Hylton08533fd2006-02-28 18:29:00 +0000172 }
173 return p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000174}
175
176int
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000177PyArena_AddPyObject(PyArena *arena, PyObject *obj)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000178{
Jeremy Hylton224003b2006-03-01 15:02:24 +0000179 int r = PyList_Append(arena->a_objects, obj);
180 if (r >= 0) {
181 Py_DECREF(obj);
182 }
183 return r;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000184}