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