more regularity with vectors + sizeof computed by the macros themselves

This commit is contained in:
Roberto Ierusalimschy 2014-02-28 13:13:01 -03:00
parent 93e28031de
commit 99a1c06ea3
2 changed files with 18 additions and 19 deletions

22
ldump.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldump.c,v 2.21 2014/02/27 18:56:15 roberto Exp roberto $ ** $Id: ldump.c,v 2.22 2014/02/28 12:25:12 roberto Exp roberto $
** save precompiled Lua chunks ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -23,8 +23,9 @@ typedef struct {
int status; int status;
} DumpState; } DumpState;
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) #define DumpVar(x,D) DumpBlock(&x,sizeof(x),D)
#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
static void DumpBlock(const void* b, size_t size, DumpState* D) static void DumpBlock(const void* b, size_t size, DumpState* D)
{ {
@ -57,12 +58,6 @@ static void DumpInteger(lua_Integer x, DumpState* D)
DumpVar(x,D); DumpVar(x,D);
} }
static void DumpVector(const void* b, int n, size_t size, DumpState* D)
{
DumpInt(n,D);
DumpMem(b,n,size,D);
}
static void DumpString(const TString* s, DumpState* D) static void DumpString(const TString* s, DumpState* D)
{ {
if (s==NULL) if (s==NULL)
@ -78,7 +73,11 @@ static void DumpString(const TString* s, DumpState* D)
} }
} }
#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) static void DumpCode(const Proto* f, DumpState* D)
{
DumpInt(f->sizecode,D);
DumpVector(f->code,f->sizecode,D);
}
static void DumpFunction(const Proto* f, DumpState* D); static void DumpFunction(const Proto* f, DumpState* D);
@ -130,7 +129,8 @@ static void DumpDebug(const Proto* f, DumpState* D)
int i,n; int i,n;
DumpString((D->strip) ? NULL : f->source,D); DumpString((D->strip) ? NULL : f->source,D);
n= (D->strip) ? 0 : f->sizelineinfo; n= (D->strip) ? 0 : f->sizelineinfo;
DumpVector(f->lineinfo,n,sizeof(int),D); DumpInt(n,D);
DumpVector(f->lineinfo,n,D);
n= (D->strip) ? 0 : f->sizelocvars; n= (D->strip) ? 0 : f->sizelocvars;
DumpInt(n,D); DumpInt(n,D);
for (i=0; i<n; i++) for (i=0; i<n; i++)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 2.27 2014/02/27 18:56:15 roberto Exp roberto $ ** $Id: lundump.c,v 2.28 2014/02/28 12:25:12 roberto Exp roberto $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -33,9 +33,8 @@ static l_noret error(LoadState* S, const char* why)
luaD_throw(S->L,LUA_ERRSYNTAX); luaD_throw(S->L,LUA_ERRSYNTAX);
} }
#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) #define LoadVar(S,x) LoadBlock(S,&x,sizeof(x))
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
#if !defined(luai_verifycode) #if !defined(luai_verifycode)
#define luai_verifycode(L,b,f) /* empty */ #define luai_verifycode(L,b,f) /* empty */
@ -84,7 +83,7 @@ static TString* LoadString(LoadState* S)
else else
{ {
char* s=luaZ_openspace(S->L,S->b,size); char* s=luaZ_openspace(S->L,S->b,size);
LoadBlock(S,s,size*sizeof(char)); LoadVector(S,s,size);
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
} }
} }
@ -94,7 +93,7 @@ static void LoadCode(LoadState* S, Proto* f)
int n=LoadInt(S); int n=LoadInt(S);
f->code=luaM_newvector(S->L,n,Instruction); f->code=luaM_newvector(S->L,n,Instruction);
f->sizecode=n; f->sizecode=n;
LoadVector(S,f->code,n,sizeof(Instruction)); LoadVector(S,f->code,n);
} }
static void LoadFunction(LoadState* S, Proto* f); static void LoadFunction(LoadState* S, Proto* f);
@ -162,7 +161,7 @@ static void LoadDebug(LoadState* S, Proto* f)
n=LoadInt(S); n=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,n,int); f->lineinfo=luaM_newvector(S->L,n,int);
f->sizelineinfo=n; f->sizelineinfo=n;
LoadVector(S,f->lineinfo,n,sizeof(int)); LoadVector(S,f->lineinfo,n);
n=LoadInt(S); n=LoadInt(S);
f->locvars=luaM_newvector(S->L,n,LocVar); f->locvars=luaM_newvector(S->L,n,LocVar);
f->sizelocvars=n; f->sizelocvars=n;
@ -193,7 +192,7 @@ static void LoadFunction(LoadState* S, Proto* f)
static void checkstring(LoadState *S, const char *s, const char *msg) static void checkstring(LoadState *S, const char *s, const char *msg)
{ {
char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */ char buff[sizeof(LUA_SIGNATURE)+sizeof(LUAC_DATA)]; /* larger than each */
LoadMem(S,buff,strlen(s)+1,sizeof(char)); LoadVector(S,buff,strlen(s)+1);
if (strcmp(s,buff)!=0) error(S,msg); if (strcmp(s,buff)!=0) error(S,msg);
} }