Skip to content

Commit bb12903

Browse files
committed
type 'Udata' refers directly to structure inside the union (union
used only for aligning purposes now)
1 parent ca41b43 commit bb12903

File tree

7 files changed

+48
-35
lines changed

7 files changed

+48
-35
lines changed

lapi.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lapi.c,v 2.226 2014/07/17 13:53:37 roberto Exp roberto $
2+
** $Id: lapi.c,v 2.227 2014/07/18 12:17:54 roberto Exp roberto $
33
** Lua API
44
** See Copyright Notice in lua.h
55
*/
@@ -420,7 +420,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
420420
LUA_API void *lua_touserdata (lua_State *L, int idx) {
421421
StkId o = index2addr(L, idx);
422422
switch (ttnov(o)) {
423-
case LUA_TUSERDATA: return (rawuvalue(o) + 1);
423+
case LUA_TUSERDATA: return getudatamem(uvalue(o));
424424
case LUA_TLIGHTUSERDATA: return pvalue(o);
425425
default: return NULL;
426426
}
@@ -706,7 +706,7 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
706706
lua_lock(L);
707707
o = index2addr(L, idx);
708708
api_check(ttisfulluserdata(o), "full userdata expected");
709-
getuservalue(L, rawuvalue(o), L->top);
709+
getuservalue(L, uvalue(o), L->top);
710710
api_incr_top(L);
711711
lua_unlock(L);
712712
return ttnov(L->top - 1);
@@ -842,7 +842,7 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
842842
api_checknelems(L, 1);
843843
o = index2addr(L, idx);
844844
api_check(ttisfulluserdata(o), "full userdata expected");
845-
setuservalue(L, rawuvalue(o), L->top - 1);
845+
setuservalue(L, uvalue(o), L->top - 1);
846846
luaC_barrier(L, gcvalue(o), L->top - 1);
847847
L->top--;
848848
lua_unlock(L);
@@ -1142,7 +1142,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
11421142
setuvalue(L, L->top, u);
11431143
api_incr_top(L);
11441144
lua_unlock(L);
1145-
return u + 1;
1145+
return getudatamem(u);
11461146
}
11471147

11481148

lgc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.c,v 2.186 2014/07/18 12:17:54 roberto Exp roberto $
2+
** $Id: lgc.c,v 2.187 2014/07/18 13:36:14 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -237,7 +237,7 @@ static void reallymarkobject (global_State *g, GCObject *o) {
237237
markobject(g, gco2u(o)->metatable); /* mark its metatable */
238238
gray2black(o);
239239
g->GCmemtrav += sizeudata(gco2u(o));
240-
getuservalue(g->mainthread, rawgco2u(o), &uvalue);
240+
getuservalue(g->mainthread, gco2u(o), &uvalue);
241241
if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */
242242
o = gcvalue(&uvalue);
243243
goto reentry;

lobject.h

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lobject.h,v 2.97 2014/07/18 12:17:54 roberto Exp roberto $
2+
** $Id: lobject.h,v 2.98 2014/07/18 13:36:14 roberto Exp roberto $
33
** Type definitions for Lua objects
44
** See Copyright Notice in lua.h
55
*/
@@ -157,8 +157,7 @@ typedef struct lua_TValue TValue;
157157
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
158158
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
159159
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
160-
#define rawuvalue(o) check_exp(ttisfulluserdata(o), rawgco2u(val_(o).gc))
161-
#define uvalue(o) (&rawuvalue(o)->uv)
160+
#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
162161
#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
163162
#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
164163
#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
@@ -214,7 +213,7 @@ typedef struct lua_TValue TValue;
214213

215214
#define setuvalue(L,obj,x) \
216215
{ TValue *io = (obj); Udata *x_ = (x); \
217-
val_(io).gc = obj2gco(&x_->uv); settt_(io, ctb(LUA_TUSERDATA)); \
216+
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
218217
checkliveness(G(L),io); }
219218

220219
#define setthvalue(L,obj,x) \
@@ -324,36 +323,50 @@ typedef union UTString {
324323
*/
325324
#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
326325
#define getstr(ts) \
327-
((void)(ts)->extra, cast(const char*, getaddrstr(ts)))
326+
check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts)))
328327

329328
/* get the actual string (array of bytes) from a Lua value */
330329
#define svalue(o) getstr(tsvalue(o))
331330

332331

