garbage collection tag for strings organized in struct TaggedString

This commit is contained in:
Roberto Ierusalimschy
1994-11-23 12:32:00 -02:00
parent ad0ec203f6
commit d490555ec9
7 changed files with 57 additions and 54 deletions

View File

@@ -5,7 +5,7 @@
** Also provides some predefined lua functions. ** Also provides some predefined lua functions.
*/ */
char *rcs_inout="$Id: inout.c,v 2.11 1994/11/14 21:40:14 roberto Exp roberto $"; char *rcs_inout="$Id: inout.c,v 2.12 1994/11/21 21:41:09 roberto Exp $";
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -166,7 +166,7 @@ void lua_reportbug (char *s)
{ {
sprintf (strchr(msg,0), sprintf (strchr(msg,0),
"\t-> function \"%s\" at file \"%s\":%d\n", "\t-> function \"%s\" at file \"%s\":%d\n",
lua_constant[func->function], func->file, line); lua_constant[func->function]->str, func->file, line);
line = func->line; line = func->line;
func = func->next; func = func->next;
lua_popfunction(); lua_popfunction();

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 3.20 1994/11/21 18:22:58 roberto Exp roberto $"; char *rcs_opcode="$Id: opcode.c,v 3.21 1994/11/22 16:02:53 roberto Exp roberto $";
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h> #include <stdio.h>
@@ -165,8 +165,8 @@ static int lua_tostring (Object *obj)
sprintf (s, "%d", (int) nvalue(obj)); sprintf (s, "%d", (int) nvalue(obj));
else else
sprintf (s, "%g", nvalue(obj)); sprintf (s, "%g", nvalue(obj));
svalue(obj) = lua_createstring(s); tsvalue(obj) = lua_createstring(s);
if (svalue(obj) == NULL) if (tsvalue(obj) == NULL)
return 1; return 1;
tag(obj) = LUA_T_STRING; tag(obj) = LUA_T_STRING;
return 0; return 0;
@@ -637,7 +637,7 @@ int lua_pushnumber (real n)
int lua_pushstring (char *s) int lua_pushstring (char *s)
{ {
lua_checkstack(top-stack+1); lua_checkstack(top-stack+1);
svalue(top) = lua_createstring(s); tsvalue(top) = lua_createstring(s);
tag(top) = LUA_T_STRING; tag(top) = LUA_T_STRING;
top++; top++;
return 0; return 0;
@@ -770,7 +770,7 @@ static int lua_execute (Byte *pc, int base)
{ {
CodeWord code; CodeWord code;
get_word(code,pc); get_word(code,pc);
tag(top) = LUA_T_STRING; svalue(top++) = lua_constant[code.w]; tag(top) = LUA_T_STRING; tsvalue(top++) = lua_constant[code.w];
} }
break; break;
@@ -877,7 +877,7 @@ static int lua_execute (Byte *pc, int base)
{ {
CodeWord code; CodeWord code;
get_word(code,pc); get_word(code,pc);
tag(top) = LUA_T_STRING; svalue(top) = lua_constant[code.w]; tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
*(lua_hashdefine (avalue(arr), top)) = *(top-1); *(lua_hashdefine (avalue(arr), top)) = *(top-1);
top--; top--;
n--; n--;
@@ -996,7 +996,7 @@ static int lua_execute (Byte *pc, int base)
do_call(&luaI_fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2); do_call(&luaI_fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2);
else else
{ {
svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r))); tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
--top; --top;
} }
} }

View File

@@ -1,12 +1,13 @@
/* /*
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: opcode.h,v 3.7 1994/11/10 17:11:52 roberto Exp roberto $ ** $Id: opcode.h,v 3.8 1994/11/10 17:36:54 roberto Exp roberto $
*/ */
#ifndef opcode_h #ifndef opcode_h
#define opcode_h #define opcode_h
#include "lua.h" #include "lua.h"
#include "tree.h"
#ifndef STACKGAP #ifndef STACKGAP
#define STACKGAP 128 #define STACKGAP 128
@@ -109,7 +110,7 @@ typedef union
{ {
Cfunction f; Cfunction f;
real n; real n;
char *s; TaggedString *ts;
Byte *b; Byte *b;
struct Hash *a; struct Hash *a;
void *u; void *u;
@@ -129,7 +130,8 @@ typedef struct
/* Macros to access structure members */ /* Macros to access structure members */
#define tag(o) ((o)->tag) #define tag(o) ((o)->tag)
#define nvalue(o) ((o)->value.n) #define nvalue(o) ((o)->value.n)
#define svalue(o) ((o)->value.s) #define svalue(o) ((o)->value.ts->str)
#define tsvalue(o) ((o)->value.ts)
#define bvalue(o) ((o)->value.b) #define bvalue(o) ((o)->value.b)
#define avalue(o) ((o)->value.a) #define avalue(o) ((o)->value.a)
#define fvalue(o) ((o)->value.f) #define fvalue(o) ((o)->value.f)

24
table.c
View File

@@ -3,7 +3,7 @@
** Module to control static tables ** Module to control static tables
*/ */
char *rcs_table="$Id: table.c,v 2.21 1994/11/18 19:27:38 roberto Exp roberto $"; char *rcs_table="$Id: table.c,v 2.22 1994/11/21 21:41:09 roberto Exp roberto $";
#include <string.h> #include <string.h>
@@ -17,13 +17,15 @@ char *rcs_table="$Id: table.c,v 2.21 1994/11/18 19:27:38 roberto Exp roberto $";
#include "fallback.h" #include "fallback.h"
#define MAX_WORD 0xFFFD
#define BUFFER_BLOCK 256 #define BUFFER_BLOCK 256
Symbol *lua_table; Symbol *lua_table;
static Word lua_ntable = 0; static Word lua_ntable = 0;
static Long lua_maxsymbol = 0; static Long lua_maxsymbol = 0;
char **lua_constant; TaggedString **lua_constant;
static Word lua_nconstant = 0; static Word lua_nconstant = 0;
static Long lua_maxconstant = 0; static Long lua_maxconstant = 0;
@@ -79,7 +81,7 @@ static void lua_initsymbol (void)
void lua_initconstant (void) void lua_initconstant (void)
{ {
lua_maxconstant = BUFFER_BLOCK; lua_maxconstant = BUFFER_BLOCK;
lua_constant = newvector(lua_maxconstant, char *); lua_constant = newvector(lua_maxconstant, TaggedString *);
} }
@@ -92,7 +94,7 @@ int luaI_findsymbol (TreeNode *t)
{ {
if (lua_table == NULL) if (lua_table == NULL)
lua_initsymbol(); lua_initsymbol();
if (t->varindex == UNMARKED_STRING) if (t->varindex == NOT_USED)
{ {
if (lua_ntable == lua_maxsymbol) if (lua_ntable == lua_maxsymbol)
{ {
@@ -124,17 +126,17 @@ int luaI_findconstant (TreeNode *t)
{ {
if (lua_constant == NULL) if (lua_constant == NULL)
lua_initconstant(); lua_initconstant();
if (t->constindex == UNMARKED_STRING) if (t->constindex == NOT_USED)
{ {
if (lua_nconstant == lua_maxconstant) if (lua_nconstant == lua_maxconstant)
{ {
lua_maxconstant *= 2; lua_maxconstant *= 2;
if (lua_maxconstant > MAX_WORD) if (lua_maxconstant > MAX_WORD)
lua_error("constant table overflow"); lua_error("constant table overflow");
lua_constant = growvector(lua_constant, lua_maxconstant, char*); lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
} }
t->constindex = lua_nconstant; t->constindex = lua_nconstant;
lua_constant[lua_nconstant] = t->str; lua_constant[lua_nconstant] = &(t->ts);
lua_nconstant++; lua_nconstant++;
} }
return t->constindex; return t->constindex;
@@ -157,10 +159,10 @@ void lua_travsymbol (void (*fn)(Object *))
*/ */
void lua_markobject (Object *o) void lua_markobject (Object *o)
{ {
if (tag(o) == LUA_T_STRING && indexstring(svalue(o)) == UNMARKED_STRING) if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
indexstring(svalue(o)) = MARKED_STRING; tsvalue(o)->marked = 1;
else if (tag(o) == LUA_T_ARRAY) else if (tag(o) == LUA_T_ARRAY)
lua_hashmark (avalue(o)); lua_hashmark (avalue(o));
} }
@@ -247,7 +249,7 @@ static void lua_nextvar (void)
{ {
Object name; Object name;
tag(&name) = LUA_T_STRING; tag(&name) = LUA_T_STRING;
svalue(&name) = next->str; tsvalue(&name) = &(next->ts);
luaI_pushobject(&name); luaI_pushobject(&name);
luaI_pushobject(&s_object(next->varindex)); luaI_pushobject(&s_object(next->varindex));
} }

View File

@@ -1,16 +1,17 @@
/* /*
** Module to control static tables ** Module to control static tables
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: table.h,v 2.7 1994/11/17 13:58:57 roberto Exp roberto $ ** $Id: table.h,v 2.8 1994/11/18 19:27:38 roberto Exp roberto $
*/ */
#ifndef table_h #ifndef table_h
#define table_h #define table_h
#include "tree.h" #include "tree.h"
#include "opcode.h"
extern Symbol *lua_table; extern Symbol *lua_table;
extern char **lua_constant; extern TaggedString **lua_constant;
extern char *lua_file[]; extern char *lua_file[];
extern int lua_nfile; extern int lua_nfile;

31
tree.c
View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_tree="$Id: tree.c,v 1.8 1994/11/17 13:58:57 roberto Exp roberto $"; char *rcs_tree="$Id: tree.c,v 1.9 1994/11/18 19:27:38 roberto Exp roberto $";
#include <string.h> #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 $";
typedef struct StringNode { typedef struct StringNode {
struct StringNode *next; struct StringNode *next;
Word mark; TaggedString ts;
char str[1];
} StringNode; } StringNode;
static StringNode *string_root = NULL; static StringNode *string_root = NULL;
static TreeNode *constant_root = NULL; static TreeNode *constant_root = NULL;
/* /*
@@ -37,13 +35,14 @@ static TreeNode *tree_create (TreeNode **node, char *str)
{ {
*node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str)); *node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
(*node)->left = (*node)->right = NULL; (*node)->left = (*node)->right = NULL;
strcpy((*node)->str, str); strcpy((*node)->ts.str, str);
(*node)->varindex = (*node)->constindex = UNMARKED_STRING; (*node)->ts.marked = 0;
(*node)->varindex = (*node)->constindex = NOT_USED;
return *node; return *node;
} }
else else
{ {
int c = lua_strcmp(str, (*node)->str); int c = lua_strcmp(str, (*node)->ts.str);
if (c < 0) if (c < 0)
return tree_create(&(*node)->left, str); return tree_create(&(*node)->left, str);
else if (c > 0) else if (c > 0)
@@ -53,17 +52,17 @@ static TreeNode *tree_create (TreeNode **node, char *str)
} }
} }
char *lua_createstring (char *str) TaggedString *lua_createstring (char *str)
{ {
StringNode *newString; StringNode *newString;
if (str == NULL) return NULL; if (str == NULL) return NULL;
lua_pack(); lua_pack();
newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str)); newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
newString->mark = UNMARKED_STRING; newString->ts.marked = 0;
strcpy(newString->str, str); strcpy(newString->ts.str, str);
newString->next = string_root; newString->next = string_root;
string_root = newString; string_root = newString;
return newString->str; return &(newString->ts);
} }
@@ -84,7 +83,7 @@ int lua_strcollector (void)
while (curr) while (curr)
{ {
StringNode *next = curr->next; StringNode *next = curr->next;
if (curr->mark == UNMARKED_STRING) if (!curr->ts.marked)
{ {
if (prev == NULL) string_root = next; if (prev == NULL) string_root = next;
else prev->next = next; else prev->next = next;
@@ -93,7 +92,7 @@ int lua_strcollector (void)
} }
else else
{ {
curr->mark = UNMARKED_STRING; curr->ts.marked = 0;
prev = curr; prev = curr;
} }
curr = next; curr = next;
@@ -110,7 +109,7 @@ static TreeNode *tree_next (TreeNode *node, char *str)
else if (str == NULL) return node; else if (str == NULL) return node;
else else
{ {
int c = lua_strcmp(str, node->str); int c = lua_strcmp(str, node->ts.str);
if (c == 0) if (c == 0)
return node->left != NULL ? node->left : node->right; return node->left != NULL ? node->left : node->right;
else if (c < 0) else if (c < 0)
@@ -131,10 +130,10 @@ TreeNode *lua_varnext (char *n)
{ /* repeat until a valid (non nil) variable */ { /* repeat until a valid (non nil) variable */
result = tree_next(constant_root, name); result = tree_next(constant_root, name);
if (result == NULL) return NULL; if (result == NULL) return NULL;
if (result->varindex != UNMARKED_STRING && if (result->varindex != NOT_USED &&
s_tag(result->varindex) != LUA_T_NIL) s_tag(result->varindex) != LUA_T_NIL)
return result; return result;
name = result->str; name = result->ts.str;
} }
} }

25
tree.h
View File

@@ -1,34 +1,33 @@
/* /*
** tree.h ** tree.h
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
** $Id: tree.h,v 1.4 1994/11/17 13:58:57 roberto Exp roberto $ ** $Id: tree.h,v 1.5 1994/11/18 19:27:38 roberto Exp roberto $
*/ */
#ifndef tree_h #ifndef tree_h
#define tree_h #define tree_h
#include "opcode.h"
#define NOT_USED 0xFFFE
#define UNMARKED_STRING 0xFFFF typedef struct TaggedString
#define MARKED_STRING 0xFFFE {
#define MAX_WORD 0xFFFD char marked; /* for garbage collection */
char str[1]; /* \0 byte already reserved */
} TaggedString;
typedef struct TreeNode typedef struct TreeNode
{ {
struct TreeNode *right; struct TreeNode *right;
struct TreeNode *left; struct TreeNode *left;
Word varindex; /* if this is a symbol */ unsigned short varindex; /* != NOT_USED if this is a symbol */
Word constindex; /* if this is a constant; also used for garbage collection */ unsigned short constindex; /* != NOT_USED if this is a constant */
char str[1]; /* \0 byte already reserved */ TaggedString ts;
} TreeNode; } TreeNode;
#define indexstring(s) (*(((Word *)s)-1)) TaggedString *lua_createstring (char *str);
char *lua_createstring (char *str);
TreeNode *lua_constcreate (char *str); TreeNode *lua_constcreate (char *str);
int lua_strcollector (void); int lua_strcollector (void);
TreeNode *lua_varnext (char *n); TreeNode *lua_varnext (char *n);