Mercurial > cpython-lru_cache-weakref
annotate Python/pyarena.c @ 37690:751f7c79b5fd legacy-trunk
Force 8-alignment of memory blocks, as needed on
64-bit machines that require pointers to be aligned (e.g. IA64)
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 13 Apr 2006 19:16:13 +0000 |
| parents | f448d0075691 |
| children | ac79e9037e06 |
| rev | line source |
|---|---|
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1 #include "Python.h" |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2 #include "pyarena.h" |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
3 |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
4 /* A simple arena block structure. |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
5 |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
6 Measurements with standard library modules suggest the average |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
7 allocation is about 20 bytes and that most compiles use a single |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
8 block. |
| 37337 | 9 |
| 10 TODO(jhylton): Think about a realloc API, maybe just for the last | |
| 11 allocation? | |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
12 */ |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
13 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
14 #define DEFAULT_BLOCK_SIZE 8192 |
|
37690
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
15 #define ALIGNMENT 8 |
|
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
16 #define ALIGNMENT_SHIFT 3 |
|
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
17 #define ALIGNMENT_MASK (ALIGNMENT - 1) |
|
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
18 #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
|
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
19 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
20 typedef struct _block { |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
21 /* Total number of bytes owned by this block available to pass out. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
22 * Read-only after initialization. The first such byte starts at |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
23 * ab_mem. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
24 */ |
| 36720 | 25 size_t ab_size; |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
26 |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
27 /* Total number of bytes already passed out. The next byte available |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
28 * to pass out starts at ab_mem + ab_offset. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
29 */ |
| 36720 | 30 size_t ab_offset; |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
31 |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
32 /* An arena maintains a singly-linked, NULL-terminated list of |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
33 * all blocks owned by the arena. These are linked via the |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
34 * ab_next member. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
35 */ |
| 36720 | 36 struct _block *ab_next; |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
37 |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
38 /* Pointer to the first allocatable byte owned by this block. Read- |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
39 * only after initialization. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
40 */ |
| 36720 | 41 void *ab_mem; |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
42 } block; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
43 |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
44 /* The arena manages two kinds of memory, blocks of raw memory |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
45 and a list of PyObject* pointers. PyObjects are decrefed |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
46 when the arena is freed. |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
47 */ |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
48 |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
49 struct _arena { |
| 37337 | 50 /* 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 */ | |
| 36720 | 54 block *a_head; |
| 37337 | 55 |
| 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 */ | |
| 36720 | 61 block *a_cur; |
| 37337 | 62 |
| 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 */ | |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
67 PyObject *a_objects; |
| 37337 | 68 |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
69 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
70 /* Debug output */ |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
71 size_t total_allocs; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
72 size_t total_size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
73 size_t total_blocks; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
74 size_t total_block_size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
75 size_t total_big_blocks; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
76 #endif |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
77 }; |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
78 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
79 static block * |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
80 block_new(size_t size) |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
81 { |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
82 /* Allocate header and block as one unit. |
| 36720 | 83 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; | |
|
37690
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
90 b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) - |
|
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
91 (Py_uintptr_t)(b->ab_mem); |
| 36720 | 92 return b; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
93 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
94 |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
95 static void |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
96 block_free(block *b) { |
| 36720 | 97 while (b) { |
| 98 block *next = b->ab_next; | |
| 99 free(b); | |
| 100 b = next; | |
| 101 } | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
102 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
103 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
104 static void * |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
105 block_alloc(block *b, size_t size) |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
106 { |
| 36720 | 107 void *p; |
| 108 assert(b); | |
|
37690
751f7c79b5fd
Force 8-alignment of memory blocks, as needed on
Martin v. Löwis <martin@v.loewis.de>
parents:
37606
diff
changeset
|
109 size = ROUNDUP(size); |
| 36720 | 110 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 */ | |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37337
diff
changeset
|
115 block *newbl = block_new( |
| 36720 | 116 size < DEFAULT_BLOCK_SIZE ? |
| 117 DEFAULT_BLOCK_SIZE : size); | |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37337
diff
changeset
|
118 if (!newbl) |
| 36720 | 119 return NULL; |
| 120 assert(!b->ab_next); | |
|
37606
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37337
diff
changeset
|
121 b->ab_next = newbl; |
|
f448d0075691
more low-hanging fruit to make code compile under a C++ compiler. Not
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37337
diff
changeset
|
122 b = newbl; |
| 36720 | 123 } |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
124 |
| 36720 | 125 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; | |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
129 } |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
130 |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
131 PyArena * |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
132 PyArena_New() |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
133 { |
| 36720 | 134 PyArena* arena = (PyArena *)malloc(sizeof(PyArena)); |
| 135 if (!arena) | |
| 136 return NULL; | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
137 |
| 36720 | 138 arena->a_head = block_new(DEFAULT_BLOCK_SIZE); |
| 139 arena->a_cur = arena->a_head; | |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
140 if (!arena->a_head) { |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
141 free((void *)arena); |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
142 return NULL; |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
143 } |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
144 arena->a_objects = PyList_New(0); |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
145 if (!arena->a_objects) { |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
146 block_free(arena->a_head); |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
147 free((void *)arena); |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
148 return NULL; |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
149 } |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
150 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
151 arena->total_allocs = 0; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
152 arena->total_size = 0; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
153 arena->total_blocks = 1; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
154 arena->total_block_size = DEFAULT_BLOCK_SIZE; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
155 arena->total_big_blocks = 0; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
156 #endif |
| 36720 | 157 return arena; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
158 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
159 |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
160 void |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
161 PyArena_Free(PyArena *arena) |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
162 { |
| 37337 | 163 int r; |
| 36720 | 164 assert(arena); |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
165 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
166 /* |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
167 fprintf(stderr, |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
168 "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n", |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
169 arena->total_allocs, arena->total_size, arena->total_blocks, |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
170 arena->total_block_size, arena->total_big_blocks, |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
171 PyList_Size(arena->a_objects)); |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
172 */ |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
173 #endif |
| 36720 | 174 block_free(arena->a_head); |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
175 assert(arena->a_objects->ob_refcnt == 1); |
| 37337 | 176 |
| 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); | |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
183 Py_DECREF(arena->a_objects); |
| 36720 | 184 free(arena); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
185 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
186 |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
187 void * |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
188 PyArena_Malloc(PyArena *arena, size_t size) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
189 { |
| 36720 | 190 void *p = block_alloc(arena->a_cur, size); |
| 191 if (!p) | |
| 192 return NULL; | |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
193 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
194 arena->total_allocs++; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
195 arena->total_size += size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
196 #endif |
| 36720 | 197 /* Reset cur if we allocated a new block. */ |
| 198 if (arena->a_cur->ab_next) { | |
| 199 arena->a_cur = arena->a_cur->ab_next; | |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
200 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
201 arena->total_blocks++; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
202 arena->total_block_size += arena->a_cur->ab_size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
203 if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE) |
|
36812
1d055ebd8322
Added words about what PyArena_Malloc() does.
Tim Peters <tim.peters@gmail.com>
parents:
36808
diff
changeset
|
204 ++arena->total_big_blocks; |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
205 #endif |
| 36720 | 206 } |
| 207 return p; | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
208 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
209 |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
210 int |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
211 PyArena_AddPyObject(PyArena *arena, PyObject *obj) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
212 { |
| 36765 | 213 int r = PyList_Append(arena->a_objects, obj); |
| 214 if (r >= 0) { | |
| 215 Py_DECREF(obj); | |
| 216 } | |
| 217 return r; | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
218 } |
