Vladimir Marangozov | 58e64a8 | 2000-09-03 23:47:08 +0000 | [diff] [blame] | 1 | /* Parse tree node implementation */ |
| 2 | |
Tim Peters | 1d6a729 | 2000-09-26 06:11:54 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Vladimir Marangozov | 58e64a8 | 2000-09-03 23:47:08 +0000 | [diff] [blame] | 4 | #include "node.h" |
| 5 | #include "errcode.h" |
| 6 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | node * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 8 | PyNode_New(int type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 10 | node *n = PyMem_NEW(node, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | if (n == NULL) |
| 12 | return NULL; |
| 13 | n->n_type = type; |
| 14 | n->n_str = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | n->n_lineno = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 16 | n->n_nchildren = 0; |
| 17 | n->n_child = NULL; |
| 18 | return n; |
| 19 | } |
| 20 | |
Tim Peters | 623fdb9 | 2002-07-08 19:11:07 +0000 | [diff] [blame] | 21 | /* See comments at XXXROUNDUP below. Returns -1 on overflow. */ |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 22 | static int |
| 23 | fancy_roundup(int n) |
| 24 | { |
| 25 | /* Round up to the closest power of 2 >= n. */ |
| 26 | int result = 256; |
| 27 | assert(n > 128); |
Tim Peters | 623fdb9 | 2002-07-08 19:11:07 +0000 | [diff] [blame] | 28 | while (result < n) { |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 29 | result <<= 1; |
Tim Peters | 623fdb9 | 2002-07-08 19:11:07 +0000 | [diff] [blame] | 30 | if (result <= 0) |
| 31 | return -1; |
| 32 | } |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 33 | return result; |
| 34 | } |
| 35 | |
| 36 | /* A gimmick to make massive numbers of reallocs quicker. The result is |
Tim Peters | e561dc2 | 2002-07-15 17:58:03 +0000 | [diff] [blame] | 37 | * a number >= the input. In PyNode_AddChild, it's used like so, when |
| 38 | * we're about to add child number current_size + 1: |
| 39 | * |
| 40 | * if XXXROUNDUP(current_size) < XXXROUNDUP(current_size + 1): |
| 41 | * allocate space for XXXROUNDUP(current_size + 1) total children |
| 42 | * else: |
| 43 | * we already have enough space |
| 44 | * |
| 45 | * Since a node starts out empty, we must have |
| 46 | * |
| 47 | * XXXROUNDUP(0) < XXXROUNDUP(1) |
| 48 | * |
| 49 | * so that we allocate space for the first child. One-child nodes are very |
| 50 | * common (presumably that would change if we used a more abstract form |
| 51 | * of syntax tree), so to avoid wasting memory it's desirable that |
| 52 | * XXXROUNDUP(1) == 1. That in turn forces XXXROUNDUP(0) == 0. |
| 53 | * |
| 54 | * Else for 2 <= n <= 128, we round up to the closest multiple of 4. Why 4? |
| 55 | * Rounding up to a multiple of an exact power of 2 is very efficient, and |
| 56 | * most nodes with more than one child have <= 4 kids. |
| 57 | * |
| 58 | * Else we call fancy_roundup() to grow proportionately to n. We've got an |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 59 | * extreme case then (like test_longexp.py), and on many platforms doing |
| 60 | * anything less than proportional growth leads to exorbitant runtime |
| 61 | * (e.g., MacPython), or extreme fragmentation of user address space (e.g., |
| 62 | * Win98). |
Tim Peters | e561dc2 | 2002-07-15 17:58:03 +0000 | [diff] [blame] | 63 | * |
| 64 | * In a run of compileall across the 2.3a0 Lib directory, Andrew MacIntyre |
| 65 | * reported that, with this scheme, 89% of PyMem_RESIZE calls in |
| 66 | * PyNode_AddChild passed 1 for the size, and 9% passed 4. So this usually |
| 67 | * wastes very little memory, but is very effective at sidestepping |
| 68 | * platform-realloc disasters on vulnernable platforms. |
| 69 | * |
| 70 | * Note that this would be straightforward if a node stored its current |
| 71 | * capacity. The code is tricky to avoid that. |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 72 | */ |
Tim Peters | e561dc2 | 2002-07-15 17:58:03 +0000 | [diff] [blame] | 73 | #define XXXROUNDUP(n) ((n) <= 1 ? (n) : \ |
| 74 | (n) <= 128 ? (((n) + 3) & ~3) : \ |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 75 | fancy_roundup(n)) |
| 76 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 78 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 79 | PyNode_AddChild(register node *n1, int type, char *str, int lineno) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | { |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 81 | const int nch = n1->n_nchildren; |
| 82 | int current_capacity; |
| 83 | int required_capacity; |
| 84 | node *n; |
| 85 | |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 86 | if (nch == INT_MAX || nch < 0) |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 87 | return E_OVERFLOW; |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 88 | |
| 89 | current_capacity = XXXROUNDUP(nch); |
| 90 | required_capacity = XXXROUNDUP(nch + 1); |
Tim Peters | 623fdb9 | 2002-07-08 19:11:07 +0000 | [diff] [blame] | 91 | if (current_capacity < 0 || required_capacity < 0) |
| 92 | return E_OVERFLOW; |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 93 | if (current_capacity < required_capacity) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | n = n1->n_child; |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 95 | PyMem_RESIZE(n, node, required_capacity); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | if (n == NULL) |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 97 | return E_NOMEM; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | n1->n_child = n; |
| 99 | } |
Tim Peters | 755ebea | 2002-07-08 06:32:09 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | n = &n1->n_child[n1->n_nchildren++]; |
| 102 | n->n_type = type; |
| 103 | n->n_str = str; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 104 | n->n_lineno = lineno; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | n->n_nchildren = 0; |
| 106 | n->n_child = NULL; |
Jeremy Hylton | 9498806 | 2000-06-20 19:10:44 +0000 | [diff] [blame] | 107 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | } |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 110 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 111 | static void freechildren(node *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 115 | PyNode_Free(node *n) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 116 | { |
| 117 | if (n != NULL) { |
| 118 | freechildren(n); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 119 | PyMem_DEL(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 123 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 124 | freechildren(node *n) |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 125 | { |
| 126 | int i; |
| 127 | for (i = NCH(n); --i >= 0; ) |
| 128 | freechildren(CHILD(n, i)); |
| 129 | if (n->n_child != NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 130 | PyMem_DEL(n->n_child); |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 131 | if (STR(n) != NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 132 | PyMem_DEL(STR(n)); |
Guido van Rossum | 03a24cd | 1990-11-18 17:37:06 +0000 | [diff] [blame] | 133 | } |