Browse Source

string pointers are always fully aligned

Roberto Ierusalimschy 24 năm trước cách đây
mục cha
commit
d2e340f467
11 tập tin đã thay đổi với 47 bổ sung51 xóa
  1. 5 5
      ldebug.c
  2. 2 2
      lfunc.c
  3. 2 2
      llex.c
  4. 15 10
      lobject.h
  5. 4 4
      lparser.c
  6. 4 4
      lstring.c
  7. 2 13
      lstring.h
  8. 5 3
      ltests.c
  9. 2 2
      ltm.c
  10. 2 2
      ltm.h
  11. 4 4
      lvm.c

+ 5 - 5
ldebug.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.60 2001/02/07 18:13:49 roberto Exp roberto $
+** $Id: ldebug.c,v 1.61 2001/02/09 18:37:33 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -198,7 +198,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
 
 
 static void infoLproto (lua_Debug *ar, Proto *f) {
-  ar->source = f->source->str;
+  ar->source = getstr(f->source);
   ar->linedefined = f->lineDefined;
   ar->what = "Lua";
 }
@@ -249,7 +249,7 @@ static const char *travglobals (lua_State *L, const TObject *o) {
   for (i=0; i<g->size; i++) {
     if (luaO_equalObj(o, val(node(g, i))) &&
         ttype_key(node(g, i)) == LUA_TSTRING)
-      return tsvalue_key(node(g, i))->str;
+      return getstr(tsvalue_key(node(g, i)));
   }
   return NULL;
 }
