Browse Source

Garbage collection of functions + header structure for functions

Roberto Ierusalimschy 30 years ago
parent
commit
68f337dfa6
8 changed files with 56 additions and 36 deletions
  1. 8 1
      fallback.c
  2. 2 1
      fallback.h
  3. 3 3
      hash.c
  4. 2 2
      inout.c
  5. 18 13
      lua.stx
  6. 13 9
      opcode.c
  7. 5 6
      opcode.h
  8. 5 1
      table.c

+ 8 - 1
fallback.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 ** TecCGraf - PUC-Rio
 */
 */
  
  
-char *rcs_fallback="$Id: fallback.c,v 1.12 1995/05/02 18:43:03 roberto Exp roberto $";
+char *rcs_fallback="$Id: fallback.c,v 1.13 1995/10/02 17:03:33 roberto Exp roberto $";
 
 
 #include <stdio.h>
 #include <stdio.h>
 #include <string.h>
 #include <string.h>
@@ -168,3 +168,10 @@ void luaI_travlock (void (*fn)(Object *))
     fn(&lockArray[i]);
     fn(&lockArray[i]);
 }
 }
 
 
+
+void luaI_travfallbacks (void (*fn)(Object *))
+{
+  Word i;
+  for (i=0; i<N_FB; i++)
+    fn(&luaI_fallBacks[i].function);
+}

+ 2 - 1
fallback.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: fallback.h,v 1.6 1994/11/21 13:30:15 roberto Exp roberto $
+** $Id: fallback.h,v 1.7 1994/11/21 18:22:58 roberto Stab roberto $
 */
 */
  
  
 #ifndef fallback_h
 #ifndef fallback_h
@@ -26,6 +26,7 @@ void luaI_setfallback (void);
 int luaI_lock (Object *object);
 int luaI_lock (Object *object);
 Object *luaI_getlocked (int ref);
 Object *luaI_getlocked (int ref);
 void luaI_travlock (void (*fn)(Object *));
 void luaI_travlock (void (*fn)(Object *));
+void luaI_travfallbacks (void (*fn)(Object *));
 
 
 #endif
 #endif
 
 

+ 3 - 3
hash.c

@@ -3,7 +3,7 @@
 ** hash manager for lua
 ** hash manager for lua
 */
 */
 
 
-char *rcs_hash="$Id: hash.c,v 2.24 1995/02/06 19:34:03 roberto Exp roberto $";
+char *rcs_hash="$Id: hash.c,v 2.25 1995/05/02 18:43:03 roberto Exp $";
 
 
 #include <string.h>
 #include <string.h>
 
 
@@ -70,7 +70,7 @@ static Word hashindex (Hash *t, Object *ref)		/* hash function */
    return (Word)h%nhash(t);  /* make it a valid index */
    return (Word)h%nhash(t);  /* make it a valid index */
   }
   }
   case LUA_T_FUNCTION:
   case LUA_T_FUNCTION:
-   return (((IntPoint)bvalue(ref))%nhash(t));
+   return (((IntPoint)ref->value.tf)%nhash(t));
   case LUA_T_CFUNCTION:
   case LUA_T_CFUNCTION:
    return (((IntPoint)fvalue(ref))%nhash(t));
    return (((IntPoint)fvalue(ref))%nhash(t));
   case LUA_T_ARRAY:
   case LUA_T_ARRAY:
@@ -89,7 +89,7 @@ Bool lua_equalObj (Object *t1, Object *t2)
     case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
     case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
     case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
     case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
     case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
     case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
-    case LUA_T_FUNCTION: return bvalue(t1) == bvalue(t2);
+    case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
     case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
     case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
     default: return uvalue(t1) == uvalue(t2);
     default: return uvalue(t1) == uvalue(t2);
   }
   }

+ 2 - 2
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 ** Also provides some predefined lua functions.
 */
 */
 
 
