diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-30 15:49:19 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-30 15:49:19 -0200 |
commit | d1c689af402e847f77dbe164265e097e90bdc61a (patch) | |
tree | 15280b1d60381277d904049ced37b07ded8ad6ac | |
parent | 37e9c2e74486ed443151430a9b73a3844d3554ef (diff) | |
download | lua-d1c689af402e847f77dbe164265e097e90bdc61a.tar.gz lua-d1c689af402e847f77dbe164265e097e90bdc61a.tar.bz2 lua-d1c689af402e847f77dbe164265e097e90bdc61a.zip |
subtelties in layout of TString
-rw-r--r-- | lobject.h | 15 | ||||
-rw-r--r-- | lstring.c | 17 | ||||
-rw-r--r-- | lstring.h | 5 |
3 files changed, 28 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 1.80 2000/10/26 12:47:05 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.81 2000/10/30 16:29:59 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -71,9 +71,16 @@ typedef struct lua_TObject { | |||
71 | /* | 71 | /* |
72 | ** String headers for string table | 72 | ** String headers for string table |
73 | */ | 73 | */ |
74 | |||
75 | /* | ||
76 | ** most `malloc' libraries allocate memory in blocks of 8 bytes. TSPACK | ||
77 | ** tries to make sizeof(TString) a multiple of this granularity, to reduce | ||
78 | ** waste of space. | ||
79 | */ | ||
80 | #define TSPACK ((int)sizeof(int)) | ||
81 | |||
74 | typedef struct TString { | 82 | typedef struct TString { |
75 | union { | 83 | union { |
76 | union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ | ||
77 | struct { /* for strings */ | 84 | struct { /* for strings */ |
78 | unsigned long hash; | 85 | unsigned long hash; |
79 | int constindex; /* hint to reuse constants */ | 86 | int constindex; /* hint to reuse constants */ |
@@ -85,8 +92,8 @@ typedef struct TString { | |||
85 | } u; | 92 | } u; |
86 | size_t len; | 93 | size_t len; |
87 | struct TString *nexthash; /* chain for hash table */ | 94 | struct TString *nexthash; /* chain for hash table */ |
88 | unsigned char marked; | 95 | int marked; |
89 | char str[1]; /* variable length string!! must be the last field! */ | 96 | char str[TSPACK]; /* variable length string!! must be the last field! */ |
90 | } TString; | 97 | } TString; |
91 | 98 | ||
92 | 99 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.43 2000/09/29 12:42:13 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -15,6 +15,15 @@ | |||
15 | #include "lstring.h" | 15 | #include "lstring.h" |
16 | 16 | ||
17 | 17 | ||
18 | /* | ||
19 | ** type equivalent to TString, but with maximum alignment requirements | ||
20 | */ | ||
21 | union L_UTString { | ||
22 | TString ts; | ||
23 | union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ | ||
24 | }; | ||
25 | |||
26 | |||
18 | 27 | ||
19 | void luaS_init (lua_State *L) { | 28 | void luaS_init (lua_State *L) { |
20 | L->strt.hash = luaM_newvector(L, 1, TString *); | 29 | L->strt.hash = luaM_newvector(L, 1, TString *); |
@@ -103,12 +112,14 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
103 | 112 | ||
104 | 113 | ||
105 | TString *luaS_newudata (lua_State *L, size_t s, void *udata) { | 114 | TString *luaS_newudata (lua_State *L, size_t s, void *udata) { |
106 | TString *ts = (TString *)luaM_malloc(L, (lint32)sizeof(TString)+s); | 115 | union L_UTString *uts = (union L_UTString *)luaM_malloc(L, |
116 | (lint32)sizeof(union L_UTString)+s); | ||
117 | TString *ts = &uts->ts; | ||
107 | ts->marked = 0; | 118 | ts->marked = 0; |
108 | ts->nexthash = NULL; | 119 | ts->nexthash = NULL; |
109 | ts->len = s; | 120 | ts->len = s; |
110 | ts->u.d.tag = 0; | 121 | ts->u.d.tag = 0; |
111 | ts->u.d.value = (udata == NULL) ? ts+1 : udata; | 122 | ts->u.d.value = (udata == NULL) ? uts+1 : udata; |
112 | L->nblocks += sizestring(s); | 123 | L->nblocks += sizestring(s); |
113 | /* insert it on table */ | 124 | /* insert it on table */ |
114 | newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1)); | 125 | newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1)); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.h,v 1.22 2000/09/29 12:42:13 roberto Exp roberto $ | 2 | ** $Id: lstring.h,v 1.23 2000/10/26 12:47:05 roberto Exp roberto $ |
3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -20,7 +20,8 @@ | |||
20 | #define RESERVEDMARK 3 | 20 | #define RESERVEDMARK 3 |
21 | 21 | ||
22 | 22 | ||
23 | #define sizestring(l) (sizeof(TString)+(lint32)(l)*sizeof(char)) | 23 | #define sizestring(l) ((long)sizeof(TString) + \ |
24 | ((long)(l+1)-TSPACK)*(long)sizeof(char)) | ||
24 | 25 | ||
25 | 26 | ||
26 | void luaS_init (lua_State *L); | 27 | void luaS_init (lua_State *L); |