Skip to content

Commit ca41b43

Browse files
committed
type 'TString' refers directly to the structure inside the union
(union used only for size purposes)
1 parent 3511e18 commit ca41b43

13 files changed

+102
-104
lines changed

ldump.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ldump.c,v 2.31 2014/06/18 13:54:31 roberto Exp roberto $
2+
** $Id: ldump.c,v 2.32 2014/06/18 18:35:43 roberto Exp roberto $
33
** save precompiled Lua chunks
44
** See Copyright Notice in lua.h
55
*/
@@ -71,7 +71,7 @@ static void DumpString (const TString *s, DumpState *D) {
7171
if (s == NULL)
7272
DumpByte(0, D);
7373
else {
74-
size_t size = s->tsv.len + 1; /* include trailing '\0' */
74+
size_t size = s->len + 1; /* include trailing '\0' */
7575
if (size < 0xFF)
7676
DumpByte(cast_int(size), D);
7777
else {
@@ -112,7 +112,7 @@ static void DumpConstants (const Proto *f, DumpState *D) {
112112
break;
113113
case LUA_TSHRSTR:
114114
case LUA_TLNGSTR:
115-
DumpString(rawtsvalue(o), D);
115+
DumpString(tsvalue(o), D);
116116
break;
117117
default:
118118
lua_assert(0);

lgc.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.c,v 2.185 2014/07/17 17:27:49 roberto Exp roberto $
2+
** $Id: lgc.c,v 2.186 2014/07/18 12:17:54 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -83,9 +83,6 @@
8383
#define markobject(g,t) \
8484
{ if ((t) && iswhite(obj2gco(t))) reallymarkobject(g, obj2gco(t)); }
8585

86-
#define markstring(g,t) \
87-
{ if ((t) && iswhite(ts2gco(t))) reallymarkobject(g, ts2gco(t)); }
88-
8986
static void reallymarkobject (global_State *g, GCObject *o);
9087

9188

@@ -451,15 +448,15 @@ static int traverseproto (global_State *g, Proto *f) {
451448
int i;
452449
if (f->cache && iswhite(obj2gco(f->cache)))
453450
f->cache = NULL; /* allow cache to be collected */
454-
markstring(g, f->source);
451+
markobject(g, f->source);
455452
for (i = 0; i < f->sizek; i++) /* mark literals */
456453
markvalue(g, &f->k[i]);
457454
for (i = 0; i < f->sizeupvalues; i++) /* mark upvalue names */
458-
markstring(g, f->upvalues[i].name);
455+
markobject(g, f->upvalues[i].name);
459456
for (i = 0; i < f->sizep; i++) /* mark nested protos */
460457
markobject(g, f->p[i]);
461458
for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */
462-
markstring(g, f->locvars[i].varname);
459+
markobject(g, f->locvars[i].varname);
463460
return sizeof(Proto) + sizeof(Instruction) * f->sizecode +
464461
sizeof(Proto *) * f->sizep +
465462
sizeof(TValue) * f->sizek +
@@ -702,7 +699,7 @@ static void freeobj (lua_State *L, GCObject *o) {
702699
case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break;
703700
case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break;
704701
case LUA_TSHRSTR:
705-
luaS_remove(L, rawgco2ts(o)); /* remove it from hash table */
702+
luaS_remove(L, gco2ts(o)); /* remove it from hash table */
706703
/* go through */
707704
case LUA_TLNGSTR: {
708705
luaM_freemem(L, o, sizestring(gco2ts(o)));

llex.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: llex.c,v 2.78 2014/05/21 15:22:02 roberto Exp roberto $
2+
** $Id: llex.c,v 2.79 2014/07/18 12:17:54 roberto Exp roberto $
33
** Lexical Analyzer
44
** See Copyright Notice in lua.h
55
*/
@@ -67,11 +67,11 @@ static void save (LexState *ls, int c) {
6767
void luaX_init (lua_State *L) {
6868
int i;
6969
TString *e = luaS_new(L, LUA_ENV); /* create env name */
70-
luaC_fix(L, ts2gco(e)); /* never collect this name */
70+
luaC_fix(L, obj2gco(e)); /* never collect this name */
7171
for (i=0; i<NUM_RESERVED; i++) {
7272
TString *ts = luaS_new(L, luaX_tokens[i]);
73-
luaC_fix(L, ts2gco(ts)); /* reserved words are never collected */
74-
ts->tsv.extra = cast_byte(i+1); /* reserved word */
73+
luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */
74+
ts->extra = cast_byte(i+1); /* reserved word */
7575
}
7676
}
7777

@@ -137,7 +137,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
137137
luaC_checkGC(L);
138138
}
139139
else { /* string already present */
140-
ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */
140+
ts = tsvalue(keyfromval(o)); /* re-use value previously stored */
141141
}
142142
L->top--; /* remove string from stack */
143143
return ts;
@@ -565,7 +565,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
565565
luaZ_bufflen(ls->buff));
566566
seminfo->ts = ts;
567567
if (isreserved(ts)) /* reserved word? */
568-
return ts->tsv.extra - 1 + FIRST_RESERVED;
568+
return ts->extra - 1 + FIRST_RESERVED;
569569
else {
570570
return TK_NAME;
571571
}

lobject.h

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lobject.h,v 2.96 2014/07/17 17:27:49 roberto Exp roberto $
2+
** $Id: lobject.h,v 2.97 2014/07/18 12:17:54 roberto Exp roberto $
33
** Type definitions for Lua objects
44
** See Copyright Notice in lua.h
55
*/
@@ -156,8 +156,7 @@ typedef struct lua_TValue TValue;
156156
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
157157
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
158158
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
159-
#define rawtsvalue(o) check_exp(ttisstring(o), rawgco2ts(val_(o).gc))
160-
#define tsvalue(o) (&rawtsvalue(o)->tsv)
159+
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
161160
#define rawuvalue(o) check_exp(ttisfulluserdata(o), rawgco2u(val_(o).gc))
162161
#define uvalue(o) (&rawuvalue(o)->uv)
163162
#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
@@ -210,7 +209,7 @@ typedef struct lua_TValue TValue;
210209

211210
#define setsvalue(L,obj,x) \
212211
{ TValue *io = (obj); TString *x_ = (x); \
213-
val_(io).gc = obj2gco(&x_->tsv); settt_(io, ctb(x_->tsv.tt)); \
212+
val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
214213
checkliveness(G(L),io); }
215214

216215
#define setuvalue(L,obj,x) \
@@ -299,24 +298,36 @@ typedef TValue *StkId; /* index to stack elements */
299298

300299
/*
301300
** Header for string value; string bytes follow the end of this structure
301+
** (aligned according to 'UTString'; see next).
302302
*/
303-
typedef union TString {
304-
L_Umaxalign dummy; /* ensures maximum alignment for strings */
305-
struct {
306-
CommonHeader;
307-
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
308-
unsigned int hash;
309-
size_t len; /* number of characters in string */
310-
union TString *hnext; /* linked list for hash table */
311-
} tsv;
303+
typedef struct TString {
304+
CommonHeader;
305+
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
306+
unsigned int hash;
307+
size_t len; /* number of characters in string */
308+
struct TString *hnext; /* linked list for hash table */
312309
} TString;
313310

314311

315-
/* get the actual string (array of bytes) from a TString */
316-
#define getstr(ts) cast(const char *, (ts) + 1)
312+
/*
313+
** Ensures that address after this type is always fully aligned.
314+
*/
315+
typedef union UTString {
316+
L_Umaxalign dummy; /* ensures maximum alignment for strings */
317+
TString tsv;
318+
} UTString;
319+
320+
321+
/*
322+
** Get the actual string (array of bytes) from a 'TString'.
323+
** (Access to 'extra' ensures that value is really a 'TString'.)
324+
*/
325+
#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
326+
#define getstr(ts) \
327+
((void)(ts)->extra, cast(const char*, getaddrstr(ts)))
317328

318329
/* get the actual string (array of bytes) from a Lua value */
319-
#define svalue(o) getstr(rawtsvalue(o))
330+
#define svalue(o) getstr(tsvalue(o))
320331

321332

322333
/*

lparser.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lparser.c,v 2.139 2014/06/19 18:27:20 roberto Exp roberto $
2+
** $Id: lparser.c,v 2.140 2014/07/18 12:17:54 roberto Exp roberto $
33
** Lua Parser
44
** See Copyright Notice in lua.h
55
*/
@@ -164,7 +164,7 @@ static int registerlocalvar (LexState *ls, TString *varname) {
164164
LocVar, SHRT_MAX, "local variables");
165165
while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
166166
f->locvars[fs->nlocvars].varname = varname;
167-
luaC_objbarrier(ls->L, f, ts2gco(varname));
167+
luaC_objbarrier(ls->L, f, obj2gco(varname));
168168
return fs->nlocvars++;
169169
}
170170

@@ -232,7 +232,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
232232
f->upvalues[fs->nups].instack = (v->k == VLOCAL);
233233
f->upvalues[fs->nups].idx = cast_byte(v->u.info);
234234
f->upvalues[fs->nups].name = name;
235-
luaC_objbarrier(fs->ls->L, f, ts2gco(name));
235+
luaC_objbarrier(fs->ls->L, f, obj2gco(name));
236236
return fs->nups++;
237237
}
238238

@@ -1630,7 +1630,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
16301630
incr_top(L);
16311631
funcstate.f = cl->p = luaF_newproto(L);
16321632
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
1633-
luaC_objbarrier(L, funcstate.f, ts2gco(funcstate.f->source));
1633+
luaC_objbarrier(L, funcstate.f, obj2gco(funcstate.f->source));
16341634
lexstate.buff = buff;
16351635
lexstate.dyd = dyd;
16361636
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;

lstate.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstate.c,v 2.121 2014/02/18 13:46:26 roberto Exp roberto $
2+
** $Id: lstate.c,v 2.122 2014/07/18 12:17:54 roberto Exp roberto $
33
** Global State
44
** See Copyright Notice in lua.h
55
*/
@@ -206,7 +206,7 @@ static void f_luaopen (lua_State *L, void *ud) {
206206
luaX_init(L);
207207
/* pre-create memory-error message */
208208
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
209-
luaC_fix(L, ts2gco(g->memerrmsg)); /* it should never be collected */
209+
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
210210
g->gcrunning = 1; /* allow gc */
211211
g->version = lua_version(NULL);
212212
luai_userstateopen(L);

lstate.h

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstate.h,v 2.110 2014/07/17 17:27:49 roberto Exp roberto $
2+
** $Id: lstate.h,v 2.111 2014/07/18 12:17:54 roberto Exp roberto $
33
** Global State
44
** See Copyright Notice in lua.h
55
*/
@@ -174,7 +174,7 @@ struct lua_State {
174174
*/
175175
union GCUnion {
176176
GCObject gc; /* common header */
177-
union TString ts;
177+
struct TString ts;
178178
union Udata u;
179179
union Closure cl;
180180
struct Table h;
@@ -186,9 +186,8 @@ union GCUnion {
186186
#define cast_u(o) cast(union GCUnion *, (o))
187187

188188
/* macros to convert a GCObject into a specific value */
189-
#define rawgco2ts(o) \
189+
#define gco2ts(o) \
190190
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
191-
#define gco2ts(o) (&rawgco2ts(o)->tsv)
192191
#define rawgco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
193192
#define gco2u(o) (&rawgco2u(o)->uv)
194193
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
@@ -204,13 +203,6 @@ union GCUnion {
204203
#define obj2gco(v) \
205204
check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
206205

207-
/*
208-
** macro to convert a TString into a GCObject.
209-
** (TString is a union, and therefore needs an access slightly different
210-
** from the other objects.)
211-
*/
212-
#define ts2gco(v) (obj2gco(&(v)->tsv))
213-
214206

215207
/* actual number of total bytes allocated */
216208
#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)

lstring.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstring.c,v 2.40 2014/06/18 22:59:29 roberto Exp roberto $
2+
** $Id: lstring.c,v 2.41 2014/07/18 12:17:54 roberto Exp roberto $
33
** String table (keeps all strings handled by Lua)
44
** See Copyright Notice in lua.h
55
*/
@@ -34,10 +34,10 @@
3434
** equality for long strings
3535
*/
3636
int luaS_eqlngstr (TString *a, TString *b) {
37-
size_t len = a->tsv.len;
38-
lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
37+
size_t len = a->len;
38+
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
3939
return (a == b) || /* same instance or... */
40-
((len == b->tsv.len) && /* equal length and ... */
40+
((len == b->len) && /* equal length and ... */
4141
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
4242
}
4343

@@ -67,9 +67,9 @@ void luaS_resize (lua_State *L, int newsize) {
6767
TString *p = tb->hash[i];
6868
tb->hash[i] = NULL;
6969
while (p) { /* for each node in the list */
70-
TString *hnext = p->tsv.hnext; /* save next */
71-
unsigned int h = lmod(p->tsv.hash, newsize); /* new position */
72-
p->tsv.hnext = tb->hash[h]; /* chain it */
70+
TString *hnext = p->hnext; /* save next */
71+
unsigned int h = lmod(p->hash, newsize); /* new position */
72+
p->hnext = tb->hash[h]; /* chain it */
7373
tb->hash[h] = p;
7474
p = hnext;
7575
}
@@ -92,24 +92,24 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
9292
TString *ts;
9393
GCObject *o;
9494
size_t totalsize; /* total size of TString object */
95-
totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
95+
totalsize = sizelstring(l);
9696
o = luaC_newobj(L, tag, totalsize);
97-
ts = rawgco2ts(o);
98-
ts->tsv.len = l;
99-
ts->tsv.hash = h;
100-
ts->tsv.extra = 0;
101-
memcpy(ts+1, str, l*sizeof(char));
102-
((char *)(ts+1))[l] = '\0'; /* ending 0 */
97+
ts = gco2ts(o);
98+
ts->len = l;
99+
ts->hash = h;
100+
ts->extra = 0;
101+
memcpy(getaddrstr(ts), str, l * sizeof(char));
102+
getaddrstr(ts)[l] = '\0'; /* ending 0 */
103103
return ts;
104104
}
105105

106106

107107
void luaS_remove (lua_State *L, TString *ts) {
108108
stringtable *tb = &G(L)->strt;
109-
TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
109+
TString **p = &tb->hash[lmod(ts->hash, tb->size)];
110110
while (*p != ts) /* find previous element */
111-
p = &(*p)->tsv.hnext;
112-
*p = (*p)->tsv.hnext; /* remove element from its list */
111+
p = &(*p)->hnext;
112+
*p = (*p)->hnext; /* remove element from its list */
113113
tb->nuse--;
114114
}
115115

@@ -122,12 +122,12 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
122122
global_State *g = G(L);
123123
unsigned int h = luaS_hash(str, l, g->seed);
124124
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
125-
for (ts = *list; ts != NULL; ts = ts->tsv.hnext) {
126-
if (l == ts->tsv.len &&
125+
for (ts = *list; ts != NULL; ts = ts->hnext) {
126+
if (l == ts->len &&
127127
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
128128
/* found! */
129-
if (isdead(g, ts2gco(ts))) /* dead (but not collected yet)? */
130-
changewhite(ts2gco(ts)); /* resurrect it */
129+
if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */
130+
changewhite(obj2gco(ts)); /* resurrect it */
131131
return ts;
132132
}
133133
}
@@ -136,7 +136,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
136136
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
137137
}
138138
ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
139-
ts->tsv.hnext = *list;
139+
ts->hnext = *list;
140140
*list = ts;
141141
g->strt.nuse++;
142142
return ts;

0 commit comments

Comments
 (0)