From 0ef5cf22891c9d34a88ccc5d89eb0ed82b004471 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Apr 1996 15:00:37 -0300 Subject: lock mechanism seperseded by the REFERENCE mechanism. --- fallback.c | 70 +++++++++++++++++++++++++++++++++++---------------------- fallback.h | 8 ++++--- lua.h | 21 +++++++++++++----- luamem.h | 4 ++-- manual.tex | 75 ++++++++++++++++++++++++++++++++++++++------------------------ mathlib.c | 8 +++---- opcode.c | 46 +++++++++++++++++++++----------------- table.c | 21 +++++++++++++++++- table.h | 3 ++- 9 files changed, 163 insertions(+), 93 deletions(-) diff --git a/fallback.c b/fallback.c index 8913797f..9d2db515 100644 --- a/fallback.c +++ b/fallback.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp roberto $"; +char *rcs_fallback="$Id: fallback.c,v 1.23 1996/03/21 16:31:32 roberto Exp roberto $"; #include #include @@ -12,6 +12,7 @@ char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp rober #include "fallback.h" #include "opcode.h" #include "lua.h" +#include "table.h" static void errorFB (void); @@ -112,59 +113,74 @@ static void funcFB (void) /* -** Lock routines +** Reference routines */ -static Object *lockArray = NULL; -static int lockSize = 0; +static struct ref { + Object o; + enum {LOCK, HOLD, FREE, COLLECTED} status; +} *refArray = NULL; +static int refSize = 0; -int luaI_lock (Object *object) +lua_Reference luaI_ref (Object *object, int lock) { int i; int oldSize; if (tag(object) == LUA_T_NIL) - return -1; /* special lock ref for nil */ - for (i=0; i= 0 && ref < lockSize) - tag(&lockArray[ref]) = LUA_T_NIL; + if (ref >= 0 && ref < refSize) + refArray[ref].status = FREE; } -Object *luaI_getlocked (int ref) +Object *luaI_getref (lua_Reference ref) { static Object nul = {LUA_T_NIL, {0}}; - if (ref >= 0 && ref < lockSize) - return &lockArray[ref]; - else + if (ref == -1) return &nul; + if (ref >= 0 && ref < refSize && + (refArray[ref].status == LOCK || refArray[ref].status == HOLD)) + return &refArray[ref].o; + else + return NULL; } void luaI_travlock (int (*fn)(Object *)) { int i; - for (i=0; i #include @@ -104,7 +104,7 @@ static void math_sqrt (void) lua_pushnumber (sqrt(d)); } -static int old_pow; +static lua_Reference old_pow; static void math_pow (void) { @@ -113,7 +113,7 @@ static void math_pow (void) lua_Object op = lua_getparam(3); if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') { - lua_Object old = lua_getlocked(old_pow); + lua_Object old = lua_getref(old_pow); lua_pushobject(o1); lua_pushobject(o2); lua_pushobject(op); @@ -223,5 +223,5 @@ void mathlib_open (void) lua_register ("random", math_random); lua_register ("randomseed", math_randomseed); - old_pow = lua_lockobject(lua_setfallback("arith", math_pow)); + old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1); } diff --git a/opcode.c b/opcode.c index ffa1c844..c2437a56 100644 --- a/opcode.c +++ b/opcode.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_opcode="$Id: opcode.c,v 3.65 1996/03/21 18:55:02 roberto Exp roberto $"; +char *rcs_opcode="$Id: opcode.c,v 3.66 1996/03/22 19:12:15 roberto Exp roberto $"; #include #include @@ -717,27 +717,31 @@ void *lua_getuserdata (lua_Object object) } -lua_Object lua_getlocked (int ref) +lua_Object lua_getref (lua_Reference ref) { - adjustC(0); - *top = *luaI_getlocked(ref); - incr_top; - CBase++; /* incorporate object in the stack */ - return Ref(top-1); + Object *o = luaI_getref(ref); + if (o == NULL) + return LUA_NOOBJECT; + adjustC(0); + luaI_pushobject(o); + CBase++; /* incorporate object in the stack */ + return Ref(top-1); } -void lua_pushlocked (int ref) +void lua_pushref (lua_Reference ref) { - *top = *luaI_getlocked(ref); - incr_top; + Object *o = luaI_getref(ref); + if (o == NULL) + lua_error("access to invalid (possibly garbage collected) reference"); + luaI_pushobject(o); } -int lua_lock (void) +lua_Reference lua_ref (int lock) { adjustC(1); - return luaI_lock(--top); + return luaI_ref(--top, lock); } @@ -812,27 +816,29 @@ void lua_pushcfunction (lua_CFunction fn) */ void lua_pushusertag (void *u, int tag) { - if (tag < LUA_T_USERDATA) return; + if (tag < LUA_T_USERDATA) + lua_error("invalid tag in `lua_pushusertag'"); tag(top) = tag; uvalue(top) = u; incr_top; } /* -** Push a lua_Object to stack. +** Push an object on the stack. */ -void lua_pushobject (lua_Object o) +void luaI_pushobject (Object *o) { - *top = *Address(o); + *top = *o; incr_top; } /* -** Push an object on the stack. +** Push a lua_Object on stack. */ -void luaI_pushobject (Object *o) +void lua_pushobject (lua_Object o) { - *top = *o; - incr_top; + if (o == LUA_NOOBJECT) + lua_error("attempt to push a NOOBJECT"); + luaI_pushobject(Address(o)); } int lua_type (lua_Object o) diff --git a/table.c b/table.c index 6c77d2e9..31ec0ac1 100644 --- a/table.c +++ b/table.c @@ -3,7 +3,7 @@ ** Module to control static tables */ -char *rcs_table="$Id: table.c,v 2.50 1996/03/21 16:31:32 roberto Exp roberto $"; +char *rcs_table="$Id: table.c,v 2.51 1996/03/21 18:54:29 roberto Exp roberto $"; #include "mem.h" #include "opcode.h" @@ -170,6 +170,24 @@ int lua_markobject (Object *o) return 0; } +/* +* returns 0 if the object is going to be (garbage) collected +*/ +int luaI_ismarked (Object *o) +{ + switch (o->tag) + { + case LUA_T_STRING: + return o->value.ts->marked; + case LUA_T_FUNCTION: + return o->value.tf->marked; + case LUA_T_ARRAY: + return o->value.a->mark; + default: /* nil, number, cfunction, or user data */ + return 1; + } +} + /* ** Garbage collection. @@ -182,6 +200,7 @@ Long luaI_collectgarbage (void) lua_travsymbol(lua_markobject); /* mark symbol table objects */ luaI_travlock(lua_markobject); /* mark locked objects */ luaI_travfallbacks(lua_markobject); /* mark fallbacks */ + luaI_invalidaterefs(); recovered += lua_strcollector(); recovered += lua_hashcollector(); recovered += luaI_funccollector(); diff --git a/table.h b/table.h index de942458..bfe1a6be 100644 --- a/table.h +++ b/table.h @@ -1,7 +1,7 @@ /* ** Module to control static tables ** TeCGraf - PUC-Rio -** $Id: table.h,v 2.19 1996/02/26 21:00:27 roberto Exp roberto $ +** $Id: table.h,v 2.20 1996/03/14 15:57:19 roberto Exp roberto $ */ #ifndef table_h @@ -30,6 +30,7 @@ Word luaI_findconstant (TaggedString *t); Word luaI_findconstantbyname (char *name); TaggedString *luaI_createfixedstring (char *str); int lua_markobject (Object *o); +int luaI_ismarked (Object *o); Long luaI_collectgarbage (void); void lua_pack (void); -- cgit v1.2.3-55-g6feb