Mercurial > cpython-lru_cache-weakref
annotate Python/pyarena.c @ 58664:993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77484 | skip.montanaro | 2010-01-13 19:12:34 -0600 (Wed, 13 Jan 2010) | 4 lines
Update PyEval_EvalFrame to PyEval_EvalFrameEx. This looks to have been done
partially before. Also add a comment describing how this might have to work
with different versions of the interpreter.
........
r77487 | ezio.melotti | 2010-01-14 05:34:10 -0600 (Thu, 14 Jan 2010) | 1 line
Fixed typo
........
r77561 | georg.brandl | 2010-01-17 02:42:30 -0600 (Sun, 17 Jan 2010) | 1 line
#7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such.
........
r77570 | georg.brandl | 2010-01-17 06:14:42 -0600 (Sun, 17 Jan 2010) | 1 line
Add note about usage of STRINGLIB_EMPTY.
........
r77593 | georg.brandl | 2010-01-17 17:33:53 -0600 (Sun, 17 Jan 2010) | 1 line
Fix internal reference.
........
r77603 | benjamin.peterson | 2010-01-18 17:07:56 -0600 (Mon, 18 Jan 2010) | 8 lines
data descriptors do not override the class dictionary if __get__ is not defined
Adjust documentation and add a test to verify this behavior.
See http://mail.python.org/pipermail/python-dev/2010-January/095637.html for
discussion.
........
r77608 | gregory.p.smith | 2010-01-19 02:19:03 -0600 (Tue, 19 Jan 2010) | 6 lines
Do not compile stubs for the sha2 series hashes in the openssl hashlib
module when the openssl version is too old to support them. That
leads both compiled code bloat and to unittests attempting to test
implementations that don't exist for comparison purposes on such
platforms.
........
r77667 | mark.dickinson | 2010-01-21 12:32:27 -0600 (Thu, 21 Jan 2010) | 1 line
Add two more test_strtod test values.
........
r77702 | georg.brandl | 2010-01-23 02:43:31 -0600 (Sat, 23 Jan 2010) | 1 line
#7762: fix refcount annotation of PyUnicode_Tailmatch().
........
r77703 | georg.brandl | 2010-01-23 02:47:54 -0600 (Sat, 23 Jan 2010) | 1 line
#7725: fix referencing issue.
........
r77739 | benjamin.peterson | 2010-01-24 21:52:52 -0600 (Sun, 24 Jan 2010) | 1 line
mention from_float() in error message
........
r77858 | georg.brandl | 2010-01-30 11:57:48 -0600 (Sat, 30 Jan 2010) | 1 line
#7802: fix invalid example (heh).
........
r77887 | georg.brandl | 2010-01-31 12:51:49 -0600 (Sun, 31 Jan 2010) | 5 lines
Fix-up ftplib documentation:
move exception descriptions to toplevel, not inside a class
remove attribution in "versionadded"
spell and grammar check docstring of FTP_TLS
........
r77889 | michael.foord | 2010-01-31 13:59:26 -0600 (Sun, 31 Jan 2010) | 1 line
Minor modification to unittest documentation.
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Wed, 03 Feb 2010 02:35:45 +0000 |
| parents | 654c380cf8b9 |
| children | cab14be0ada1 |
| 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. |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
9 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
10 TODO(jhylton): Think about a realloc API, maybe just for the last |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
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 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
15 #define ALIGNMENT 8 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
16 #define ALIGNMENT_MASK (ALIGNMENT - 1) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
17 #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
18 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
19 typedef struct _block { |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
20 /* 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
|
21 * 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
|
22 * ab_mem. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
23 */ |
| 36720 | 24 size_t ab_size; |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
25 |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
26 /* 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
|
27 * 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
|
28 */ |
| 36720 | 29 size_t ab_offset; |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
30 |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
31 /* 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
|
32 * 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
|
33 * ab_next member. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
34 */ |
| 36720 | 35 struct _block *ab_next; |
|
36813
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
36 |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
37 /* 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
|
38 * only after initialization. |
|
54754f0d25c7
Document the purpose of the struct _block members.
Tim Peters <tim.peters@gmail.com>
parents:
36812
diff
changeset
|
39 */ |
| 36720 | 40 void *ab_mem; |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
41 } 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
|
42 |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
43 /* 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
|
44 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
|
45 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
|
46 */ |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
47 |
|
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
|
48 struct _arena { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
49 /* Pointer to the first block allocated for the arena, never NULL. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
50 It is used only to find the first block when the arena is |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
51 being freed. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
52 */ |
| 36720 | 53 block *a_head; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
54 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
55 /* Pointer to the block currently used for allocation. It's |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
56 ab_next field should be NULL. If it is not-null after a |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
57 call to block_alloc(), it means a new block has been allocated |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
58 and a_cur should be reset to point it. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
59 */ |
| 36720 | 60 block *a_cur; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
61 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
62 /* A Python list object containing references to all the PyObject |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
63 pointers associated with this area. They will be DECREFed |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
64 when the arena is freed. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
65 */ |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
66 PyObject *a_objects; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
67 |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
68 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
69 /* Debug output */ |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
70 size_t total_allocs; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
71 size_t total_size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
72 size_t total_blocks; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
73 size_t total_block_size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
74 size_t total_big_blocks; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
75 #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
|
76 }; |
|
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 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
78 static block * |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
79 block_new(size_t size) |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
80 { |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
81 /* Allocate header and block as one unit. |
| 36720 | 82 ab_mem points just past header. */ |
| 83 block *b = (block *)malloc(sizeof(block) + size); | |
| 84 if (!b) | |
| 85 return NULL; | |
| 86 b->ab_size = size; | |
| 87 b->ab_mem = (void *)(b + 1); | |
| 88 b->ab_next = NULL; | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
89 b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) - |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
90 (Py_uintptr_t)(b->ab_mem); |
| 36720 | 91 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
|
92 } |
|
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 static void |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
95 block_free(block *b) { |
| 36720 | 96 while (b) { |
| 97 block *next = b->ab_next; | |
| 98 free(b); | |
| 99 b = next; | |
| 100 } | |
|
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
|
101 } |
|
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 |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
103 static void * |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
104 block_alloc(block *b, size_t size) |
|
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
105 { |
| 36720 | 106 void *p; |
| 107 assert(b); | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
108 size = ROUNDUP(size); |
| 36720 | 109 if (b->ab_offset + size > b->ab_size) { |
| 110 /* If we need to allocate more memory than will fit in | |
| 111 the default block, allocate a one-off block that is | |
| 112 exactly the right size. */ | |
| 113 /* TODO(jhylton): Think about space waste at end of block */ | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
114 block *newbl = block_new( |
| 36720 | 115 size < DEFAULT_BLOCK_SIZE ? |
| 116 DEFAULT_BLOCK_SIZE : size); | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
117 if (!newbl) |
| 36720 | 118 return NULL; |
| 119 assert(!b->ab_next); | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
120 b->ab_next = newbl; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
121 b = newbl; |
| 36720 | 122 } |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
123 |
| 36720 | 124 assert(b->ab_offset + size <= b->ab_size); |
| 125 p = (void *)(((char *)b->ab_mem) + b->ab_offset); | |
| 126 b->ab_offset += size; | |
| 127 return p; | |
|
36716
71d873054d29
Real arena implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36084
diff
changeset
|
128 } |
|
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
|
129 |
|
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 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
|
131 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
|
132 { |
| 36720 | 133 PyArena* arena = (PyArena *)malloc(sizeof(PyArena)); |
| 134 if (!arena) | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
135 return (PyArena*)PyErr_NoMemory(); |
|
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
|
136 |
| 36720 | 137 arena->a_head = block_new(DEFAULT_BLOCK_SIZE); |
| 138 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
|
139 if (!arena->a_head) { |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
140 free((void *)arena); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
141 return (PyArena*)PyErr_NoMemory(); |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
142 } |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
143 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
|
144 if (!arena->a_objects) { |
|
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
145 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
|
146 free((void *)arena); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
147 return (PyArena*)PyErr_NoMemory(); |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
148 } |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
149 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
150 arena->total_allocs = 0; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
151 arena->total_size = 0; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
152 arena->total_blocks = 1; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
153 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
|
154 arena->total_big_blocks = 0; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
155 #endif |
| 36720 | 156 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
|
157 } |
|
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 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
|
160 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
|
161 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
162 int r; |
| 36720 | 163 assert(arena); |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
164 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
165 /* |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
166 fprintf(stderr, |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
167 "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
|
168 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
|
169 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
|
170 PyList_Size(arena->a_objects)); |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
171 */ |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
172 #endif |
| 36720 | 173 block_free(arena->a_head); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
174 /* This property normally holds, except when the code being compiled |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
175 is sys.getobjects(0), in which case there will be two references. |
|
36724
5f3822c2e731
Use simple PyList to implement list of PyObject pointers
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36720
diff
changeset
|
176 assert(arena->a_objects->ob_refcnt == 1); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
177 */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
178 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
179 /* Clear all the elements from the list. This is necessary |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
180 to guarantee that they will be DECREFed. */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
181 r = PyList_SetSlice(arena->a_objects, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
182 0, PyList_GET_SIZE(arena->a_objects), NULL); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
183 assert(r == 0); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36813
diff
changeset
|
184 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
|
185 Py_DECREF(arena->a_objects); |
| 36720 | 186 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
|
187 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
188 |
|
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 void * |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
190 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
|
191 { |
| 36720 | 192 void *p = block_alloc(arena->a_cur, size); |
| 193 if (!p) | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
194 return PyErr_NoMemory(); |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
195 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
196 arena->total_allocs++; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
197 arena->total_size += size; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
198 #endif |
| 36720 | 199 /* Reset cur if we allocated a new block. */ |
| 200 if (arena->a_cur->ab_next) { | |
| 201 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
|
202 #if defined(Py_DEBUG) |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
203 arena->total_blocks++; |
|
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
204 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
|
205 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
|
206 ++arena->total_big_blocks; |
|
36728
f2a895d33a5c
Add some stats collection in debugging mode.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36724
diff
changeset
|
207 #endif |
| 36720 | 208 } |
| 209 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
|
210 } |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
211 |
|
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 int |
|
36808
b1a3c2238f97
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
36765
diff
changeset
|
213 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
|
214 { |
| 36765 | 215 int r = PyList_Append(arena->a_objects, obj); |
| 216 if (r >= 0) { | |
| 217 Py_DECREF(obj); | |
| 218 } | |
| 219 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
|
220 } |