333332
/*
334333
** Header for userdata; memory area follows the end of this structure
334+
** (aligned according to 'UUdata'; see next).
335335
*/
336-
typedef union Udata {
337-
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
338-
struct {
339-
CommonHeader;
340-
lu_byte ttuv_; /* user value's tag */
341-
struct Table *metatable;
342-
size_t len; /* number of bytes */
343-
union Value user_; /* user value */
344-
} uv;
336+
typedef struct Udata {
337+
CommonHeader;
338+
lu_byte ttuv_; /* user value's tag */
339+
struct Table *metatable;
340+
size_t len; /* number of bytes */
341+
union Value user_; /* user value */
345342
} Udata;
346343

347344

345+
/*
346+
** Ensures that address after this type is always fully aligned.
347+
*/
348+
typedef union UUdata {
349+
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
350+
Udata uv;
351+
} UUdata;
352+
353+
354+
/*
355+
** Get the address of memory block inside 'Udata'.
356+
** (Access to 'ttuv_' ensures that value is really a 'Udata'.)
357+
*/
358+
#define getudatamem(u) \
359+
check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata)))
360+
348361
#define setuservalue(L,u,o) \
349362
{ const TValue *io=(o); Udata *iu = (u); \
350-
iu->uv.user_ = io->value_; iu->uv.ttuv_ = io->tt_; \
363+
iu->user_ = io->value_; iu->ttuv_ = io->tt_; \
351364
checkliveness(G(L),io); }
352365

353366

354367
#define getuservalue(L,u,o) \
355368
{ TValue *io=(o); const Udata *iu = (u); \
356-
io->value_ = iu->uv.user_; io->tt_ = iu->uv.ttuv_; \
369+
io->value_ = iu->user_; io->tt_ = iu->ttuv_; \
357370
checkliveness(G(L),io); }
358371

359372

lstate.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstate.h,v 2.111 2014/07/18 12:17:54 roberto Exp roberto $
2+
** $Id: lstate.h,v 2.112 2014/07/18 13:36:14 roberto Exp roberto $
33
** Global State
44
** See Copyright Notice in lua.h
55
*/
@@ -175,7 +175,7 @@ struct lua_State {
175175
union GCUnion {
176176
GCObject gc; /* common header */
177177
struct TString ts;
178-
union Udata u;
178+
struct Udata u;
179179
union Closure cl;
180180
struct Table h;
181181
struct Proto p;
@@ -188,8 +188,7 @@ union GCUnion {
188188
/* macros to convert a GCObject into a specific value */
189189
#define gco2ts(o) \
190190
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
191-
#define rawgco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
192-
#define gco2u(o) (&rawgco2u(o)->uv)
191+
#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
193192
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
194193
#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
195194
#define gco2cl(o) \

lstring.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lstring.c,v 2.41 2014/07/18 12:17:54 roberto Exp roberto $
2+
** $Id: lstring.c,v 2.42 2014/07/18 13:36:14 roberto Exp roberto $
33
** String table (keeps all strings handled by Lua)
44
** See Copyright Notice in lua.h
55
*/
@@ -170,10 +170,10 @@ Udata *luaS_newudata (lua_State *L, size_t s) {
170170
GCObject *o;
171171
if (s > MAX_SIZE - sizeof(Udata))
172172
luaM_toobig(L);
173-
o = luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s);
174-
u = rawgco2u(o);
175-
u->uv.len = s;
176-
u->uv.metatable = NULL;
173+
o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
174+
u = gco2u(o);
175+
u->len = s;
176+
u->metatable = NULL;
177177
setuservalue(L, u, luaO_nilobject);
178178
return u;
179179
}

lstring.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
1616
#define sizestring(s) sizelstring((s)->len)
1717

18-
#define sizeudata(u) (sizeof(union Udata)+(u)->len)
18+
#define sizeludata(l) (sizeof(union UUdata) + (l))
19+
#define sizeudata(u) sizeludata((u)->len)
1920

2021
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
2122
(sizeof(s)/sizeof(char))-1))

ltests.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltests.c,v 2.178 2014/07/18 12:17:54 roberto Exp roberto $
2+
** $Id: ltests.c,v 2.179 2014/07/18 13:36:14 roberto Exp roberto $
33
** Internal Module for Debugging of the Lua Implementation
44
** See Copyright Notice in lua.h
55
*/
@@ -319,7 +319,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead) {
319319
TValue uservalue;
320320
Table *mt = gco2u(o)->metatable;
321321
checkobjref(g, o, mt);
322-
getuservalue(g->mainthread, rawgco2u(o), &uservalue);
322+
getuservalue(g->mainthread, gco2u(o), &uservalue);
323323
checkvalref(g, o, &uservalue);
324324
break;
325325
}

0 commit comments

Comments
 (0)