-char *rcs_inout="$Id: inout.c,v 2.19 1995/05/02 18:43:03 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.20 1995/05/16 17:23:58 roberto Exp $";
 
 
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
@@ -222,7 +222,7 @@ void lua_print (void)
  {
  {
   if      (lua_isnumber(obj))    printf("%g\n",lua_getnumber(obj));
   if      (lua_isnumber(obj))    printf("%g\n",lua_getnumber(obj));
   else if (lua_isstring(obj))    printf("%s\n",lua_getstring(obj));
   else if (lua_isstring(obj))    printf("%s\n",lua_getstring(obj));
-  else if (lua_isfunction(obj))  printf("function: %p\n",bvalue(luaI_Address(obj)));
+  else if (lua_isfunction(obj))  printf("function: %p\n",(luaI_Address(obj))->value.tf);
   else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
   else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
 );
 );
   else if (lua_isuserdata(obj))  printf("userdata: %p\n",lua_getuserdata(obj));
   else if (lua_isuserdata(obj))  printf("userdata: %p\n",lua_getuserdata(obj));

+ 18 - 13
lua.stx

@@ -1,6 +1,6 @@
 %{
 %{
 
 
-char *rcs_luastx = "$Id: lua.stx,v 3.18 1995/04/11 17:56:30 celes Exp roberto $";
+char *rcs_luastx = "$Id: lua.stx,v 3.19 1995/06/08 19:47:28 roberto Exp $";
 
 
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
@@ -13,6 +13,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.18 1995/04/11 17:56:30 celes Exp roberto $"
 #include "tree.h"
 #include "tree.h"
 #include "table.h"
 #include "table.h"
 #include "lua.h"
 #include "lua.h"
+#include "func.h"
 
 
 /* to avoid warnings generated by yacc */
 /* to avoid warnings generated by yacc */
 int yyparse (void);
 int yyparse (void);
@@ -84,10 +85,10 @@ static void code_float (float n)
  code_byte(code.m.c4);
  code_byte(code.m.c4);
 }
 }
 
 
-static void code_code (Byte *b)
+static void code_code (TFunc *tf)
 {
 {
  CodeCode code;
  CodeCode code;
- code.b = b;
+ code.tf = tf;
  code_byte(code.m.c1);
  code_byte(code.m.c1);
  code_byte(code.m.c2);
  code_byte(code.m.c2);
  code_byte(code.m.c3);
  code_byte(code.m.c3);
@@ -246,7 +247,7 @@ static void init_function (TreeNode *func)
   if (lua_debug)
   if (lua_debug)
   {
   {
     code_byte(SETFUNCTION); 
     code_byte(SETFUNCTION); 
-    code_code((Byte *)luaI_strdup(lua_file[lua_nfile-1]));
+    code_code((TFunc *)luaI_strdup(lua_file[lua_nfile-1]));
     code_word(luaI_findconstant(func));
     code_word(luaI_findconstant(func));
   }
   }
 }
 }
@@ -353,14 +354,15 @@ static void yyerror (char *s)
 /*
 /*
 ** Parse LUA code.
 ** Parse LUA code.
 */
 */
-void lua_parse (Byte **code)
+void lua_parse (TFunc *tf)
 {
 {
- initcode = code;
+ initcode = &(tf->code);
  *initcode = newvector(CODE_BLOCK, Byte);
  *initcode = newvector(CODE_BLOCK, Byte);
  maincode = 0; 
  maincode = 0; 
  maxmain = CODE_BLOCK;
  maxmain = CODE_BLOCK;
  if (yyparse ()) lua_error("parse error");
  if (yyparse ()) lua_error("parse error");
  (*initcode)[maincode++] = RETCODE0;
  (*initcode)[maincode++] = RETCODE0;
+ tf->size = maincode;
 #if LISTING
 #if LISTING
 { static void PrintCode (Byte *c, Byte *end);
 { static void PrintCode (Byte *c, Byte *end);
  PrintCode(*initcode,*initcode+maincode); }
  PrintCode(*initcode,*initcode+maincode); }
@@ -378,7 +380,7 @@ void lua_parse (Byte **code)
  char *pChar;
  char *pChar;
  Word  vWord;
  Word  vWord;
  Long  vLong;
  Long  vLong;
- Byte *pByte;
+ TFunc *pFunc;
  TreeNode *pNode;
  TreeNode *pNode;
 }
 }
 
 
