Mercurial > cpython-lru_cache-weakref
annotate Parser/node.c @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 1c964ed35ef9 |
| children | fd25030223b0 |
| rev | line source |
|---|---|
|
14391
1fd64b3af760
Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents:
14375
diff
changeset
|
1 /* Parse tree node implementation */ |
|
1fd64b3af760
Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents:
14375
diff
changeset
|
2 |
|
14871
fb6b51033c3b
More limits.h stuff in node.c.
Tim Peters <tim.peters@gmail.com>
parents:
14391
diff
changeset
|
3 #include "Python.h" |
|
14391
1fd64b3af760
Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents:
14375
diff
changeset
|
4 #include "node.h" |
|
1fd64b3af760
Move down the INT_MAX logic, because HAVE_LIMITS_H was always undefined
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents:
14375
diff
changeset
|
5 #include "errcode.h" |
| 145 | 6 |
| 6 | 7 node * |
| 13659 | 8 PyNode_New(int type) |
| 6 | 9 { |
|
24529
3444005b6527
SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
24241
diff
changeset
|
10 node *n = (node *) PyObject_MALLOC(1 * sizeof(node)); |
| 6 | 11 if (n == NULL) |
| 12 return NULL; | |
| 13 n->n_type = type; | |
| 14 n->n_str = NULL; | |
| 86 | 15 n->n_lineno = 0; |
| 6 | 16 n->n_nchildren = 0; |
| 17 n->n_child = NULL; | |
| 18 return n; | |
| 19 } | |
| 20 | |
|
24141
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
21 /* See comments at XXXROUNDUP below. Returns -1 on overflow. */ |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
22 static int |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
23 fancy_roundup(int n) |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
24 { |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
25 /* Round up to the closest power of 2 >= n. */ |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
26 int result = 256; |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
27 assert(n > 128); |
|
24141
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
28 while (result < n) { |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
29 result <<= 1; |
|
24141
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
30 if (result <= 0) |
|
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
31 return -1; |
|
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
32 } |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
33 return result; |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
34 } |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
35 |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
36 /* A gimmick to make massive numbers of reallocs quicker. The result is |
|
24241
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
37 * a number >= the input. In PyNode_AddChild, it's used like so, when |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
38 * we're about to add child number current_size + 1: |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
39 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
40 * if XXXROUNDUP(current_size) < XXXROUNDUP(current_size + 1): |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
41 * allocate space for XXXROUNDUP(current_size + 1) total children |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
42 * else: |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
43 * we already have enough space |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
44 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
45 * Since a node starts out empty, we must have |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
46 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
47 * XXXROUNDUP(0) < XXXROUNDUP(1) |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
48 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
49 * so that we allocate space for the first child. One-child nodes are very |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
50 * common (presumably that would change if we used a more abstract form |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
51 * of syntax tree), so to avoid wasting memory it's desirable that |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
52 * XXXROUNDUP(1) == 1. That in turn forces XXXROUNDUP(0) == 0. |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
53 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
54 * Else for 2 <= n <= 128, we round up to the closest multiple of 4. Why 4? |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
55 * Rounding up to a multiple of an exact power of 2 is very efficient, and |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
56 * most nodes with more than one child have <= 4 kids. |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
57 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
58 * Else we call fancy_roundup() to grow proportionately to n. We've got an |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
59 * extreme case then (like test_longexp.py), and on many platforms doing |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
60 * anything less than proportional growth leads to exorbitant runtime |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
61 * (e.g., MacPython), or extreme fragmentation of user address space (e.g., |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
62 * Win98). |
|
24241
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
63 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
64 * In a run of compileall across the 2.3a0 Lib directory, Andrew MacIntyre |
|
37557
6ae4adefc573
SF patch #1467512, fix double free with triple quoted string in standard build.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36786
diff
changeset
|
65 * reported that, with this scheme, 89% of PyObject_REALLOC calls in |
|
24241
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
66 * PyNode_AddChild passed 1 for the size, and 9% passed 4. So this usually |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
67 * wastes very little memory, but is very effective at sidestepping |
| 38631 | 68 * platform-realloc disasters on vulnerable platforms. |
|
24241
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
69 * |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
70 * Note that this would be straightforward if a node stored its current |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
71 * capacity. The code is tricky to avoid that. |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
72 */ |
|
24241
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
73 #define XXXROUNDUP(n) ((n) <= 1 ? (n) : \ |
|
fc27ffe2036a
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
Tim Peters <tim.peters@gmail.com>
parents:
24141
diff
changeset
|
74 (n) <= 128 ? (((n) + 3) & ~3) : \ |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
75 fancy_roundup(n)) |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
76 |
| 6 | 77 |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
78 int |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
24529
diff
changeset
|
79 PyNode_AddChild(register node *n1, int type, char *str, int lineno, int col_offset) |
| 6 | 80 { |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
81 const int nch = n1->n_nchildren; |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
82 int current_capacity; |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
83 int required_capacity; |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
84 node *n; |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
85 |
|
14147
21ec69a56228
Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents:
13659
diff
changeset
|
86 if (nch == INT_MAX || nch < 0) |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
87 return E_OVERFLOW; |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
88 |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
89 current_capacity = XXXROUNDUP(nch); |
|
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
90 required_capacity = XXXROUNDUP(nch + 1); |
|
24141
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
91 if (current_capacity < 0 || required_capacity < 0) |
|
40bda3e090f6
PyNode_AddChild() and fancy_roundup(): Be paranoid about int overflow.
Tim Peters <tim.peters@gmail.com>
parents:
24128
diff
changeset
|
92 return E_OVERFLOW; |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
93 if (current_capacity < required_capacity) { |
|
47587
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
38631
diff
changeset
|
94 if (required_capacity > PY_SIZE_MAX / sizeof(node)) { |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
38631
diff
changeset
|
95 return E_NOMEM; |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
38631
diff
changeset
|
96 } |
| 6 | 97 n = n1->n_child; |
|
24529
3444005b6527
SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
24241
diff
changeset
|
98 n = (node *) PyObject_REALLOC(n, |
|
3444005b6527
SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
24241
diff
changeset
|
99 required_capacity * sizeof(node)); |
| 6 | 100 if (n == NULL) |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
101 return E_NOMEM; |
| 6 | 102 n1->n_child = n; |
| 103 } | |
|
24128
76e9a14fb7c5
PyNode_AddChild(): Do aggressive over-allocation when the number of
Tim Peters <tim.peters@gmail.com>
parents:
14871
diff
changeset
|
104 |
| 6 | 105 n = &n1->n_child[n1->n_nchildren++]; |
| 106 n->n_type = type; | |
| 107 n->n_str = str; | |
| 86 | 108 n->n_lineno = lineno; |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
24529
diff
changeset
|
109 n->n_col_offset = col_offset; |
| 6 | 110 n->n_nchildren = 0; |
| 111 n->n_child = NULL; | |
|
12970
63436719d12e
Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
5379
diff
changeset
|
112 return 0; |
| 6 | 113 } |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
114 |
| 86 | 115 /* Forward */ |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
116 static void freechildren(node *); |
| 86 | 117 |
| 118 | |
| 119 void | |
| 13659 | 120 PyNode_Free(node *n) |
| 86 | 121 { |
| 122 if (n != NULL) { | |
| 123 freechildren(n); | |
|
24529
3444005b6527
SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
24241
diff
changeset
|
124 PyObject_FREE(n); |
| 86 | 125 } |
| 126 } | |
| 127 | |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
128 static void |
| 13659 | 129 freechildren(node *n) |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
130 { |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
131 int i; |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
132 for (i = NCH(n); --i >= 0; ) |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
133 freechildren(CHILD(n, i)); |
|
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
134 if (n->n_child != NULL) |
|
24529
3444005b6527
SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
24241
diff
changeset
|
135 PyObject_FREE(n->n_child); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
136 if (STR(n) != NULL) |
|
24529
3444005b6527
SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
24241
diff
changeset
|
137 PyObject_FREE(STR(n)); |
|
76
ede86799705b
Add function to free an entire parse tree.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
138 } |
