1 | n/a | #include "Python.h" |
---|
2 | n/a | |
---|
3 | n/a | /* A simple arena block structure. |
---|
4 | n/a | |
---|
5 | n/a | Measurements with standard library modules suggest the average |
---|
6 | n/a | allocation is about 20 bytes and that most compiles use a single |
---|
7 | n/a | block. |
---|
8 | n/a | |
---|
9 | n/a | TODO(jhylton): Think about a realloc API, maybe just for the last |
---|
10 | n/a | allocation? |
---|
11 | n/a | */ |
---|
12 | n/a | |
---|
13 | n/a | #define DEFAULT_BLOCK_SIZE 8192 |
---|
14 | n/a | #define ALIGNMENT 8 |
---|
15 | n/a | |
---|
16 | n/a | typedef struct _block { |
---|
17 | n/a | /* Total number of bytes owned by this block available to pass out. |
---|
18 | n/a | * Read-only after initialization. The first such byte starts at |
---|
19 | n/a | * ab_mem. |
---|
20 | n/a | */ |
---|
21 | n/a | size_t ab_size; |
---|
22 | n/a | |
---|
23 | n/a | /* Total number of bytes already passed out. The next byte available |
---|
24 | n/a | * to pass out starts at ab_mem + ab_offset. |
---|
25 | n/a | */ |
---|
26 | n/a | size_t ab_offset; |
---|
27 | n/a | |
---|
28 | n/a | /* An arena maintains a singly-linked, NULL-terminated list of |
---|
29 | n/a | * all blocks owned by the arena. These are linked via the |
---|
30 | n/a | * ab_next member. |
---|
31 | n/a | */ |
---|
32 | n/a | struct _block *ab_next; |
---|
33 | n/a | |
---|
34 | n/a | /* Pointer to the first allocatable byte owned by this block. Read- |
---|
35 | n/a | * only after initialization. |
---|
36 | n/a | */ |
---|
37 | n/a | void *ab_mem; |
---|
38 | n/a | } block; |
---|
39 | n/a | |
---|
40 | n/a | /* The arena manages two kinds of memory, blocks of raw memory |
---|
41 | n/a | and a list of PyObject* pointers. PyObjects are decrefed |
---|
42 | n/a | when the arena is freed. |
---|
43 | n/a | */ |
---|
44 | n/a | |
---|
45 | n/a | struct _arena { |
---|
46 | n/a | /* Pointer to the first block allocated for the arena, never NULL. |
---|
47 | n/a | It is used only to find the first block when the arena is |
---|
48 | n/a | being freed. |
---|
49 | n/a | */ |
---|
50 | n/a | block *a_head; |
---|
51 | n/a | |
---|
52 | n/a | /* Pointer to the block currently used for allocation. It's |
---|
53 | n/a | ab_next field should be NULL. If it is not-null after a |
---|
54 | n/a | call to block_alloc(), it means a new block has been allocated |
---|
55 | n/a | and a_cur should be reset to point it. |
---|
56 | n/a | */ |
---|
57 | n/a | block *a_cur; |
---|
58 | n/a | |
---|
59 | n/a | /* A Python list object containing references to all the PyObject |
---|
60 | n/a | pointers associated with this area. They will be DECREFed |
---|
61 | n/a | when the arena is freed. |
---|
62 | n/a | */ |
---|
63 | n/a | PyObject *a_objects; |
---|
64 | n/a | |
---|
65 | n/a | #if defined(Py_DEBUG) |
---|
66 | n/a | /* Debug output */ |
---|
67 | n/a | size_t total_allocs; |
---|
68 | n/a | size_t total_size; |
---|
69 | n/a | size_t total_blocks; |
---|
70 | n/a | size_t total_block_size; |
---|
71 | n/a | size_t total_big_blocks; |
---|
72 | n/a | #endif |
---|
73 | n/a | }; |
---|
74 | n/a | |
---|
75 | n/a | static block * |
---|
76 | n/a | block_new(size_t size) |
---|
77 | n/a | { |
---|
78 | n/a | /* Allocate header and block as one unit. |
---|
79 | n/a | ab_mem points just past header. */ |
---|
80 | n/a | block *b = (block *)PyMem_Malloc(sizeof(block) + size); |
---|
81 | n/a | if (!b) |
---|
82 | n/a | return NULL; |
---|
83 | n/a | b->ab_size = size; |
---|
84 | n/a | b->ab_mem = (void *)(b + 1); |
---|
85 | n/a | b->ab_next = NULL; |
---|
86 | n/a | b->ab_offset = (char *)_Py_ALIGN_UP(b->ab_mem, ALIGNMENT) - |
---|
87 | n/a | (char *)(b->ab_mem); |
---|
88 | n/a | return b; |
---|
89 | n/a | } |
---|
90 | n/a | |
---|
91 | n/a | static void |
---|
92 | n/a | block_free(block *b) { |
---|
93 | n/a | while (b) { |
---|
94 | n/a | block *next = b->ab_next; |
---|
95 | n/a | PyMem_Free(b); |
---|
96 | n/a | b = next; |
---|
97 | n/a | } |
---|
98 | n/a | } |
---|
99 | n/a | |
---|
100 | n/a | static void * |
---|
101 | n/a | block_alloc(block *b, size_t size) |
---|
102 | n/a | { |
---|
103 | n/a | void *p; |
---|
104 | n/a | assert(b); |
---|
105 | n/a | size = _Py_SIZE_ROUND_UP(size, ALIGNMENT); |
---|
106 | n/a | if (b->ab_offset + size > b->ab_size) { |
---|
107 | n/a | /* If we need to allocate more memory than will fit in |
---|
108 | n/a | the default block, allocate a one-off block that is |
---|
109 | n/a | exactly the right size. */ |
---|
110 | n/a | /* TODO(jhylton): Think about space waste at end of block */ |
---|
111 | n/a | block *newbl = block_new( |
---|
112 | n/a | size < DEFAULT_BLOCK_SIZE ? |
---|
113 | n/a | DEFAULT_BLOCK_SIZE : size); |
---|
114 | n/a | if (!newbl) |
---|
115 | n/a | return NULL; |
---|
116 | n/a | assert(!b->ab_next); |
---|
117 | n/a | b->ab_next = newbl; |
---|
118 | n/a | b = newbl; |
---|
119 | n/a | } |
---|
120 | n/a | |
---|
121 | n/a | assert(b->ab_offset + size <= b->ab_size); |
---|
122 | n/a | p = (void *)(((char *)b->ab_mem) + b->ab_offset); |
---|
123 | n/a | b->ab_offset += size; |
---|
124 | n/a | return p; |
---|
125 | n/a | } |
---|
126 | n/a | |
---|
127 | n/a | PyArena * |
---|
128 | n/a | PyArena_New() |
---|
129 | n/a | { |
---|
130 | n/a | PyArena* arena = (PyArena *)PyMem_Malloc(sizeof(PyArena)); |
---|
131 | n/a | if (!arena) |
---|
132 | n/a | return (PyArena*)PyErr_NoMemory(); |
---|
133 | n/a | |
---|
134 | n/a | arena->a_head = block_new(DEFAULT_BLOCK_SIZE); |
---|
135 | n/a | arena->a_cur = arena->a_head; |
---|
136 | n/a | if (!arena->a_head) { |
---|
137 | n/a | PyMem_Free((void *)arena); |
---|
138 | n/a | return (PyArena*)PyErr_NoMemory(); |
---|
139 | n/a | } |
---|
140 | n/a | arena->a_objects = PyList_New(0); |
---|
141 | n/a | if (!arena->a_objects) { |
---|
142 | n/a | block_free(arena->a_head); |
---|
143 | n/a | PyMem_Free((void *)arena); |
---|
144 | n/a | return (PyArena*)PyErr_NoMemory(); |
---|
145 | n/a | } |
---|
146 | n/a | #if defined(Py_DEBUG) |
---|
147 | n/a | arena->total_allocs = 0; |
---|
148 | n/a | arena->total_size = 0; |
---|
149 | n/a | arena->total_blocks = 1; |
---|
150 | n/a | arena->total_block_size = DEFAULT_BLOCK_SIZE; |
---|
151 | n/a | arena->total_big_blocks = 0; |
---|
152 | n/a | #endif |
---|
153 | n/a | return arena; |
---|
154 | n/a | } |
---|
155 | n/a | |
---|
156 | n/a | void |
---|
157 | n/a | PyArena_Free(PyArena *arena) |
---|
158 | n/a | { |
---|
159 | n/a | assert(arena); |
---|
160 | n/a | #if defined(Py_DEBUG) |
---|
161 | n/a | /* |
---|
162 | n/a | fprintf(stderr, |
---|
163 | n/a | "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n", |
---|
164 | n/a | arena->total_allocs, arena->total_size, arena->total_blocks, |
---|
165 | n/a | arena->total_block_size, arena->total_big_blocks, |
---|
166 | n/a | PyList_Size(arena->a_objects)); |
---|
167 | n/a | */ |
---|
168 | n/a | #endif |
---|
169 | n/a | block_free(arena->a_head); |
---|
170 | n/a | /* This property normally holds, except when the code being compiled |
---|
171 | n/a | is sys.getobjects(0), in which case there will be two references. |
---|
172 | n/a | assert(arena->a_objects->ob_refcnt == 1); |
---|
173 | n/a | */ |
---|
174 | n/a | |
---|
175 | n/a | Py_DECREF(arena->a_objects); |
---|
176 | n/a | PyMem_Free(arena); |
---|
177 | n/a | } |
---|
178 | n/a | |
---|
179 | n/a | void * |
---|
180 | n/a | PyArena_Malloc(PyArena *arena, size_t size) |
---|
181 | n/a | { |
---|
182 | n/a | void *p = block_alloc(arena->a_cur, size); |
---|
183 | n/a | if (!p) |
---|
184 | n/a | return PyErr_NoMemory(); |
---|
185 | n/a | #if defined(Py_DEBUG) |
---|
186 | n/a | arena->total_allocs++; |
---|
187 | n/a | arena->total_size += size; |
---|
188 | n/a | #endif |
---|
189 | n/a | /* Reset cur if we allocated a new block. */ |
---|
190 | n/a | if (arena->a_cur->ab_next) { |
---|
191 | n/a | arena->a_cur = arena->a_cur->ab_next; |
---|
192 | n/a | #if defined(Py_DEBUG) |
---|
193 | n/a | arena->total_blocks++; |
---|
194 | n/a | arena->total_block_size += arena->a_cur->ab_size; |
---|
195 | n/a | if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE) |
---|
196 | n/a | ++arena->total_big_blocks; |
---|
197 | n/a | #endif |
---|
198 | n/a | } |
---|
199 | n/a | return p; |
---|
200 | n/a | } |
---|
201 | n/a | |
---|
202 | n/a | int |
---|
203 | n/a | PyArena_AddPyObject(PyArena *arena, PyObject *obj) |
---|
204 | n/a | { |
---|
205 | n/a | int r = PyList_Append(arena->a_objects, obj); |
---|
206 | n/a | if (r >= 0) { |
---|
207 | n/a | Py_DECREF(obj); |
---|
208 | n/a | } |
---|
209 | n/a | return r; |
---|
210 | n/a | } |
---|