@@ -499,7 +499,7 @@ static const char *getobjname (lua_State *L, StkId obj, const char **name) {
     lua_assert(pc != -1);
     switch (GET_OPCODE(i)) {
       case OP_GETGLOBAL: {
-        *name = p->kstr[GETARG_U(i)]->str;
+        *name = getstr(p->kstr[GETARG_U(i)]);
         return "global";
       }
       case OP_GETLOCAL: {
@@ -509,7 +509,7 @@ static const char *getobjname (lua_State *L, StkId obj, const char **name) {
       }
       case OP_PUSHSELF:
       case OP_GETDOTTED: {
-        *name = p->kstr[GETARG_U(i)]->str;
+        *name = getstr(p->kstr[GETARG_U(i)]);
         return "field";
       }
       default:

+ 2 - 2
lfunc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lfunc.c,v 1.38 2001/01/29 19:34:02 roberto Exp roberto $
+** $Id: lfunc.c,v 1.39 2001/02/01 17:40:48 roberto Exp roberto $
 ** Auxiliary functions to manipulate prototypes and closures
 ** See Copyright Notice in lua.h
 */
@@ -79,7 +79,7 @@ const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
     if (pc < f->locvars[i].endpc) {  /* is variable active? */
       local_number--;
       if (local_number == 0)
-        return f->locvars[i].varname->str;
+        return getstr(f->locvars[i].varname);
     }
   }
   return NULL;  /* not found */

+ 2 - 2
llex.c

@@ -1,5 +1,5 @@
 /*
-** $Id: llex.c,v 1.75 2001/01/15 18:07:56 roberto Exp roberto $
+** $Id: llex.c,v 1.76 2001/01/19 13:20:30 roberto Exp roberto $
 ** Lexical Analyzer
 ** See Copyright Notice in lua.h
 */
@@ -55,7 +55,7 @@ void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
 
 void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
   char buff[MAXSRC];
-  luaO_chunkid(buff, ls->source->str, sizeof(buff));
+  luaO_chunkid(buff, getstr(ls->source), sizeof(buff));
   luaO_verror(ls->L, "%.99s;\n  last token read: `%.30s' at line %d in %.80s",
               s, token, ls->linenumber, buff);
 }

+ 15 - 10
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 1.93 2001/02/02 15:13:05 roberto Exp roberto $
+** $Id: lobject.h,v 1.94 2001/02/02 16:32:00 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -57,7 +57,6 @@ typedef struct lua_TObject {
 #define clvalue(o)      ((o)->value.cl)
 #define hvalue(o)       ((o)->value.h)
 #define infovalue(o)	((o)->value.info)
-#define svalue(o)       (tsvalue(o)->str)
 
 
 /* Macros to set values */
@@ -91,13 +90,6 @@ typedef struct lua_TObject {
 ** String headers for string table
 */
 
-/*
-** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK
-** tries to make sizeof(TString) a multiple of this granularity, to reduce
-** waste of space.
-*/
-#define TSPACK	((int)sizeof(int))
-
 typedef struct TString {
   union {
     struct {  /* for strings */
@@ -112,10 +104,23 @@ typedef struct TString {
   size_t len;
   int marked;
   struct TString *nexthash;  /* chain for hash table */
-  char str[TSPACK];   /* variable length string!! must be the last field! */
 } TString;
 
 
+/*
+** type equivalent to TString, but with maximum alignment requirements
+*/
+union L_UTString {
+  TString ts;
+  union L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
+};
+
+
+
+#define getstr(ts)	((char *)(ts) + sizeof(union L_UTString))
+#define svalue(o)       getstr(tsvalue(o))
+
+
 /*
 ** Function Prototypes
 */

+ 4 - 4
lparser.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 1.130 2001/02/08 11:19:10 roberto Exp roberto $
+** $Id: lparser.c,v 1.131 2001/02/09 18:37:33 roberto Exp roberto $
 ** LL(1) Parser and code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -209,7 +209,7 @@ static int search_local (LexState *ls, TString *n, expdesc *var) {
 static void singlevar (LexState *ls, TString *n, expdesc *var) {
   int level = search_local(ls, n, var);
   if (level >= 1)  /* neither local (0) nor global (-1)? */
-    luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
+    luaX_syntaxerror(ls, "cannot access a variable in outer scope", getstr(n));
   else if (level == -1)  /* global? */
     var->u.index = string_constant(ls->fs, n);
 }
@@ -235,12 +235,12 @@ static void pushupvalue (LexState *ls, TString *n) {
   int level = search_local(ls, n, &v);
   if (level == -1) {  /* global? */
     if (fs->prev == NULL)
-      luaX_syntaxerror(ls, "cannot access an upvalue at top level", n->str);
+      luaX_syntaxerror(ls, "cannot access an upvalue at top level", getstr(n));
     v.u.index = string_constant(fs->prev, n);
   }
   else if (level != 1)
     luaX_syntaxerror(ls,
-         "upvalue must be global or local to immediately outer scope", n->str);
+      "upvalue must be global or local to immediately outer scope", getstr(n));
   luaK_code1(fs, OP_PUSHUPVALUE, indexupvalue(ls, &v));
 }
 

+ 4 - 4
lstring.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 1.55 2001/02/01 17:40:48 roberto Exp roberto $
+** $Id: lstring.c,v 1.56 2001/02/09 19:53:16 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -71,7 +71,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
   for (l1=l; l1>=step; l1-=step)  /* compute hash */
     h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]);
   for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; ts; ts = ts->nexthash) {
-    if (ts->len == l && (memcmp(str, ts->str, l) == 0))
+    if (ts->len == l && (memcmp(str, getstr(ts), l) == 0))
       return ts;
   }
   /* not found */
@@ -81,8 +81,8 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
   ts->len = l;
   ts->u.s.hash = h;
   ts->u.s.constindex = 0;
-  memcpy(ts->str, str, l);
-  ts->str[l] = 0;  /* ending 0 */
+  memcpy(getstr(ts), str, l);
+  getstr(ts)[l] = 0;  /* ending 0 */
   newentry(L, &G(L)->strt, ts, lmod(h, G(L)->strt.size));  /* insert it */
   return ts;
 }

+ 2 - 13
lstring.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.h,v 1.27 2001/01/10 17:41:50 roberto Exp roberto $
+** $Id: lstring.h,v 1.28 2001/02/09 19:53:16 roberto Exp roberto $
 ** String table (keep all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -13,16 +13,6 @@
 
 
 
-/*
-** type equivalent to TString, but with maximum alignment requirements
-*/
-union L_UTString {
-  TString ts;
-  union L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
-};
-
-
-
 /*
 ** any TString with mark>=FIXMARK is never collected.
 ** Marks>=RESERVEDMARK are used to identify reserved words.
@@ -31,8 +21,7 @@ union L_UTString {
 #define RESERVEDMARK	3
 
 
-#define sizestring(l)	((lint32)sizeof(TString) + \
-                         ((lint32)(l+1)-TSPACK)*(lint32)sizeof(char))
+#define sizestring(l)	((luint32)sizeof(union L_UTString)+(l)+1)
 
 #define sizeudata(l)	((luint32)sizeof(union L_UTString)+(l))
 

+ 5 - 3
ltests.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 1.64 2001/02/06 18:18:58 roberto Exp roberto $
+** $Id: ltests.c,v 1.65 2001/02/09 19:53:16 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -201,7 +201,7 @@ static int liststrings (lua_State *L) {
   lua_newtable(L);
   for (i=0; i<p->sizekstr; i++) {
     lua_pushnumber(L, i+1);
-    lua_pushstring(L, p->kstr[i]->str);
+    lua_pushstring(L, getstr(p->kstr[i]));
     lua_settable(L, -3);
   }
   return 1;
@@ -537,7 +537,9 @@ static int testC (lua_State *L) {
       lua_pushnumber(L, lua_tonumber(L, getnum));
     }
     else if EQ("tostring") {
-      lua_pushstring(L, lua_tostring(L, getnum));
+      const char *s = lua_tostring(L, getnum);
+      lua_assert((unsigned long)s % 4 == 0);  /* check alignment */
+      lua_pushstring(L, s);
     }
     else if EQ("tonumber") {
       lua_pushnumber(L, lua_tonumber(L, getnum));

+ 2 - 2
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 1.64 2001/01/26 11:45:51 roberto Exp roberto $
+** $Id: ltm.c,v 1.65 2001/02/02 15:13:05 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -150,7 +150,7 @@ const char *luaT_typename (global_State *G, const TObject *o) {
   ts = G->TMtable[tag].name;
   if (ts == NULL)
     ts = G->TMtable[t].name;
-  return ts->str;
+  return getstr(ts);
 }
 
 

+ 2 - 2
ltm.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.h,v 1.21 2001/01/24 16:20:54 roberto Exp roberto $
+** $Id: ltm.h,v 1.22 2001/01/25 16:45:36 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -62,7 +62,7 @@ struct TM {
 #define luaT_gettm(G,tag,event) (G->TMtable[tag].method[event])
 #define luaT_gettmbyObj(G,o,e)  (luaT_gettm((G),luaT_tag(o),(e)))
 
-#define basictypename(G, t)	(G->TMtable[t].name->str)
+#define basictypename(G, t)	getstr(G->TMtable[t].name)
 
 
 #define validtag(G,t) (NUM_TAGS <= (t) && (t) < G->ntag)

+ 4 - 4
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.166 2001/02/07 18:13:49 roberto Exp roberto $
+** $Id: lvm.c,v 1.167 2001/02/09 18:07:47 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -225,9 +225,9 @@ static void call_arith (lua_State *L, StkId p1, TMS event) {
 
 
 static int luaV_strlessthan (const TString *ls, const TString *rs) {
-  const char *l = ls->str;
+  const char *l = getstr(ls);
   size_t ll = ls->len;
-  const char *r = rs->str;
+  const char *r = getstr(rs);
   size_t lr = rs->len;
   for (;;) {
     int temp = strcoll(l, r);
@@ -281,7 +281,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
       tl = 0;
       for (i=n; i>0; i--) {  /* concat all strings */
         size_t l = tsvalue(top-i)->len;
-        memcpy(buffer+tl, tsvalue(top-i)->str, l);
+        memcpy(buffer+tl, svalue(top-i), l);
         tl += l;
       }
       setsvalue(top-n, luaS_newlstr(L, buffer, tl));