aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-23 12:32:00 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-23 12:32:00 -0200
commitd490555ec9d5efb886211f735694d4e7dd0c166d (patch)
treef9b75cdc3a6d4132dddc884280ae218a7f9beb5c
parentad0ec203f60df5e2f8a3b294c9e8c1014280b8f1 (diff)
downloadlua-d490555ec9d5efb886211f735694d4e7dd0c166d.tar.gz
lua-d490555ec9d5efb886211f735694d4e7dd0c166d.tar.bz2
lua-d490555ec9d5efb886211f735694d4e7dd0c166d.zip
garbage collection tag for strings organized in struct TaggedString
-rw-r--r--inout.c4
-rw-r--r--opcode.c14
-rw-r--r--opcode.h8
-rw-r--r--table.c24
-rw-r--r--table.h5
-rw-r--r--tree.c31
-rw-r--r--tree.h23
7 files changed, 56 insertions, 53 deletions
diff --git a/inout.c b/inout.c
index 9cef2653..31859a82 100644
--- a/inout.c
+++ b/inout.c
@@ -5,7 +5,7 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.11 1994/11/14 21:40:14 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.12 1994/11/21 21:41:09 roberto Exp $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <stdlib.h> 11#include <stdlib.h>
@@ -166,7 +166,7 @@ void lua_reportbug (char *s)
166 { 166 {
167 sprintf (strchr(msg,0), 167 sprintf (strchr(msg,0),
168 "\t-> function \"%s\" at file \"%s\":%d\n", 168 "\t-> function \"%s\" at file \"%s\":%d\n",
169 lua_constant[func->function], func->file, line); 169 lua_constant[func->function]->str, func->file, line);
170 line = func->line; 170 line = func->line;
171 func = func->next; 171 func = func->next;
172 lua_popfunction(); 172 lua_popfunction();
diff --git a/opcode.c b/opcode.c
index a8de7f13..144a573e 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.20 1994/11/21 18:22:58 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 3.21 1994/11/22 16:02:53 roberto Exp roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -165,8 +165,8 @@ static int lua_tostring (Object *obj)
165 sprintf (s, "%d", (int) nvalue(obj)); 165 sprintf (s, "%d", (int) nvalue(obj));
166 else 166 else
167 sprintf (s, "%g", nvalue(obj)); 167 sprintf (s, "%g", nvalue(obj));
168 svalue(obj) = lua_createstring(s); 168 tsvalue(obj) = lua_createstring(s);
169 if (svalue(obj) == NULL) 169 if (tsvalue(obj) == NULL)
170 return 1; 170 return 1;
171 tag(obj) = LUA_T_STRING; 171 tag(obj) = LUA_T_STRING;
172 return 0; 172 return 0;
@@ -637,7 +637,7 @@ int lua_pushnumber (real n)
637int lua_pushstring (char *s) 637int lua_pushstring (char *s)
638{ 638{
639 lua_checkstack(top-stack+1); 639 lua_checkstack(top-stack+1);
640 svalue(top) = lua_createstring(s); 640 tsvalue(top) = lua_createstring(s);
641 tag(top) = LUA_T_STRING; 641 tag(top) = LUA_T_STRING;
642 top++; 642 top++;
643 return 0; 643 return 0;
@@ -770,7 +770,7 @@ static int lua_execute (Byte *pc, int base)
770 { 770 {
771 CodeWord code; 771 CodeWord code;
772 get_word(code,pc); 772 get_word(code,pc);
773 tag(top) = LUA_T_STRING; svalue(top++) = lua_constant[code.w]; 773 tag(top) = LUA_T_STRING; tsvalue(top++) = lua_constant[code.w];
774 } 774 }
775 break; 775 break;
776 776
@@ -877,7 +877,7 @@ static int lua_execute (Byte *pc, int base)
877 { 877 {
878 CodeWord code; 878 CodeWord code;
879 get_word(code,pc); 879 get_word(code,pc);
880 tag(top) = LUA_T_STRING; svalue(top) = lua_constant[code.w]; 880 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
881 *(lua_hashdefine (avalue(arr), top)) = *(top-1); 881 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
882 top--; 882 top--;
883 n--; 883 n--;
@@ -996,7 +996,7 @@ static int lua_execute (Byte *pc, int base)
996 do_call(&luaI_fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2); 996 do_call(&luaI_fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2);
997 else 997 else
998 { 998 {
999 svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r))); 999 tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
1000 --top; 1000 --top;
1001 } 1001 }
1002 } 1002 }
diff --git a/opcode.h b/opcode.h
index e26fcce1..f375c7d7 100644
--- a/opcode.h
+++ b/opcode.h
@@ -1,12 +1,13 @@
1/* 1/*
2** TeCGraf - PUC-Rio 2** TeCGraf - PUC-Rio
3** $Id: opcode.h,v 3.7 1994/11/10 17:11:52 roberto Exp roberto $ 3** $Id: opcode.h,v 3.8 1994/11/10 17:36:54 roberto Exp roberto $
4*/ 4*/
5 5
6#ifndef opcode_h 6#ifndef opcode_h
7#define opcode_h 7#define opcode_h
8 8
9#include "lua.h" 9#include "lua.h"
10#include "tree.h"
10 11
11#ifndef STACKGAP 12#ifndef STACKGAP
12#define STACKGAP 128 13#define STACKGAP 128
@@ -109,7 +110,7 @@ typedef union
109{ 110{
110 Cfunction f; 111 Cfunction f;
111 real n; 112 real n;
112 char *s; 113 TaggedString *ts;
113 Byte *b; 114 Byte *b;
114 struct Hash *a; 115 struct Hash *a;
115 void *u; 116 void *u;
@@ -129,7 +130,8 @@ typedef struct
129/* Macros to access structure members */ 130/* Macros to access structure members */
130#define tag(o) ((o)->tag) 131#define tag(o) ((o)->tag)
131#define nvalue(o) ((o)->value.n) 132#define nvalue(o) ((o)->value.n)
132#define svalue(o) ((o)->value.s) 133#define svalue(o) ((o)->value.ts->str)
134#define tsvalue(o) ((o)->value.ts)
133#define bvalue(o) ((o)->value.b) 135#define bvalue(o) ((o)->value.b)
134#define avalue(o) ((o)->value.a) 136#define avalue(o) ((o)->value.a)
135#define fvalue(o) ((o)->value.f) 137#define fvalue(o) ((o)->value.f)
diff --git a/table.c b/table.c
index cac27add..2a3bb213 100644
--- a/table.c
+++ b/table.c
@@ -3,7 +3,7 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 2.21 1994/11/18 19:27:38 roberto Exp roberto $"; 6char *rcs_table="$Id: table.c,v 2.22 1994/11/21 21:41:09 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9 9
@@ -17,13 +17,15 @@ char *rcs_table="$Id: table.c,v 2.21 1994/11/18 19:27:38 roberto Exp roberto $";
17#include "fallback.h" 17#include "fallback.h"
18 18
19 19
20#define MAX_WORD 0xFFFD
21
20#define BUFFER_BLOCK 256 22#define BUFFER_BLOCK 256
21 23
22Symbol *lua_table; 24Symbol *lua_table;
23static Word lua_ntable = 0; 25static Word lua_ntable = 0;
24static Long lua_maxsymbol = 0; 26static Long lua_maxsymbol = 0;
25 27
26char **lua_constant; 28TaggedString **lua_constant;
27static Word lua_nconstant = 0; 29static Word lua_nconstant = 0;
28static Long lua_maxconstant = 0; 30static Long lua_maxconstant = 0;
29 31
@@ -79,7 +81,7 @@ static void lua_initsymbol (void)
79void lua_initconstant (void) 81void lua_initconstant (void)
80{ 82{
81 lua_maxconstant = BUFFER_BLOCK; 83 lua_maxconstant = BUFFER_BLOCK;
82 lua_constant = newvector(lua_maxconstant, char *); 84 lua_constant = newvector(lua_maxconstant, TaggedString *);
83} 85}
84 86
85 87
@@ -92,7 +94,7 @@ int luaI_findsymbol (TreeNode *t)
92{ 94{
93 if (lua_table == NULL) 95 if (lua_table == NULL)
94 lua_initsymbol(); 96 lua_initsymbol();
95 if (t->varindex == UNMARKED_STRING) 97 if (t->varindex == NOT_USED)
96 { 98 {
97 if (lua_ntable == lua_maxsymbol) 99 if (lua_ntable == lua_maxsymbol)
98 { 100 {
@@ -124,17 +126,17 @@ int luaI_findconstant (TreeNode *t)
124{ 126{
125 if (lua_constant == NULL) 127 if (lua_constant == NULL)
126 lua_initconstant(); 128 lua_initconstant();
127 if (t->constindex == UNMARKED_STRING) 129 if (t->constindex == NOT_USED)
128 { 130 {
129 if (lua_nconstant == lua_maxconstant) 131 if (lua_nconstant == lua_maxconstant)
130 { 132 {
131 lua_maxconstant *= 2; 133 lua_maxconstant *= 2;
132 if (lua_maxconstant > MAX_WORD) 134 if (lua_maxconstant > MAX_WORD)
133 lua_error("constant table overflow"); 135 lua_error("constant table overflow");
134 lua_constant = growvector(lua_constant, lua_maxconstant, char*); 136 lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
135 } 137 }
136 t->constindex = lua_nconstant; 138 t->constindex = lua_nconstant;
137 lua_constant[lua_nconstant] = t->str; 139 lua_constant[lua_nconstant] = &(t->ts);
138 lua_nconstant++; 140 lua_nconstant++;
139 } 141 }
140 return t->constindex; 142 return t->constindex;
@@ -157,10 +159,10 @@ void lua_travsymbol (void (*fn)(Object *))
157*/ 159*/
158void lua_markobject (Object *o) 160void lua_markobject (Object *o)
159{ 161{
160 if (tag(o) == LUA_T_STRING && indexstring(svalue(o)) == UNMARKED_STRING) 162 if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
161 indexstring(svalue(o)) = MARKED_STRING; 163 tsvalue(o)->marked = 1;
162 else if (tag(o) == LUA_T_ARRAY) 164 else if (tag(o) == LUA_T_ARRAY)
163 lua_hashmark (avalue(o)); 165 lua_hashmark (avalue(o));
164} 166}
165 167
166 168
@@ -247,7 +249,7 @@ static void lua_nextvar (void)
247 { 249 {
248 Object name; 250 Object name;
249 tag(&name) = LUA_T_STRING; 251 tag(&name) = LUA_T_STRING;
250 svalue(&name) = next->str; 252 tsvalue(&name) = &(next->ts);
251 luaI_pushobject(&name); 253 luaI_pushobject(&name);
252 luaI_pushobject(&s_object(next->varindex)); 254 luaI_pushobject(&s_object(next->varindex));
253 } 255 }
diff --git a/table.h b/table.h
index 0c2f0c0a..d65751f8 100644
--- a/table.h
+++ b/table.h
@@ -1,16 +1,17 @@
1/* 1/*
2** Module to control static tables 2** Module to control static tables
3** TeCGraf - PUC-Rio 3** TeCGraf - PUC-Rio
4** $Id: table.h,v 2.7 1994/11/17 13:58:57 roberto Exp roberto $ 4** $Id: table.h,v 2.8 1994/11/18 19:27:38 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef table_h 7#ifndef table_h
8#define table_h 8#define table_h
9 9
10#include "tree.h" 10#include "tree.h"
11#include "opcode.h"
11 12
12extern Symbol *lua_table; 13extern Symbol *lua_table;
13extern char **lua_constant; 14extern TaggedString **lua_constant;
14 15
15extern char *lua_file[]; 16extern char *lua_file[];
16extern int lua_nfile; 17extern int lua_nfile;
diff --git a/tree.c b/tree.c
index 0f3e021c..90fa64ab 100644
--- a/tree.c
+++ b/tree.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_tree="$Id: tree.c,v 1.8 1994/11/17 13:58:57 roberto Exp roberto $"; 6char *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
20typedef struct StringNode { 20typedef 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
26static StringNode *string_root = NULL; 25static StringNode *string_root = NULL;
27 26
28
29static TreeNode *constant_root = NULL; 27static 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
56char *lua_createstring (char *str) 55TaggedString *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
diff --git a/tree.h b/tree.h
index e8accc7e..14bf9959 100644
--- a/tree.h
+++ b/tree.h
@@ -1,34 +1,33 @@
1/* 1/*
2** tree.h 2** tree.h
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4** $Id: tree.h,v 1.4 1994/11/17 13:58:57 roberto Exp roberto $ 4** $Id: tree.h,v 1.5 1994/11/18 19:27:38 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef tree_h 7#ifndef tree_h
8#define tree_h 8#define tree_h
9 9
10#include "opcode.h"
11 10
11#define NOT_USED 0xFFFE
12 12
13#define UNMARKED_STRING 0xFFFF
14#define MARKED_STRING 0xFFFE
15#define MAX_WORD 0xFFFD
16 13
14typedef struct TaggedString
15{
16 char marked; /* for garbage collection */
17 char str[1]; /* \0 byte already reserved */
18} TaggedString;
17 19
18typedef struct TreeNode 20typedef struct TreeNode
19{ 21{
20 struct TreeNode *right; 22 struct TreeNode *right;
21 struct TreeNode *left; 23 struct TreeNode *left;
22 Word varindex; /* if this is a symbol */ 24 unsigned short varindex; /* != NOT_USED if this is a symbol */
23 Word constindex; /* if this is a constant; also used for garbage collection */ 25 unsigned short constindex; /* != NOT_USED if this is a constant */
24 char str[1]; /* \0 byte already reserved */ 26 TaggedString ts;
25} TreeNode; 27} TreeNode;
26 28
27 29
28#define indexstring(s) (*(((Word *)s)-1)) 30TaggedString *lua_createstring (char *str);
29
30
31char *lua_createstring (char *str);
32TreeNode *lua_constcreate (char *str); 31TreeNode *lua_constcreate (char *str);
33int lua_strcollector (void); 32int lua_strcollector (void);
34TreeNode *lua_varnext (char *n); 33TreeNode *lua_varnext (char *n);