diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-23 12:32:00 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-11-23 12:32:00 -0200 |
commit | d490555ec9d5efb886211f735694d4e7dd0c166d (patch) | |
tree | f9b75cdc3a6d4132dddc884280ae218a7f9beb5c /tree.c | |
parent | ad0ec203f60df5e2f8a3b294c9e8c1014280b8f1 (diff) | |
download | lua-d490555ec9d5efb886211f735694d4e7dd0c166d.tar.gz lua-d490555ec9d5efb886211f735694d4e7dd0c166d.tar.bz2 lua-d490555ec9d5efb886211f735694d4e7dd0c166d.zip |
garbage collection tag for strings organized in struct TaggedString
Diffstat (limited to 'tree.c')
-rw-r--r-- | tree.c | 31 |
1 files changed, 15 insertions, 16 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_tree="$Id: tree.c,v 1.8 1994/11/17 13:58:57 roberto Exp roberto $"; | 6 | char *rcs_tree="$Id: tree.c,v 1.9 1994/11/18 19:27:38 roberto Exp roberto $"; |
7 | 7 | ||
8 | 8 | ||
9 | #include <string.h> | 9 | #include <string.h> |
@@ -19,13 +19,11 @@ char *rcs_tree="$Id: tree.c,v 1.8 1994/11/17 13:58:57 roberto Exp roberto $"; | |||
19 | 19 | ||
20 | typedef struct StringNode { | 20 | typedef struct StringNode { |
21 | struct StringNode *next; | 21 | struct StringNode *next; |
22 | Word mark; | 22 | TaggedString ts; |
23 | char str[1]; | ||
24 | } StringNode; | 23 | } StringNode; |
25 | 24 | ||
26 | static StringNode *string_root = NULL; | 25 | static StringNode *string_root = NULL; |
27 | 26 | ||
28 | |||
29 | static TreeNode *constant_root = NULL; | 27 | static TreeNode *constant_root = NULL; |
30 | 28 | ||
31 | /* | 29 | /* |
@@ -37,13 +35,14 @@ static TreeNode *tree_create (TreeNode **node, char *str) | |||
37 | { | 35 | { |
38 | *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str)); | 36 | *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str)); |
39 | (*node)->left = (*node)->right = NULL; | 37 | (*node)->left = (*node)->right = NULL; |
40 | strcpy((*node)->str, str); | 38 | strcpy((*node)->ts.str, str); |
41 | (*node)->varindex = (*node)->constindex = UNMARKED_STRING; | 39 | (*node)->ts.marked = 0; |
40 | (*node)->varindex = (*node)->constindex = NOT_USED; | ||
42 | return *node; | 41 | return *node; |
43 | } | 42 | } |
44 | else | 43 | else |
45 | { | 44 | { |
46 | int c = lua_strcmp(str, (*node)->str); | 45 | int c = lua_strcmp(str, (*node)->ts.str); |
47 | if (c < 0) | 46 | if (c < 0) |
48 | return tree_create(&(*node)->left, str); | 47 | return tree_create(&(*node)->left, str); |
49 | else if (c > 0) | 48 | else if (c > 0) |
@@ -53,17 +52,17 @@ static TreeNode *tree_create (TreeNode **node, char *str) | |||
53 | } | 52 | } |
54 | } | 53 | } |
55 | 54 | ||
56 | char *lua_createstring (char *str) | 55 | TaggedString *lua_createstring (char *str) |
57 | { | 56 | { |
58 | StringNode *newString; | 57 | StringNode *newString; |
59 | if (str == NULL) return NULL; | 58 | if (str == NULL) return NULL; |
60 | lua_pack(); | 59 | lua_pack(); |
61 | newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str)); | 60 | newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str)); |
62 | newString->mark = UNMARKED_STRING; | 61 | newString->ts.marked = 0; |
63 | strcpy(newString->str, str); | 62 | strcpy(newString->ts.str, str); |
64 | newString->next = string_root; | 63 | newString->next = string_root; |
65 | string_root = newString; | 64 | string_root = newString; |
66 | return newString->str; | 65 | return &(newString->ts); |
67 | } | 66 | } |
68 | 67 | ||
69 | 68 | ||
@@ -84,7 +83,7 @@ int lua_strcollector (void) | |||
84 | while (curr) | 83 | while (curr) |
85 | { | 84 | { |
86 | StringNode *next = curr->next; | 85 | StringNode *next = curr->next; |
87 | if (curr->mark == UNMARKED_STRING) | 86 | if (!curr->ts.marked) |
88 | { | 87 | { |
89 | if (prev == NULL) string_root = next; | 88 | if (prev == NULL) string_root = next; |
90 | else prev->next = next; | 89 | else prev->next = next; |
@@ -93,7 +92,7 @@ int lua_strcollector (void) | |||
93 | } | 92 | } |
94 | else | 93 | else |
95 | { | 94 | { |
96 | curr->mark = UNMARKED_STRING; | 95 | curr->ts.marked = 0; |
97 | prev = curr; | 96 | prev = curr; |
98 | } | 97 | } |
99 | curr = next; | 98 | curr = next; |
@@ -110,7 +109,7 @@ static TreeNode *tree_next (TreeNode *node, char *str) | |||
110 | else if (str == NULL) return node; | 109 | else if (str == NULL) return node; |
111 | else | 110 | else |
112 | { | 111 | { |
113 | int c = lua_strcmp(str, node->str); | 112 | int c = lua_strcmp(str, node->ts.str); |
114 | if (c == 0) | 113 | if (c == 0) |
115 | return node->left != NULL ? node->left : node->right; | 114 | return node->left != NULL ? node->left : node->right; |
116 | else if (c < 0) | 115 | else if (c < 0) |
@@ -131,10 +130,10 @@ TreeNode *lua_varnext (char *n) | |||
131 | { /* repeat until a valid (non nil) variable */ | 130 | { /* repeat until a valid (non nil) variable */ |
132 | result = tree_next(constant_root, name); | 131 | result = tree_next(constant_root, name); |
133 | if (result == NULL) return NULL; | 132 | if (result == NULL) return NULL; |
134 | if (result->varindex != UNMARKED_STRING && | 133 | if (result->varindex != NOT_USED && |
135 | s_tag(result->varindex) != LUA_T_NIL) | 134 | s_tag(result->varindex) != LUA_T_NIL) |
136 | return result; | 135 | return result; |
137 | name = result->str; | 136 | name = result->ts.str; |
138 | } | 137 | } |
139 | } | 138 | } |
140 | 139 | ||