1999-10-04 21:50:24 +04:00
|
|
|
/*
|
2000-03-10 21:37:44 +03:00
|
|
|
** $Id: lref.c,v 1.8 2000/03/03 14:58:26 roberto Exp roberto $
|
1999-12-27 20:33:22 +03:00
|
|
|
** reference mechanism
|
1999-10-04 21:50:24 +04:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
1999-12-14 21:33:29 +03:00
|
|
|
#include "lapi.h"
|
1999-10-04 21:50:24 +04:00
|
|
|
#include "lmem.h"
|
|
|
|
#include "lref.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
|
1999-12-14 21:33:29 +03:00
|
|
|
int lua_ref (lua_State *L, int lock) {
|
1999-10-04 21:50:24 +04:00
|
|
|
int ref;
|
2000-03-03 17:58:26 +03:00
|
|
|
luaA_checkCargs(L, 1);
|
2000-03-10 21:37:44 +03:00
|
|
|
if (ttype(L->top-1) == TAG_NIL)
|
1999-10-04 21:50:24 +04:00
|
|
|
ref = LUA_REFNIL;
|
|
|
|
else {
|
1999-11-10 18:37:50 +03:00
|
|
|
if (L->refFree != NONEXT) { /* is there a free place? */
|
|
|
|
ref = L->refFree;
|
|
|
|
L->refFree = L->refArray[ref].st;
|
|
|
|
}
|
|
|
|
else { /* no more free places */
|
2000-02-08 19:39:42 +03:00
|
|
|
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref, refEM, MAX_INT);
|
1999-11-10 18:37:50 +03:00
|
|
|
ref = L->refSize++;
|
1999-10-04 21:50:24 +04:00
|
|
|
}
|
1999-12-14 21:33:29 +03:00
|
|
|
L->refArray[ref].o = *(L->top-1);
|
1999-11-10 18:37:50 +03:00
|
|
|
L->refArray[ref].st = lock ? LOCK : HOLD;
|
1999-10-04 21:50:24 +04:00
|
|
|
}
|
1999-12-14 21:33:29 +03:00
|
|
|
L->top--;
|
1999-10-04 21:50:24 +04:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_unref (lua_State *L, int ref) {
|
1999-11-10 18:37:50 +03:00
|
|
|
if (ref >= 0) {
|
|
|
|
if (ref >= L->refSize || L->refArray[ref].st >= 0)
|
2000-03-03 17:58:26 +03:00
|
|
|
lua_error(L, "Lua API error - "
|
|
|
|
"invalid argument for function `lua_unref'");
|
1999-11-10 18:37:50 +03:00
|
|
|
L->refArray[ref].st = L->refFree;
|
|
|
|
L->refFree = ref;
|
|
|
|
}
|
1999-10-04 21:50:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-14 21:33:29 +03:00
|
|
|
lua_Object lua_getref (lua_State *L, int ref) {
|
1999-10-04 21:50:24 +04:00
|
|
|
if (ref == LUA_REFNIL)
|
1999-12-14 21:33:29 +03:00
|
|
|
return luaA_putluaObject(L, &luaO_nilobject);
|
1999-11-10 18:37:50 +03:00
|
|
|
else if (0 <= ref && ref < L->refSize &&
|
|
|
|
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
1999-12-14 21:33:29 +03:00
|
|
|
return luaA_putluaObject(L, &L->refArray[ref].o);
|
1999-10-04 21:50:24 +04:00
|
|
|
else
|
1999-12-14 21:33:29 +03:00
|
|
|
return LUA_NOOBJECT;
|
1999-10-04 21:50:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-14 21:33:29 +03:00
|
|
|
void lua_beginblock (lua_State *L) {
|
|
|
|
luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
|
|
|
|
"too many nested blocks", L->stacksize);
|
|
|
|
L->Cblocks[L->numCblocks] = L->Cstack;
|
|
|
|
L->numCblocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lua_endblock (lua_State *L) {
|
|
|
|
if (L->numCblocks <= 0)
|
2000-03-03 17:58:26 +03:00
|
|
|
lua_error(L, "Lua API error - no block to end");
|
1999-12-14 21:33:29 +03:00
|
|
|
--L->numCblocks;
|
|
|
|
L->Cstack = L->Cblocks[L->numCblocks];
|
|
|
|
L->top = L->Cstack.base;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-10-04 21:50:24 +04:00
|
|
|
static int ismarked (const TObject *o) {
|
|
|
|
/* valid only for locked objects */
|
|
|
|
switch (o->ttype) {
|
2000-03-10 21:37:44 +03:00
|
|
|
case TAG_STRING: case TAG_USERDATA:
|
1999-10-04 21:50:24 +04:00
|
|
|
return o->value.ts->marked;
|
2000-03-10 21:37:44 +03:00
|
|
|
case TAG_ARRAY:
|
1999-10-04 21:50:24 +04:00
|
|
|
return o->value.a->marked;
|
2000-03-10 21:37:44 +03:00
|
|
|
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
1999-10-04 21:50:24 +04:00
|
|
|
return o->value.cl->marked;
|
2000-03-10 21:37:44 +03:00
|
|
|
case TAG_LPROTO:
|
1999-10-04 21:50:24 +04:00
|
|
|
return o->value.tf->marked;
|
1999-11-10 18:37:50 +03:00
|
|
|
default: /* number or cproto */
|
1999-10-04 21:50:24 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-10 18:37:50 +03:00
|
|
|
/* for internal debugging only; check if a link of free refs is valid */
|
1999-11-22 16:12:07 +03:00
|
|
|
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
1999-11-10 18:37:50 +03:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaR_invalidaterefs (lua_State *L) {
|
1999-11-10 18:37:50 +03:00
|
|
|
int n = L->refSize;
|
1999-10-04 21:50:24 +04:00
|
|
|
int i;
|
1999-11-10 18:37:50 +03:00
|
|
|
for (i=0; i<n; i++) {
|
2000-02-08 19:39:42 +03:00
|
|
|
struct Ref *r = &L->refArray[i];
|
1999-11-10 18:37:50 +03:00
|
|
|
if (r->st == HOLD && !ismarked(&r->o))
|
|
|
|
r->st = COLLECTED;
|
1999-11-22 16:12:07 +03:00
|
|
|
LUA_ASSERT(L, (r->st == LOCK && ismarked(&r->o)) ||
|
1999-11-10 18:37:50 +03:00
|
|
|
r->st == COLLECTED ||
|
|
|
|
r->st == NONEXT ||
|
1999-11-22 16:12:07 +03:00
|
|
|
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
1999-11-10 18:37:50 +03:00
|
|
|
"inconsistent ref table");
|
|
|
|
}
|
1999-11-22 16:12:07 +03:00
|
|
|
LUA_ASSERT(L, VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
1999-10-04 21:50:24 +04:00
|
|
|
}
|
|
|
|
|