aboutsummaryrefslogtreecommitdiff
path: root/lref.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-28 14:57:04 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-08-28 14:57:04 -0300
commit9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761 (patch)
treeda8d97d954e5ffabf9ff275df725f1e0a3a5b3e6 /lref.c
parentf1fd9b5c2c21f24d25d7813f431a3495702ebea6 (diff)
downloadlua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.gz
lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.bz2
lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.zip
first version for new API
Diffstat (limited to 'lref.c')
-rw-r--r--lref.c118
1 files changed, 0 insertions, 118 deletions
diff --git a/lref.c b/lref.c
deleted file mode 100644
index c3fc57af..00000000
--- a/lref.c
+++ /dev/null
@@ -1,118 +0,0 @@
1/*
2** $Id: lref.c,v 1.17 2000/08/09 19:16:57 roberto Exp roberto $
3** reference mechanism
4** See Copyright Notice in lua.h
5*/
6
7
8#include "lua.h"
9
10#include "lapi.h"
11#include "ldo.h"
12#include "lmem.h"
13#include "lref.h"
14#include "lstate.h"
15
16
17int lua_ref (lua_State *L, int lock) {
18 int ref;
19 luaA_checkCargs(L, 1);
20 if (ttype(L->top-1) == TAG_NIL)
21 ref = LUA_REFNIL;
22 else {
23 if (L->refFree != NONEXT) { /* is there a free place? */
24 ref = L->refFree;
25 L->refFree = L->refArray[ref].st;
26 }
27 else { /* no more free places */
28 luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
29 "reference table overflow", MAX_INT);
30 ref = L->refSize++;
31 }
32 L->refArray[ref].o = *(L->top-1);
33 L->refArray[ref].st = lock ? LOCK : HOLD;
34 }
35 L->top--;
36 return ref;
37}
38
39
40void lua_unref (lua_State *L, int ref) {
41 if (ref >= 0) {
42 if (ref >= L->refSize || L->refArray[ref].st >= 0)
43 lua_error(L, "Lua API error - "
44 "invalid argument for function `lua_unref'");
45 L->refArray[ref].st = L->refFree;
46 L->refFree = ref;
47 }
48}
49
50
51int lua_pushref (lua_State *L, int ref) {
52 if (ref == LUA_REFNIL)
53 ttype(L->top) = TAG_NIL;
54 else if (0 <= ref && ref < L->refSize &&
55 (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
56 *L->top = L->refArray[ref].o;
57 else
58 return 0;
59 incr_top;
60 return 1;
61}
62
63
64void lua_beginblock (lua_State *L) {
65 luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
66 "too many nested blocks", L->stacksize);
67 L->Cblocks[L->numCblocks] = L->Cstack;
68 L->numCblocks++;
69}
70
71
72void lua_endblock (lua_State *L) {
73 if (L->numCblocks <= 0)
74 lua_error(L, "Lua API error - no block to end");
75 --L->numCblocks;
76 L->Cstack = L->Cblocks[L->numCblocks];
77 L->top = L->Cstack.base;
78}
79
80
81
82
83
84
85static int hasmark (const TObject *o) {
86 /* valid only for locked objects */
87 switch (o->ttype) {
88 case TAG_STRING: case TAG_USERDATA:
89 return tsvalue(o)->marked;
90 case TAG_TABLE:
91 return ismarked(hvalue(o));
92 case TAG_LCLOSURE: case TAG_CCLOSURE:
93 return ismarked(clvalue(o)->mark);
94 default: /* number */
95 return 1;
96 }
97}
98
99
100/* for internal debugging only; check if a link of free refs is valid */
101#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
102
103void luaR_invalidaterefs (lua_State *L) {
104 int n = L->refSize;
105 int i;
106 for (i=0; i<n; i++) {
107 struct Ref *r = &L->refArray[i];
108 if (r->st == HOLD && !hasmark(&r->o))
109 r->st = COLLECTED;
110 LUA_ASSERT((r->st == LOCK && hasmark(&r->o)) ||
111 r->st == COLLECTED ||
112 r->st == NONEXT ||
113 (r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
114 "inconsistent ref table");
115 }
116 LUA_ASSERT(VALIDLINK(L, L->refFree, n), "inconsistent ref table");
117}
118