blob: 5a255ae497e16f4c1873a9ba900f38d575bd0efb [file] [log] [blame]
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001#include "Python.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002
Tim Petersf6386302006-03-02 21:41:18 +00003/* A simple arena block structure.
Jeremy Hylton56820c22006-02-28 19:57:06 +00004
5 Measurements with standard library modules suggest the average
6 allocation is about 20 bytes and that most compiles use a single
7 block.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008
9 TODO(jhylton): Think about a realloc API, maybe just for the last
10 allocation?
Jeremy Hylton56820c22006-02-28 19:57:06 +000011*/
Neal Norwitzadb69fc2005-12-17 20:54:49 +000012
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000013#define DEFAULT_BLOCK_SIZE 8192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014#define ALIGNMENT 8
15#define ALIGNMENT_MASK (ALIGNMENT - 1)
16#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000017
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000018typedef struct _block {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 /* Total number of bytes owned by this block available to pass out.
20 * Read-only after initialization. The first such byte starts at
21 * ab_mem.
22 */
23 size_t ab_size;
Tim Petersf6386302006-03-02 21:41:18 +000024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 /* Total number of bytes already passed out. The next byte available
26 * to pass out starts at ab_mem + ab_offset.
27 */
28 size_t ab_offset;
Tim Petersf6386302006-03-02 21:41:18 +000029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 /* An arena maintains a singly-linked, NULL-terminated list of
31 * all blocks owned by the arena. These are linked via the
32 * ab_next member.
33 */
34 struct _block *ab_next;
Tim Petersf6386302006-03-02 21:41:18 +000035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 /* Pointer to the first allocatable byte owned by this block. Read-
37 * only after initialization.
38 */
39 void *ab_mem;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000040} block;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000041
Jeremy Hylton99b4ee62006-02-28 18:52:28 +000042/* The arena manages two kinds of memory, blocks of raw memory
43 and a list of PyObject* pointers. PyObjects are decrefed
44 when the arena is freed.
45*/
Tim Peters8cfaa0e2006-03-02 20:37:32 +000046
Neal Norwitzadb69fc2005-12-17 20:54:49 +000047struct _arena {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* Pointer to the first block allocated for the arena, never NULL.
49 It is used only to find the first block when the arena is
50 being freed.
51 */
52 block *a_head;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 /* Pointer to the block currently used for allocation. It's
55 ab_next field should be NULL. If it is not-null after a
56 call to block_alloc(), it means a new block has been allocated
57 and a_cur should be reset to point it.
58 */
59 block *a_cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 /* A Python list object containing references to all the PyObject
62 pointers associated with this area. They will be DECREFed
63 when the arena is freed.
64 */
65 PyObject *a_objects;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066
Jeremy Hylton56820c22006-02-28 19:57:06 +000067#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 /* Debug output */
69 size_t total_allocs;
70 size_t total_size;
71 size_t total_blocks;
72 size_t total_block_size;
73 size_t total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +000074#endif
Neal Norwitzadb69fc2005-12-17 20:54:49 +000075};
76
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000077static block *
78block_new(size_t size)
79{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 /* Allocate header and block as one unit.
81 ab_mem points just past header. */
82 block *b = (block *)malloc(sizeof(block) + size);
83 if (!b)
84 return NULL;
85 b->ab_size = size;
86 b->ab_mem = (void *)(b + 1);
87 b->ab_next = NULL;
88 b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) -
89 (Py_uintptr_t)(b->ab_mem);
90 return b;
Neal Norwitzadb69fc2005-12-17 20:54:49 +000091}
92
93static void
Jeremy Hylton77f1bb22006-02-28 17:53:04 +000094block_free(block *b) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 while (b) {
96 block *next = b->ab_next;
97 free(b);
98 b = next;
99 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000100}
101
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000102static void *
103block_alloc(block *b, size_t size)
104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 void *p;
106 assert(b);
107 size = ROUNDUP(size);
108 if (b->ab_offset + size > b->ab_size) {
109 /* If we need to allocate more memory than will fit in
110 the default block, allocate a one-off block that is
111 exactly the right size. */
112 /* TODO(jhylton): Think about space waste at end of block */
113 block *newbl = block_new(
114 size < DEFAULT_BLOCK_SIZE ?
115 DEFAULT_BLOCK_SIZE : size);
116 if (!newbl)
117 return NULL;
118 assert(!b->ab_next);
119 b->ab_next = newbl;
120 b = newbl;
121 }
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 assert(b->ab_offset + size <= b->ab_size);
124 p = (void *)(((char *)b->ab_mem) + b->ab_offset);
125 b->ab_offset += size;
126 return p;
Jeremy Hylton77f1bb22006-02-28 17:53:04 +0000127}
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000128
129PyArena *
130PyArena_New()
131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
133 if (!arena)
134 return (PyArena*)PyErr_NoMemory();
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
137 arena->a_cur = arena->a_head;
138 if (!arena->a_head) {
139 free((void *)arena);
140 return (PyArena*)PyErr_NoMemory();
141 }
142 arena->a_objects = PyList_New(0);
143 if (!arena->a_objects) {
144 block_free(arena->a_head);
145 free((void *)arena);
146 return (PyArena*)PyErr_NoMemory();
147 }
Jeremy Hylton56820c22006-02-28 19:57:06 +0000148#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 arena->total_allocs = 0;
150 arena->total_size = 0;
151 arena->total_blocks = 1;
152 arena->total_block_size = DEFAULT_BLOCK_SIZE;
153 arena->total_big_blocks = 0;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000154#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return arena;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000156}
157
158void
159PyArena_Free(PyArena *arena)
160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 int r;
162 assert(arena);
Jeremy Hylton56820c22006-02-28 19:57:06 +0000163#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 /*
165 fprintf(stderr,
166 "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n",
167 arena->total_allocs, arena->total_size, arena->total_blocks,
168 arena->total_block_size, arena->total_big_blocks,
169 PyList_Size(arena->a_objects));
170 */
Jeremy Hylton56820c22006-02-28 19:57:06 +0000171#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 block_free(arena->a_head);
173 /* This property normally holds, except when the code being compiled
174 is sys.getobjects(0), in which case there will be two references.
175 assert(arena->a_objects->ob_refcnt == 1);
176 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* Clear all the elements from the list. This is necessary
179 to guarantee that they will be DECREFed. */
180 r = PyList_SetSlice(arena->a_objects,
181 0, PyList_GET_SIZE(arena->a_objects), NULL);
182 assert(r == 0);
183 assert(PyList_GET_SIZE(arena->a_objects) == 0);
184 Py_DECREF(arena->a_objects);
185 free(arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000186}
187
188void *
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000189PyArena_Malloc(PyArena *arena, size_t size)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 void *p = block_alloc(arena->a_cur, size);
192 if (!p)
193 return PyErr_NoMemory();
Jeremy Hylton56820c22006-02-28 19:57:06 +0000194#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 arena->total_allocs++;
196 arena->total_size += size;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* Reset cur if we allocated a new block. */
199 if (arena->a_cur->ab_next) {
200 arena->a_cur = arena->a_cur->ab_next;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000201#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 arena->total_blocks++;
203 arena->total_block_size += arena->a_cur->ab_size;
204 if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
205 ++arena->total_big_blocks;
Jeremy Hylton56820c22006-02-28 19:57:06 +0000206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 }
208 return p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000209}
210
211int
Tim Peters8cfaa0e2006-03-02 20:37:32 +0000212PyArena_AddPyObject(PyArena *arena, PyObject *obj)
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 int r = PyList_Append(arena->a_objects, obj);
215 if (r >= 0) {
216 Py_DECREF(obj);
217 }
218 return r;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000219}