Browse Source

garbage collection tag for strings organized in struct TaggedString

Roberto Ierusalimschy 31 years ago
parent
commit
d490555ec9
7 changed files with 56 additions and 53 deletions
  1. 2 2
      inout.c
  2. 7 7
      opcode.c
  3. 5 3
      opcode.h
  4. 13 11
      table.c
  5. 3 2
      table.h
  6. 15 16
      tree.c
  7. 11 12
      tree.h

+ 2 - 2
inout.c

@@ -5,7 +5,7 @@
 ** 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 <stdlib.h>
@@ -166,7 +166,7 @@ void lua_reportbug (char *s)
    {
      sprintf (strchr(msg,0),
        "\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;
      func = func->next;
      lua_popfunction();

+ 7 - 7
opcode.c

@@ -3,7 +3,7 @@
 ** 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 <stdio.h>
@@ -165,8 +165,8 @@ static int lua_tostring (Object *obj)
    sprintf (s, "%d", (int) nvalue(obj));
  else
    sprintf (s, "%g", nvalue(obj));
- svalue(obj) = lua_createstring(s);
- if (svalue(obj) == NULL)
+ tsvalue(obj) = lua_createstring(s);
+ if (tsvalue(obj) == NULL)
   return 1;
  tag(obj) = LUA_T_STRING;
  return 0;
@@ -637,7 +637,7 @@ int lua_pushnumber (real n)
 int lua_pushstring (char *s)
 {
  lua_checkstack(top-stack+1);
- svalue(top) = lua_createstring(s);
+ tsvalue(top) = lua_createstring(s);
  tag(top) = LUA_T_STRING;
  top++;
  return 0;
@@ -770,7 +770,7 @@ static int lua_execute (Byte *pc, int base)
    {
     CodeWord code;
     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;
 
@@ -877,7 +877,7 @@ static int lua_execute (Byte *pc, int base)
     {
      CodeWord code;
      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);
      top--;
      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);
     else
     {
-      svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
+      tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
       --top;
     }
    }

+ 5 - 3
opcode.h

@@ -1,12 +1,13 @@
 /*
 ** 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
 #define opcode_h
 
 #include "lua.h"
+#include "tree.h"
 
 #ifndef STACKGAP
 #define STACKGAP	128
@@ -109,7 +110,7 @@ typedef union
 {
  Cfunction     f;
  real          n;
- char         *s;
+ TaggedString *ts;
  Byte         *b;
  struct Hash    *a;
  void           *u;
@@ -129,7 +130,8 @@ typedef struct
 /* Macros to access structure members */
 #define tag(o)		((o)->tag)
 #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 avalue(o)	((o)->value.a)
 #define fvalue(o)	((o)->value.f)

+ 13 - 11
table.c

@@ -3,7 +3,7 @@
 ** 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>
 
@@ -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"
 
 
+#define MAX_WORD  0xFFFD
+
 #define BUFFER_BLOCK 256
 
 Symbol *lua_table;
 static Word lua_ntable = 0;
 static Long lua_maxsymbol = 0;
 
-char **lua_constant;
+TaggedString **lua_constant;
 static Word lua_nconstant = 0;
 static Long lua_maxconstant = 0;
 
@@ -79,7 +81,7 @@ static void lua_initsymbol (void)
 void lua_initconstant (void)
 {
  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)
   lua_initsymbol(); 
- if (t->varindex == UNMARKED_STRING)
+ if (t->varindex == NOT_USED)
  {
   if (lua_ntable == lua_maxsymbol)
   {
@@ -124,17 +126,17 @@ int luaI_findconstant (TreeNode *t)
 {
  if (lua_constant == NULL)
   lua_initconstant();
- if (t->constindex == UNMARKED_STRING)
+ if (t->constindex == NOT_USED)
  {
   if (lua_nconstant == lua_maxconstant)
   {
    lua_maxconstant *= 2;
    if (lua_maxconstant > MAX_WORD)
      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;
-  lua_constant[lua_nconstant] = t->str;
+  lua_constant[lua_nconstant] = &(t->ts);
   lua_nconstant++;
  }
  return t->constindex;
@@ -157,10 +159,10 @@ void lua_travsymbol (void (*fn)(Object *))
 */
 void lua_markobject (Object *o)
 {
- if (tag(o) == LUA_T_STRING && indexstring(svalue(o)) == UNMARKED_STRING)
-  indexstring(svalue(o)) = MARKED_STRING;
+ if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
+   tsvalue(o)->marked = 1;
  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;
   tag(&name) = LUA_T_STRING;
-  svalue(&name) = next->str;
+  tsvalue(&name) = &(next->ts);
   luaI_pushobject(&name);
   luaI_pushobject(&s_object(next->varindex));
  }

+ 3 - 2
table.h

@@ -1,16 +1,17 @@
 /*
 ** Module to control static tables
 ** 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
 #define table_h
 
 #include "tree.h"
+#include "opcode.h"
 
 extern Symbol *lua_table;
-extern char  **lua_constant;
+extern TaggedString **lua_constant;
 
 extern char   *lua_file[];
 extern int     lua_nfile;

+ 15 - 16
tree.c

@@ -3,7 +3,7 @@
 ** 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>
@@ -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 {
   struct StringNode *next;
-  Word mark;
-  char str[1];
+  TaggedString ts;
 } StringNode;
 
 static StringNode *string_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)->left = (*node)->right = NULL;
-  strcpy((*node)->str, str);
-  (*node)->varindex = (*node)->constindex = UNMARKED_STRING;
+  strcpy((*node)->ts.str, str);
+  (*node)->ts.marked = 0;
+  (*node)->varindex = (*node)->constindex = NOT_USED;
   return *node;
  }
  else
  {
-  int c = lua_strcmp(str, (*node)->str);
+  int c = lua_strcmp(str, (*node)->ts.str);
   if (c < 0) 
    return tree_create(&(*node)->left, str);
   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;
   if (str == NULL) return NULL;
   lua_pack();
   newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
-  newString->mark = UNMARKED_STRING;
-  strcpy(newString->str, str);
+  newString->ts.marked = 0;
+  strcpy(newString->ts.str, str);
   newString->next = string_root;
   string_root = newString;
-  return newString->str;
+  return &(newString->ts);
 }
 
 
@@ -84,7 +83,7 @@ int lua_strcollector (void)
   while (curr)
   {
     StringNode *next = curr->next;
-    if (curr->mark == UNMARKED_STRING)
+    if (!curr->ts.marked)
     {
       if (prev == NULL) string_root = next;
       else prev->next = next;
@@ -93,7 +92,7 @@ int lua_strcollector (void)
     }
     else
     {
-      curr->mark = UNMARKED_STRING;
+      curr->ts.marked = 0;
       prev = curr;
     }
     curr = next;
@@ -110,7 +109,7 @@ static TreeNode *tree_next (TreeNode *node, char *str)
  else if (str == NULL) return node;
  else
  {
-  int c = lua_strcmp(str, node->str);
+  int c = lua_strcmp(str, node->ts.str);
   if (c == 0)
    return node->left != NULL ? node->left : node->right;
   else if (c < 0)
@@ -131,10 +130,10 @@ TreeNode *lua_varnext (char *n)
   { /* repeat until a valid (non nil) variable */
     result = tree_next(constant_root, name);
     if (result == NULL) return NULL;
-    if (result->varindex != UNMARKED_STRING &&
+    if (result->varindex != NOT_USED &&
         s_tag(result->varindex) != LUA_T_NIL)
       return result;
-    name = result->str;
+    name = result->ts.str;
   }
 }
 

+ 11 - 12
tree.h

@@ -1,34 +1,33 @@
 /*
 ** tree.h
 ** 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
 #define tree_h
 
-#include "opcode.h"
 
+#define NOT_USED  0xFFFE
 
-#define UNMARKED_STRING	0xFFFF
-#define MARKED_STRING   0xFFFE
-#define MAX_WORD        0xFFFD
 
+typedef struct TaggedString
+{
+  char marked;   /* for garbage collection */
+  char str[1];   /* \0 byte already reserved */
+} TaggedString;
  
 typedef struct TreeNode
 {
  struct TreeNode *right;
  struct TreeNode *left;
- Word varindex;  /* if this is a symbol */
- Word constindex;  /* if this is a constant; also used for garbage collection */
- char            str[1];        /* \0 byte already reserved */
+ unsigned short varindex;  /* != NOT_USED  if this is a symbol */
+ unsigned short constindex;  /* != NOT_USED  if this is a constant */
+ TaggedString ts;
 } TreeNode;
 
 
-#define indexstring(s) (*(((Word *)s)-1))
-
-
-char *lua_createstring (char *str);
+TaggedString *lua_createstring (char *str);
 TreeNode *lua_constcreate  (char *str);
 int  lua_strcollector (void);
 TreeNode *lua_varnext      (char *n);