Skip to content

Commit b9570b3

Browse files
committed
Phase 122: Fix collectable bit in LuaT (WIP)
Fixed major issue: All GC objects must be created with ctb(LuaT::*) to include the collectable bit. Updated: - All object creation sites (Table, Udata, TString, functions, etc.) - Main thread creation in lua_newstate - Type comparisons in assertions and conditionals - Switch statements in gc_core.cpp (partial) Remaining work: - Fix 7 more switch statements on getType() in: * gc_marking.cpp (2 switches) * lgc.cpp (2 switches) * ltests.cpp (2 switches) * gc_core.cpp (1 more switch) All switches need: static_cast<int>(getType()) and static_cast<int>(ctb(LuaT::*)) for case labels.
1 parent cb6178c commit b9570b3

File tree

10 files changed

+35
-35
lines changed

10 files changed

+35
-35
lines changed

src/core/ldebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
/* Both CClosure and LClosure have tt at same offset (from GCBase) */
36-
#define LuaClosure(f) ((f) != nullptr && (f)->c.getType() == LuaT::LCL)
36+
#define LuaClosure(f) ((f) != nullptr && (f)->c.getType() == ctb(LuaT::LCL))
3737

3838
static const char strlocal[] = "local";
3939
static const char strupval[] = "upvalue";

src/core/lstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
272272
lua_lock(L);
273273
luaC_checkGC(L);
274274
/* create new thread */
275-
o = luaC_newobjdt(L, LuaT::THREAD, sizeof(LX), lxOffset());
275+
o = luaC_newobjdt(L, ctb(LuaT::THREAD), sizeof(LX), lxOffset());
276276
L1 = gco2th(o);
277277
/* anchor it on L stack */
278278
setthvalue2s(L, L->getTop().p, L1);
@@ -335,7 +335,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
335335
(*f)(ud, nullptr, LUA_TTHREAD, sizeof(global_State)));
336336
if (g == nullptr) return nullptr;
337337
L = &g->getMainThread()->l;
338-
L->setType(LuaT::THREAD);
338+
L->setType(ctb(LuaT::THREAD));
339339
g->setCurrentWhite(bitmask(WHITE0BIT));
340340
L->setMarked(g->getWhite());
341341
preinit_thread(L, g);

