diff options
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 35 |
1 files changed, 22 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.42 2000/08/09 19:16:57 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.43 2000/09/29 12:42:13 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 | */ |
@@ -81,17 +81,17 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) { | |||
81 | 81 | ||
82 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | 82 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { |
83 | unsigned long h = hash_s(str, l); | 83 | unsigned long h = hash_s(str, l); |
84 | int h1 = h&(L->strt.size-1); | 84 | int h1 = h & (L->strt.size-1); |
85 | TString *ts; | 85 | TString *ts; |
86 | for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) { | 86 | for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) { |
87 | if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) | 87 | if (ts->len == l && (memcmp(str, ts->str, l) == 0)) |
88 | return ts; | 88 | return ts; |
89 | } | 89 | } |
90 | /* not found */ | 90 | /* not found */ |
91 | ts = (TString *)luaM_malloc(L, sizestring(l)); | 91 | ts = (TString *)luaM_malloc(L, sizestring(l)); |
92 | ts->marked = 0; | 92 | ts->marked = 0; |
93 | ts->nexthash = NULL; | 93 | ts->nexthash = NULL; |
94 | ts->u.s.len = l; | 94 | ts->len = l; |
95 | ts->u.s.hash = h; | 95 | ts->u.s.hash = h; |
96 | ts->u.s.constindex = 0; | 96 | ts->u.s.constindex = 0; |
97 | memcpy(ts->str, str, l); | 97 | memcpy(ts->str, str, l); |
@@ -102,22 +102,31 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
102 | } | 102 | } |
103 | 103 | ||
104 | 104 | ||
105 | TString *luaS_newudata (lua_State *L, size_t s, void *udata) { | ||
106 | TString *ts = (TString *)luaM_malloc(L, (lint32)sizeof(TString)+s); | ||
107 | ts->marked = 0; | ||
108 | ts->nexthash = NULL; | ||
109 | ts->len = s; | ||
110 | ts->u.d.tag = 0; | ||
111 | ts->u.d.value = (udata == NULL) ? ts+1 : udata; | ||
112 | L->nblocks += sizestring(s); | ||
113 | /* insert it on table */ | ||
114 | newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1)); | ||
115 | return ts; | ||
116 | } | ||
117 | |||
118 | |||
105 | TString *luaS_createudata (lua_State *L, void *udata, int tag) { | 119 | TString *luaS_createudata (lua_State *L, void *udata, int tag) { |
106 | unsigned long h = IntPoint(udata); | 120 | int h1 = IntPoint(udata) & (L->udt.size-1); |
107 | int h1 = h&(L->udt.size-1); | ||
108 | TString *ts; | 121 | TString *ts; |
109 | for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) { | 122 | for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) { |
110 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) | 123 | if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) |
111 | return ts; | 124 | return ts; |
112 | } | 125 | } |
113 | /* not found */ | 126 | /* not found */ |
114 | ts = luaM_new(L, TString); | 127 | ts = luaS_newudata(L, 0, udata); |
115 | ts->marked = 0; | 128 | if (tag != LUA_ANYTAG) |
116 | ts->nexthash = NULL; | 129 | ts->u.d.tag = tag; |
117 | ts->u.d.value = udata; | ||
118 | ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; | ||
119 | L->nblocks += gcsizeudata; | ||
120 | newentry(L, &L->udt, ts, h1); /* insert it on table */ | ||
121 | return ts; | 130 | return ts; |
122 | } | 131 | } |
123 | 132 | ||