@@ -401,7 +403,7 @@ void lua_parse (Byte **code)
 %type <vInt>  ffieldlist, ffieldlist1, semicolonpart
 %type <vInt>  ffieldlist, ffieldlist1, semicolonpart
 %type <vInt>  lfieldlist, lfieldlist1
 %type <vInt>  lfieldlist, lfieldlist1
 %type <vLong> var, singlevar
 %type <vLong> var, singlevar
-%type <pByte> body
+%type <pFunc> body
 
 
 %left AND OR
 %left AND OR
 %left EQ NE '>' '<' LE GE
 %left EQ NE '>' '<' LE GE
@@ -437,8 +439,9 @@ function     : FUNCTION NAME
                body 	 
                body 	 
 	       { 
 	       { 
 		Word func = luaI_findsymbol($2);
 		Word func = luaI_findsymbol($2);
+		luaI_insertfunction($4);  /* may take part in GC */
 	        s_tag(func) = LUA_T_FUNCTION;
 	        s_tag(func) = LUA_T_FUNCTION;
-	        s_bvalue(func) = $4;
+                lua_table[func].object.value.tf = $4;
 	       }
 	       }
 	       ;
 	       ;
 
 
@@ -465,8 +468,10 @@ method         : FUNCTION NAME ':' NAME
 body :  '(' parlist ')' block END
 body :  '(' parlist ')' block END
 	{
 	{
           codereturn();
           codereturn();
-	  $$ = newvector(pc, Byte);
-	  memcpy($$, basepc, pc*sizeof(Byte));
+	  $$ = new(TFunc);
+	  $$->size = pc;
+	  $$->code = newvector(pc, Byte);
+	  memcpy($$->code, basepc, pc*sizeof(Byte));
 	  funcCode = basepc; maxcode=maxcurr;
 	  funcCode = basepc; maxcode=maxcurr;
 #if LISTING
 #if LISTING
                 PrintCode(funcCode,funcCode+pc);
                 PrintCode(funcCode,funcCode+pc);
@@ -805,7 +810,7 @@ static void PrintCode (Byte *code, Byte *end)
 			 int n = p-code;
 			 int n = p-code;
 			 p++;
 			 p++;
 			 get_code(c,p);
 			 get_code(c,p);
-			 printf ("%d    PUSHFUNCTION  %p\n", n, c.b);
+			 printf ("%d    PUSHFUNCTION  %p\n", n, c.tf);
 			}
 			}
 			break;
 			break;
 
 
@@ -969,7 +974,7 @@ static void PrintCode (Byte *code, Byte *end)
                          p++;
                          p++;
                          get_code(c1,p);
                          get_code(c1,p);
                          get_word(c2,p);
                          get_word(c2,p);
-                         printf ("%d    SETFUNCTION  %s  %d\n", n, (char *)c1.b, c2.w);
+                         printf ("%d    SETFUNCTION  %s  %d\n", n, (char *)c1.tf, c2.w);
                         }
                         }
                         break;
                         break;
    case SETLINE:
    case SETLINE:

+ 13 - 9
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 ** TecCGraf - PUC-Rio
 */
 */
 
 
-char *rcs_opcode="$Id: opcode.c,v 3.38 1995/05/16 17:23:58 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.38 1995/05/16 17:23:58 roberto Exp $";
 
 
 #include <setjmp.h>
 #include <setjmp.h>
 #include <stdlib.h>
 #include <stdlib.h>
@@ -259,7 +259,7 @@ static void do_call (Object *func, StkId base, int nResults, StkId whereRes)
   if (tag(func) == LUA_T_CFUNCTION)
   if (tag(func) == LUA_T_CFUNCTION)
     firstResult = callC(fvalue(func), base);
     firstResult = callC(fvalue(func), base);
   else if (tag(func) == LUA_T_FUNCTION)
   else if (tag(func) == LUA_T_FUNCTION)