src/memory/gc/gc_core.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,45 @@
2424
*/
2525
l_mem GCCore::objsize(GCObject* o) {
2626
lu_mem res;
27-
switch (o->getType()) {
28-
case LuaT::TABLE: {
27+
switch (static_cast<int>(o->getType())) {
28+
case static_cast<int>(ctb(LuaT::TABLE)): {
2929
res = luaH_size(gco2t(o));
3030
break;
3131
}
32-
case LuaT::LCL: {
32+
case static_cast<int>(ctb(LuaT::LCL)): {
3333
LClosure* cl = gco2lcl(o);
3434
res = sizeLclosure(cl->getNumUpvalues());
3535
break;
3636
}
37-
case LuaT::CCL: {
37+
case static_cast<int>(ctb(LuaT::CCL)): {
3838
CClosure* cl = gco2ccl(o);
3939
res = sizeCclosure(cl->getNumUpvalues());
4040
break;
4141
}
42-
case LuaT::USERDATA: {
42+
case static_cast<int>(ctb(LuaT::USERDATA)): {
4343
Udata* u = gco2u(o);
4444
res = sizeudata(u->getNumUserValues(), u->getLen());
4545
break;
4646
}
47-
case LuaT::PROTO: {
47+
case static_cast<int>(ctb(LuaT::PROTO)): {
4848
res = gco2p(o)->memorySize();
4949
break;
5050
}
51-
case LuaT::THREAD: {
51+
case static_cast<int>(ctb(LuaT::THREAD)): {
5252
res = luaE_threadsize(gco2th(o));
5353
break;
5454
}
55-
case LuaT::SHRSTR: {
55+
case static_cast<int>(ctb(LuaT::SHRSTR)): {
5656
TString* ts = gco2ts(o);
5757
res = sizestrshr(cast_uint(ts->getShrlen()));
5858
break;
5959
}
60-
case LuaT::LNGSTR: {
60+
case static_cast<int>(ctb(LuaT::LNGSTR)): {
6161
TString* ts = gco2ts(o);
6262
res = TString::calculateLongStringSize(ts->getLnglen(), ts->getShrlen());
6363
break;
6464
}
65-
case LuaT::UPVAL: {
65+
case static_cast<int>(ctb(LuaT::UPVAL)): {
6666
res = sizeof(UpVal);
6767
break;
6868
}

src/memory/gc/gc_sweeping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ void GCSweeping::sweep2old(lua_State* L, GCObject** p) {
137137
else { /* all surviving objects become old */
138138
setage(curr, GCAge::Old);
139139

140-
if (curr->getType() == LuaT::THREAD) { /* threads must be watched */
140+
if (curr->getType() == ctb(LuaT::THREAD)) { /* threads must be watched */
141141
lua_State* th = gco2th(curr);
142142
linkgclistThread(th, *g->getGrayAgainPtr()); /* insert into 'grayagain' list */
143143
}
144-
else if (curr->getType() == LuaT::UPVAL && gco2upv(curr)->isOpen())
144+
else if (curr->getType() == ctb(LuaT::UPVAL) && gco2upv(curr)->isOpen())
145145
set2gray(curr); /* open upvalues are always gray */
146146
else /* everything else is black */
147147
nw2black(curr);

src/memory/lgc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static GCObject **correctgraylist (GCObject **p) {
589589
setage(curr, GCAge::Touched2);
590590
goto remain; /* keep it in the list and go to next element */
591591
}
592-
else if (curr->getType() == LuaT::THREAD) {
592+
else if (curr->getType() == ctb(LuaT::THREAD)) {
593593
lua_assert(isgray(curr));
594594
goto remain; /* keep non-white threads on the list */
595595
}

src/objects/lfunc.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ CClosure::CClosure(int nupvals) {
3636
// Factory method
3737
CClosure* CClosure::create(lua_State* L, int nupvals) {
3838
size_t extra = (nupvals > 0) ? (static_cast<size_t>(nupvals) - 1) * sizeof(TValue) : 0;
39-
CClosure* c = new (L, LuaT::CCL, extra) CClosure(nupvals);
39+
CClosure* c = new (L, ctb(LuaT::CCL), extra) CClosure(nupvals);
4040
return c;
4141
}
4242

@@ -58,7 +58,7 @@ LClosure* LClosure::create(lua_State* L, int nupvals) {
5858
// So extra = sizeLclosure(nupvals) - sizeof(LClosure)
5959
size_t total_size = sizeLclosure(nupvals);
6060
size_t extra = total_size - sizeof(LClosure);
61-
LClosure* c = new (L, LuaT::LCL, extra) LClosure(nupvals);
61+
LClosure* c = new (L, ctb(LuaT::LCL), extra) LClosure(nupvals);
6262
return c;
6363
}
6464

@@ -71,7 +71,7 @@ void LClosure::initUpvals(lua_State* L) {
7171
int i;
7272
for (i = 0; i < nupvalues; i++) {
7373
// Use placement new - calls constructor (initializes to closed nil upvalue)
74-
UpVal *uv = new (L, LuaT::UPVAL) UpVal();
74+
UpVal *uv = new (L, ctb(LuaT::UPVAL)) UpVal();
7575
uv->setVP(uv->getValueSlot()); /* make it closed */
7676
// Constructor already sets value to nil, but keeping setnilvalue for clarity
7777
setnilvalue(uv->getVP());
@@ -88,7 +88,7 @@ void LClosure::initUpvals(lua_State* L) {
8888
**/
8989
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
9090
// Use placement new - calls constructor
91-
UpVal *uv = new (L, LuaT::UPVAL) UpVal();
91+
UpVal *uv = new (L, ctb(LuaT::UPVAL)) UpVal();
9292
UpVal *next = *prev;
9393
uv->setVP(s2v(level)); /* current value lives in the stack */
9494
uv->setOpenNext(next); /* link it to list of open upvalues */
@@ -271,7 +271,7 @@ StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
271271
// Phase 50: Use placement new to call constructor
272272
Proto *luaF_newproto (lua_State *L) {
273273
// Use placement new - calls constructor which initializes all fields to safe defaults
274-
Proto *f = new (L, LuaT::PROTO) Proto();
274+
Proto *f = new (L, ctb(LuaT::PROTO)) Proto();
275275
// Constructor handles all initialization
276276
return f;
277277
}

src/objects/lstring.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static TString *createstrobj (lua_State *L, size_t totalsize, LuaT tag,
206206

207207
TString* TString::createLongString(lua_State* L, size_t l) {
208208
size_t totalsize = calculateLongStringSize(l, LSTRREG);
209-
TString *ts = createstrobj(L, totalsize, LuaT::LNGSTR, G(L)->getSeed());
209+
TString *ts = createstrobj(L, totalsize, ctb(LuaT::LNGSTR), G(L)->getSeed());
210210
ts->setLnglen(l);
211211
ts->setShrlen(LSTRREG); /* signals that it is a regular long string */
212212
ts->setContents(cast_charp(ts) + tstringFallocOffset());
@@ -254,7 +254,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
254254
list = &tb->getHash()[lmod(h, tb->getSize())]; /* rehash with new size */
255255
}
256256
size_t allocsize = sizestrshr(l);
257-
ts = createstrobj(L, allocsize, LuaT::SHRSTR, h);
257+
ts = createstrobj(L, allocsize, ctb(LuaT::SHRSTR), h);
258258
ts->setShrlen(static_cast<ls_byte>(l));
259259
getshrstr(ts)[l] = '\0'; /* ending 0 */
260260
std::copy_n(str, l, getshrstr(ts));
@@ -311,7 +311,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
311311
size_t totalsize = sizeudata(nuvalue, s);
312312

313313
// Allocate exactly what we need (may be less than sizeof(Udata) for small data)
314-
GCObject *o = luaC_newobj(L, LuaT::USERDATA, totalsize);
314+
GCObject *o = luaC_newobj(L, ctb(LuaT::USERDATA), totalsize);
315315
u = gco2u(o);
316316

317317
// Manually initialize fields (can't use constructor reliably for variable-size objects)
@@ -343,7 +343,7 @@ struct NewExt {
343343
static void f_newext (lua_State *L, void *ud) {
344344
NewExt *ne = static_cast<NewExt*>(ud);
345345
size_t size = TString::calculateLongStringSize(0, ne->kind);
346-
ne->ts = createstrobj(L, size, LuaT::LNGSTR, G(L)->getSeed());
346+
ne->ts = createstrobj(L, size, ctb(LuaT::LNGSTR), G(L)->getSeed());
347347
}
348348

349349

@@ -375,7 +375,7 @@ TString* TString::createExternal(lua_State* L, const char* s, size_t len,
375375
*/
376376

377377
unsigned TString::hashLongStr() {
378-
lua_assert(getType() == LuaT::LNGSTR);
378+
lua_assert(getType() == ctb(LuaT::LNGSTR));
379379
if (getExtra() == 0) { /* no hash? */
380380
size_t len = getLnglen();
381381
setHash(computeHash(getlngstr(this), len, getHash()));

src/objects/lstring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ inline bool isreserved(const TString* s) noexcept {
302302
** equality for short strings, which are always internalized
303303
*/
304304
inline bool eqshrstr(const TString* a, const TString* b) noexcept {
305-
return check_exp((a)->getType() == LuaT::SHRSTR, (a) == (b));
305+
return check_exp((a)->getType() == ctb(LuaT::SHRSTR), (a) == (b));
306306
}
307307

308308

src/objects/ltable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ lua_Unsigned Table::getn(lua_State* L) {
15781578
// Phase 50: Factory pattern with placement new operator
15791579
Table* Table::create(lua_State* L) {
15801580
// Use placement new operator - calls constructor from lobject.h
1581-
Table *t = new (L, LuaT::TABLE) Table();
1581+
Table *t = new (L, ctb(LuaT::TABLE)) Table();
15821582

15831583
// Set non-default values
15841584
t->setFlags(maskflags); /* table has no metamethod fields */

src/testing/ltests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ static void printobj (global_State *g, GCObject *o) {
326326
ttypename(novariant(o->getType())), (void *)o,
327327
isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
328328
"ns01oTt"[static_cast<size_t>(getage(o))], o->getMarked());
329-
if (o->getType() == LuaT::SHRSTR || o->getType() == LuaT::LNGSTR)
329+
if (o->getType() == ctb(LuaT::SHRSTR) || o->getType() == ctb(LuaT::LNGSTR))
330330
printf(" '%s'", getstr(gco2ts(o)));
331331
}
332332

@@ -582,8 +582,8 @@ static void checkobject (global_State *g, GCObject *o, int maybedead,
582582
assert(isblack(o) ||
583583
getage(o) == GCAge::Touched1 ||
584584
getage(o) == GCAge::Old0 ||
585-
o->getType() == LuaT::THREAD ||
586-
(o->getType() == LuaT::UPVAL && gco2upv(o)->isOpen()));
585+
o->getType() == ctb(LuaT::THREAD) ||
586+
(o->getType() == ctb(LuaT::UPVAL) && gco2upv(o)->isOpen()));
587587
}
588588
assert(getage(o) != GCAge::Touched1 || isgray(o));
589589
}
@@ -641,7 +641,7 @@ static l_mem checkgrays (global_State *g) {
641641
static void incifingray (global_State *g, GCObject *o, l_mem *count) {
642642
if (!g->keepInvariant())
643643
return; /* gray lists not being kept in these phases */
644-
if (o->getType() == LuaT::UPVAL) {
644+
if (o->getType() == ctb(LuaT::UPVAL)) {
645645
/* only open upvalues can be gray */
646646
assert(!isgray(o) || gco2upv(o)->isOpen());
647647
return; /* upvalues are never in gray lists */
@@ -699,7 +699,7 @@ int lua_checkmemory (lua_State *L) {
699699

700700
/* check 'fixedgc' list */
701701
for (o = g->getFixedGC(); o != nullptr; o = o->getNext()) {
702-
assert(o->getType() == LuaT::SHRSTR && isgray(o) && getage(o) == GCAge::Old);
702+
assert(o->getType() == ctb(LuaT::SHRSTR) && isgray(o) && getage(o) == GCAge::Old);
703703
}
704704

705705
/* check 'allgc' list */
@@ -716,7 +716,7 @@ int lua_checkmemory (lua_State *L) {
716716
checkobject(g, o, 0, GCAge::New);
717717
incifingray(g, o, &totalshould);
718718
assert(tofinalize(o));
719-
assert(o->getType() == LuaT::USERDATA || o->getType() == LuaT::TABLE);
719+
assert(o->getType() == ctb(LuaT::USERDATA) || o->getType() == ctb(LuaT::TABLE));
720720
}
721721
if (g->keepInvariant())
722722
assert(totalin == totalshould);
@@ -1089,7 +1089,7 @@ static int hash_query (lua_State *L) {
10891089
TString *ts;
10901090
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
10911091
ts = tsvalue(obj_at(L, 1));
1092-
if (ts->getType() == LuaT::LNGSTR)
1092+
if (ts->getType() == ctb(LuaT::LNGSTR))
10931093
ts->hashLongStr(); /* make sure long string has a hash */
10941094
lua_pushinteger(L, cast_int(ts->getHash()));
10951095
}

0 commit comments

Comments
 (0)