瀏覽代碼

more regularity with vectors + sizeof computed by the macros themselves

Roberto Ierusalimschy 11 年之前
父節點
當前提交
99a1c06ea3
共有 2 個文件被更改,包括 18 次插入19 次删除
  1. 11 11
      ldump.c
  2. 7 8
      lundump.c

+ 11 - 11
ldump.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -23,8 +23,9 @@ typedef struct {
  int status;
 } 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)
 {
@@ -57,12 +58,6 @@ static void DumpInteger(lua_Integer x, DumpState* 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)
 {
  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);
 
@@ -130,7 +129,8 @@ static void DumpDebug(const Proto* f, DumpState* D)
  int i,n;
  DumpString((D->strip) ? NULL : f->source,D);
  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;
  DumpInt(n,D);
  for (i=0; i<n; i++)

+ 7 - 8
lundump.c

@@ -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
 ** 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);
 }
 
-#define LoadMem(S,b,n,size)	LoadBlock(S,b,(n)*(size))
-#define LoadVar(S,x)		LoadMem(S,&x,1,sizeof(x))
-#define LoadVector(S,b,n,size)	LoadMem(S,b,n,size)
+#define LoadVar(S,x)		LoadBlock(S,&x,sizeof(x))
+#define LoadVector(S,b,n)	LoadBlock(S,b,(n)*sizeof((b)[0]))
 
 #if !defined(luai_verifycode)
 #define luai_verifycode(L,b,f)	/* empty */
@@ -84,7 +83,7 @@ static TString* LoadString(LoadState* S)
  else
  {
   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' */
  }
 }
@@ -94,7 +93,7 @@ static void LoadCode(LoadState* S, Proto* f)
  int n=LoadInt(S);
  f->code=luaM_newvector(S->L,n,Instruction);
  f->sizecode=n;
- LoadVector(S,f->code,n,sizeof(Instruction));
+ LoadVector(S,f->code,n);
 }
 
 static void LoadFunction(LoadState* S, Proto* f);
@@ -162,7 +161,7 @@ static void LoadDebug(LoadState* S, Proto* f)
  n=LoadInt(S);
  f->lineinfo=luaM_newvector(S->L,n,int);
  f->sizelineinfo=n;
- LoadVector(S,f->lineinfo,n,sizeof(int));
+ LoadVector(S,f->lineinfo,n);
  n=LoadInt(S);
  f->locvars=luaM_newvector(S->L,n,LocVar);
  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)
 {
  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);
 }