Skip to content

Commit 0df6635

Browse files
committed
"fixed" objects kept in a separated list (instead of being kept in
'allgc' list with a bit marking them)
1 parent ae80065 commit 0df6635

File tree

7 files changed

+33
-17
lines changed

7 files changed

+33
-17
lines changed

lgc.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.c,v 2.148 2013/08/20 17:46:34 roberto Exp roberto $
2+
** $Id: lgc.c,v 2.149 2013/08/21 19:21:16 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -189,6 +189,16 @@ void luaC_checkupvalcolor (global_State *g, UpVal *uv) {
189189
}
190190

191191

192+
void luaC_fix (lua_State *L, GCObject *o) {
193+
global_State *g = G(L);
194+
lua_assert(g->allgc == o);
195+
white2gray(o);
196+
g->allgc = o->gch.next; /* remove object from 'allgc' list */
197+
o->gch.next = g->fixedgc; /* link it to 'fixedgc' list */
198+
g->fixedgc = o;
199+
}
200+
201+
192202
/*
193203
** create a new collectable object (with given type and size) and link
194204
** it to '*list'. 'offset' tells how many bytes to allocate before the
@@ -927,6 +937,7 @@ void luaC_freeallobjects (lua_State *L) {
927937
g->gckind = KGC_NORMAL;
928938
sweepwholelist(L, &g->finobj); /* finalizers can create objs. in 'finobj' */
929939
sweepwholelist(L, &g->allgc);
940+
sweepwholelist(L, &g->fixedgc); /* collect fixed objects */
930941
lua_assert(g->strt.nuse == 0);
931942
}
932943

lgc.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.h,v 2.63 2013/08/20 17:46:34 roberto Exp roberto $
2+
** $Id: lgc.h,v 2.64 2013/08/21 19:21:16 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -76,8 +76,7 @@
7676
#define WHITE1BIT 1 /* object is white (type 1) */
7777
#define BLACKBIT 2 /* object is black */
7878
#define FINALIZEDBIT 3 /* object has been marked for finalization */
79-
#define FIXEDBIT 4 /* object is fixed (should not be collected) */
80-
#define LOCALBIT 5 /* object is not local */
79+
#define LOCALBIT 4 /* object is not local */
8180
/* bit 7 is currently used by tests (luaL_checkmemory) */
8281

8382
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
@@ -127,6 +126,7 @@
127126
{ if (nolocal(obj2gco(o)), isblack(obj2gco(p)) && iswhite(obj2gco(o))) \
128127
luaC_barrierback_(L,p); }
129128

129+
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
130130
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
131131
LUAI_FUNC void luaC_step (lua_State *L);
132132
LUAI_FUNC void luaC_forcestep (lua_State *L);

llex.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: llex.c,v 2.66 2013/05/14 15:59:04 roberto Exp roberto $
2+
** $Id: llex.c,v 2.67 2013/06/19 14:27:00 roberto Exp roberto $
33
** Lexical Analyzer
44
** See Copyright Notice in lua.h
55
*/
@@ -15,6 +15,7 @@
1515

1616
#include "lctype.h"
1717
#include "ldo.h"
18+
#include "lgc.h"
1819
#include "llex.h"
1920
#include "lobject.h"
2021
#include "lparser.h"
@@ -64,9 +65,11 @@ static void save (LexState *ls, int c) {
6465

6566
void luaX_init (lua_State *L) {
6667
int i;
68+
TString *e = luaS_new(L, LUA_ENV); /* create env name */
69+
luaC_fix(L, obj2gco(e)); /* never collect this name */
6770
for (i=0; i<NUM_RESERVED; i++) {
6871
TString *ts = luaS_new(L, luaX_tokens[i]);
69-
luaS_fix(ts); /* reserved words are never collected */
72+
luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */
7073
ts->tsv.extra = cast_byte(i+1); /* reserved word */
7174
}
7275
}
@@ -163,8 +166,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
163166
ls->linenumber = 1;
164167
ls->lastline = 1;
165168
ls->source = source;
166-
ls->envn = luaS_new(L, LUA_ENV); /* create env name */
167-
luaS_fix(ls->envn); /* never collect this name */
169+
ls->envn = luaS_new(L, LUA_ENV); /* get env name */
168170
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
169171
}
170172

