commit d1c689af402e847f77dbe164265e097e90bdc61a
parent 37e9c2e74486ed443151430a9b73a3844d3554ef
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 30 Oct 2000 15:48:57 -0200
subtelties in layout of TString
Diffstat:
3 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/lobject.h b/lobject.h
@@ -1,5 +1,5 @@
/*
-** $Id: lobject.h,v 1.80 2000/10/26 12:47:05 roberto Exp roberto $
+** $Id: lobject.h,v 1.81 2000/10/30 16:29:59 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@@ -71,9 +71,16 @@ 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 {
- union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
struct { /* for strings */
unsigned long hash;
int constindex; /* hint to reuse constants */
@@ -85,8 +92,8 @@ typedef struct TString {
} u;
size_t len;
struct TString *nexthash; /* chain for hash table */
- unsigned char marked;
- char str[1]; /* variable length string!! must be the last field! */
+ int marked;
+ char str[TSPACK]; /* variable length string!! must be the last field! */
} TString;
diff --git a/lstring.c b/lstring.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstring.c,v 1.43 2000/09/29 12:42:13 roberto Exp roberto $
+** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $
** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -15,6 +15,15 @@
#include "lstring.h"
+/*
+** type equivalent to TString, but with maximum alignment requirements
+*/
+union L_UTString {
+ TString ts;
+ union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
+};
+
+
void luaS_init (lua_State *L) {
L->strt.hash = luaM_newvector(L, 1, TString *);
@@ -103,12 +112,14 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
- TString *ts = (TString *)luaM_malloc(L, (lint32)sizeof(TString)+s);
+ union L_UTString *uts = (union L_UTString *)luaM_malloc(L,
+ (lint32)sizeof(union L_UTString)+s);
+ TString *ts = &uts->ts;
ts->marked = 0;
ts->nexthash = NULL;
ts->len = s;
ts->u.d.tag = 0;
- ts->u.d.value = (udata == NULL) ? ts+1 : udata;
+ ts->u.d.value = (udata == NULL) ? uts+1 : udata;
L->nblocks += sizestring(s);
/* insert it on table */
newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
diff --git a/lstring.h b/lstring.h
@@ -1,5 +1,5 @@
/*
-** $Id: lstring.h,v 1.22 2000/09/29 12:42:13 roberto Exp roberto $
+** $Id: lstring.h,v 1.23 2000/10/26 12:47:05 roberto Exp roberto $
** String table (keep all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -20,7 +20,8 @@
#define RESERVEDMARK 3
-#define sizestring(l) (sizeof(TString)+(lint32)(l)*sizeof(char))
+#define sizestring(l) ((long)sizeof(TString) + \
+ ((long)(l+1)-TSPACK)*(long)sizeof(char))
void luaS_init (lua_State *L);