-    firstResult = lua_execute(bvalue(func), base);
+    firstResult = lua_execute(func->value.tf->code, base);
   else
   else
   { /* func is not a function */
   { /* func is not a function */
     call_funcFB(func, base, nResults, whereRes);
     call_funcFB(func, base, nResults, whereRes);
@@ -360,24 +360,26 @@ static int do_protectedrun (Object *function, int nResults)
 
 
 static int do_protectedmain (void)
 static int do_protectedmain (void)
 {
 {
-  Byte *code = NULL;
+  TFunc tf;
   int status;
   int status;
   StkId oldCBase = CBase;
   StkId oldCBase = CBase;
   jmp_buf myErrorJmp;
   jmp_buf myErrorJmp;
   jmp_buf *oldErr = errorJmp;
   jmp_buf *oldErr = errorJmp;
   errorJmp = &myErrorJmp;
   errorJmp = &myErrorJmp;
+  tf.code = NULL;
   if (setjmp(myErrorJmp) == 0)
   if (setjmp(myErrorJmp) == 0)
   {
   {
     Object f;
     Object f;
-    lua_parse(&code);
-    tag(&f) = LUA_T_FUNCTION; bvalue(&f) = code;
+    f.tag = LUA_T_FUNCTION;
+    f.value.tf = &tf;
+    lua_parse(&tf);
     do_call(&f, CBase, 0, CBase);
     do_call(&f, CBase, 0, CBase);
     status = 0;
     status = 0;
   }
   }
   else
   else
     status = 1;
     status = 1;
-  if (code)
-    luaI_free(code);
+  if (tf.code)
+    luaI_free(tf.code);
   errorJmp = oldErr;
   errorJmp = oldErr;
   CBase = oldCBase;
   CBase = oldCBase;
   top = stack+CBase;
   top = stack+CBase;
@@ -793,7 +795,9 @@ static StkId lua_execute (Byte *pc, StkId base)
    {
    {
     CodeCode code;
     CodeCode code;
     get_code(code,pc);
     get_code(code,pc);
-    tag(top) = LUA_T_FUNCTION; bvalue(top) = code.b;
+    luaI_insertfunction(code.tf);  /* may take part in GC */
+    top->tag = LUA_T_FUNCTION;
+    top->value.tf = code.tf;
     incr_top;
     incr_top;
    }
    }
    break;
    break;
@@ -1116,7 +1120,7 @@ static StkId lua_execute (Byte *pc, StkId base)
     CodeWord func;
     CodeWord func;
     get_code(file,pc);
     get_code(file,pc);
     get_word(func,pc);
     get_word(func,pc);
-    lua_pushfunction ((char *)file.b, func.w);
+    lua_pushfunction ((char *)file.tf, func.w);
    }
    }
    break;
    break;
 
 

+ 5 - 6
opcode.h

@@ -1,6 +1,6 @@
 /*
 /*
 ** TeCGraf - PUC-Rio
 ** TeCGraf - PUC-Rio
-** $Id: opcode.h,v 3.10 1994/12/20 21:20:36 roberto Exp celes $
+** $Id: opcode.h,v 3.11 1995/04/11 17:56:30 celes Exp $
 */
 */
 
 
 #ifndef opcode_h
 #ifndef opcode_h
@@ -9,6 +9,7 @@
 #include "lua.h"
 #include "lua.h"
 #include "types.h"
 #include "types.h"
 #include "tree.h"
 #include "tree.h"
+#include "func.h"
 
 
 #ifndef real
 #ifndef real
 #define real float
 #define real float
@@ -83,7 +84,7 @@ typedef union
  Cfunction     f;
  Cfunction     f;
  real          n;
  real          n;
  TaggedString *ts;
  TaggedString *ts;
- Byte         *b;
+ TFunc         *tf;
  struct Hash    *a;
  struct Hash    *a;
  void           *u;
  void           *u;
 } Value;
 } Value;
@@ -104,7 +105,6 @@ typedef struct
 #define nvalue(o)	((o)->value.n)
 #define nvalue(o)	((o)->value.n)
 #define svalue(o)	((o)->value.ts->str)
 #define svalue(o)	((o)->value.ts->str)
 #define tsvalue(o)	((o)->value.ts)
 #define tsvalue(o)	((o)->value.ts)
-#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)
 #define uvalue(o)	((o)->value.u)
 #define uvalue(o)	((o)->value.u)