lstate.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstate.c,v 2.102 2013/08/16 18:55:49 roberto Exp roberto $
2+
** $Id: lstate.c,v 2.103 2013/08/21 19:21:16 roberto Exp roberto $
33
** Global State
44
** See Copyright Notice in lua.h
55
*/
@@ -188,7 +188,7 @@ static void f_luaopen (lua_State *L, void *ud) {
188188
luaX_init(L);
189189
/* pre-create memory-error message */
190190
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
191-
luaS_fix(g->memerrmsg); /* it should never be collected */
191+
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
192192
g->gcrunning = 1; /* allow gc */
193193
}
194194

@@ -270,7 +270,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
270270
g = &l->g;
271271
L->next = NULL;
272272
L->tt = LUA_TTHREAD;
273-
g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
273+
g->currentwhite = bitmask(WHITE0BIT);
274274
L->marked = luaC_white(g) | bitmask(LOCALBIT);
275275
g->gckind = KGC_NORMAL;
276276
preinit_state(L, g);
@@ -290,6 +290,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
290290
g->allgc = NULL;
291291
g->finobj = NULL;
292292
g->tobefnz = NULL;
293+
g->fixedgc = NULL;
293294
g->sweepgc = g->sweepfin = NULL;
294295
g->gray = g->grayagain = NULL;
295296
g->weak = g->ephemeron = g->allweak = NULL;

lstate.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstate.h,v 2.85 2013/08/20 17:46:34 roberto Exp roberto $
2+
** $Id: lstate.h,v 2.86 2013/08/21 19:21:16 roberto Exp roberto $
33
** Global State
44
** See Copyright Notice in lua.h
55
*/
@@ -24,6 +24,9 @@
2424
** at the end of the 'allgc' list, after the 'l_registry' (which is
2525
** the first object to be added to the list).
2626
**
27+
** List 'fixedgc' keep objects that are not to be collected (currently
28+
** only small strings, such as reserved words).
29+
**
2730
** Open upvalues are not subject to independent garbage collection. They
2831
** are collected together with their respective threads. (They are
2932
** always gray, so they must be remarked in the atomic step. Usually
@@ -132,6 +135,7 @@ typedef struct global_State {
132135
GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
133136
GCObject *allweak; /* list of all-weak tables */
134137
GCObject *tobefnz; /* list of userdata to be GC */
138+
GCObject *fixedgc; /* list of objects not to be collected */
135139
Mbuffer buff; /* temporary buffer for string concatenation */
136140
int gcpause; /* size of pause between successive GCs */
137141
int gcstepmul; /* GC `granularity' */

lstring.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstring.h,v 1.50 2013/08/16 18:55:49 roberto Exp roberto $
2+
** $Id: lstring.h,v 1.51 2013/08/21 19:21:16 roberto Exp roberto $
33
** String table (keep all strings handled by Lua)
44
** See Copyright Notice in lua.h
55
*/
@@ -19,8 +19,6 @@
1919
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
2020
(sizeof(s)/sizeof(char))-1))
2121

22-
#define luaS_fix(s) setbits((s)->tsv.marked, bit2mask(FIXEDBIT, LOCALBIT))
23-
2422

2523
/*
2624
** test whether a string is a reserved word

ltm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltm.c,v 2.19 2013/04/29 16:56:50 roberto Exp roberto $
2+
** $Id: ltm.c,v 2.20 2013/05/06 17:19:11 roberto Exp roberto $
33
** Tag methods
44
** See Copyright Notice in lua.h
55
*/
@@ -43,7 +43,7 @@ void luaT_init (lua_State *L) {
4343
int i;
4444
for (i=0; i<TM_N; i++) {
4545
G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
46-
luaS_fix(G(L)->tmname[i]); /* never collect these names */
46+
luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
4747
}
4848
}
4949

0 commit comments

Comments
 (0)