aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstring.c')
-rw-r--r--lstring.c35
1 files changed, 8 insertions, 27 deletions
diff --git a/lstring.c b/lstring.c
index 05e5bbaa..c8cd17b1 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.46 2000/11/24 17:39:56 roberto Exp roberto $ 2** $Id: lstring.c,v 1.47 2000/12/22 16:57:46 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,32 +15,18 @@
15#include "lstring.h" 15#include "lstring.h"
16 16
17 17
18/*
19** type equivalent to TString, but with maximum alignment requirements
20*/
21union L_UTString {
22 TString ts;
23 union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
24};
25
26
27 18
28void luaS_init (lua_State *L) { 19void luaS_init (lua_State *L) {
29 L->strt.hash = luaM_newvector(L, 1, TString *); 20 luaS_resize(L, &L->strt, MINPOWER2);
30 L->udt.hash = luaM_newvector(L, 1, TString *); 21 luaS_resize(L, &L->udt, MINPOWER2);
31 L->nblocks += 2*sizeof(TString *);
32 L->strt.size = L->udt.size = 1;
33 L->strt.nuse = L->udt.nuse = 0;
34 L->strt.hash[0] = L->udt.hash[0] = NULL;
35} 22}
36 23
37 24
38void luaS_freeall (lua_State *L) { 25void luaS_freeall (lua_State *L) {
39 LUA_ASSERT(L->strt.nuse==0, "non-empty string table"); 26 LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
40 L->nblocks -= (L->strt.size + L->udt.size)*sizeof(TString *); 27 luaM_freearray(L, L->strt.hash, L->strt.size, TString *);
41 luaM_free(L, L->strt.hash);
42 LUA_ASSERT(L->udt.nuse==0, "non-empty udata table"); 28 LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
43 luaM_free(L, L->udt.hash); 29 luaM_freearray(L, L->udt.hash, L->udt.size, TString *);
44} 30}
45 31
46 32
@@ -71,9 +57,7 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
71 p = next; 57 p = next;
72 } 58 }
73 } 59 }
74 luaM_free(L, tb->hash); 60 luaM_freearray(L, tb->hash, tb->size, TString *);
75 L->nblocks -= tb->size*sizeof(TString *);
76 L->nblocks += newsize*sizeof(TString *);
77 tb->size = newsize; 61 tb->size = newsize;
78 tb->hash = newhash; 62 tb->hash = newhash;
79} 63}
@@ -106,23 +90,20 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
106 ts->u.s.constindex = 0; 90 ts->u.s.constindex = 0;
107 memcpy(ts->str, str, l); 91 memcpy(ts->str, str, l);
108 ts->str[l] = 0; /* ending 0 */ 92 ts->str[l] = 0; /* ending 0 */
109 L->nblocks += sizestring(l);
110 newentry(L, &L->strt, ts, h1); /* insert it on table */ 93 newentry(L, &L->strt, ts, h1); /* insert it on table */
111 return ts; 94 return ts;
112} 95}
113 96
114 97
115TString *luaS_newudata (lua_State *L, size_t s, void *udata) { 98TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
116 union L_UTString *uts = (union L_UTString *)luaM_malloc(L, 99 union L_UTString *uts = (union L_UTString *)luaM_malloc(L, sizeudata(s));
117 (luint32)sizeof(union L_UTString)+s);
118 TString *ts = &uts->ts; 100 TString *ts = &uts->ts;
119 ts->marked = 0; 101 ts->marked = 0;
120 ts->nexthash = NULL; 102 ts->nexthash = NULL;
121 ts->len = s; 103 ts->len = s;
122 ts->u.d.tag = 0; 104 ts->u.d.tag = 0;
123 ts->u.d.value = (udata == NULL) ? uts+1 : udata; 105 ts->u.d.value = (udata == NULL) ? uts+1 : udata;
124 L->nblocks += sizestring(s); 106 /* insert it on table */
125 /* insert it on table */
126 newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1)); 107 newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));
127 return ts; 108 return ts;
128} 109}