@@ -114,7 +114,6 @@ typedef struct
 #define s_tag(i)	(tag(&s_object(i)))
 #define s_tag(i)	(tag(&s_object(i)))
 #define s_nvalue(i)	(nvalue(&s_object(i)))
 #define s_nvalue(i)	(nvalue(&s_object(i)))
 #define s_svalue(i)	(svalue(&s_object(i)))
 #define s_svalue(i)	(svalue(&s_object(i)))
-#define s_bvalue(i)	(bvalue(&s_object(i)))
 #define s_avalue(i)	(avalue(&s_object(i)))
 #define s_avalue(i)	(avalue(&s_object(i)))
 #define s_fvalue(i)	(fvalue(&s_object(i)))
 #define s_fvalue(i)	(fvalue(&s_object(i)))
 #define s_uvalue(i)	(uvalue(&s_object(i)))
 #define s_uvalue(i)	(uvalue(&s_object(i)))
@@ -137,7 +136,7 @@ typedef union
 typedef union
 typedef union
 {
 {
  struct {char c1; char c2; char c3; char c4;} m;
  struct {char c1; char c2; char c3; char c4;} m;
- Byte *b;
+ TFunc *tf;
 } CodeCode;
 } CodeCode;
 #define get_code(code,pc)    {code.m.c1 = *pc++; code.m.c2 = *pc++;\
 #define get_code(code,pc)    {code.m.c1 = *pc++; code.m.c2 = *pc++;\
                               code.m.c3 = *pc++; code.m.c4 = *pc++;}
                               code.m.c3 = *pc++; code.m.c4 = *pc++;}
@@ -149,7 +148,7 @@ char   *lua_strdup (char *l);
 void    lua_setinput   (Input fn);	/* from "lex.c" module */
 void    lua_setinput   (Input fn);	/* from "lex.c" module */
 char   *lua_lasttext   (void);		/* from "lex.c" module */
 char   *lua_lasttext   (void);		/* from "lex.c" module */
 int     yylex (void);		        /* from "lex.c" module */
 int     yylex (void);		        /* from "lex.c" module */
-void    lua_parse      (Byte **code);	/* from "lua.stx" module */
+void    lua_parse      (TFunc *tf);	/* from "lua.stx" module */
 void    lua_travstack (void (*fn)(Object *));
 void    lua_travstack (void (*fn)(Object *));
 Object *luaI_Address (lua_Object o);
 Object *luaI_Address (lua_Object o);
 void	luaI_pushobject (Object *o);
 void	luaI_pushobject (Object *o);

+ 5 - 1
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 ** Module to control static tables
 */
 */
 
 
-char *rcs_table="$Id: table.c,v 2.31 1995/05/16 19:23:55 celes Exp roberto $";
+char *rcs_table="$Id: table.c,v 2.32 1995/09/15 20:47:53 roberto Exp $";
 
 
 #include <string.h>
 #include <string.h>
 
 
@@ -166,6 +166,8 @@ void lua_markobject (Object *o)
    tsvalue(o)->marked = 1;
    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));
+ else if (o->tag == LUA_T_FUNCTION && !o->value.tf->marked)
+   o->value.tf->marked = 1;
 }
 }
 
 
 
 
@@ -182,8 +184,10 @@ void lua_pack (void)
   lua_travstack(lua_markobject); /* mark stack objects */
   lua_travstack(lua_markobject); /* mark stack objects */
   lua_travsymbol(lua_markobject); /* mark symbol table objects */
   lua_travsymbol(lua_markobject); /* mark symbol table objects */
   luaI_travlock(lua_markobject); /* mark locked objects */
   luaI_travlock(lua_markobject); /* mark locked objects */
+  luaI_travfallbacks(lua_markobject);  /* mark fallbacks */
   recovered += lua_strcollector();
   recovered += lua_strcollector();
   recovered += lua_hashcollector();
   recovered += lua_hashcollector();
+  recovered += luaI_funccollector();
   nentity = 0;				/* reset counter */
   nentity = 0;				/* reset counter */
   block=(16*block-7*recovered)/12;	/* adapt block size */
   block=(16*block-7*recovered)/12;	/* adapt block size */
   if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
   if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;