diff options
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 34 |
1 files changed, 31 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.46 2015/01/16 16:54:37 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 | */ |
@@ -22,6 +22,8 @@ | |||
22 | #include "lstring.h" | 22 | #include "lstring.h" |
23 | 23 | ||
24 | 24 | ||
25 | #define MEMERRMSG "not enough memory" | ||
26 | |||
25 | 27 | ||
26 | /* | 28 | /* |
27 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to | 29 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to |
@@ -85,6 +87,21 @@ void luaS_resize (lua_State *L, int newsize) { | |||
85 | } | 87 | } |
86 | 88 | ||
87 | 89 | ||
90 | /* | ||
91 | ** Initialize the string table and the string cache | ||
92 | */ | ||
93 | void luaS_init (lua_State *L) { | ||
94 | global_State *g = G(L); | ||
95 | int i; | ||
96 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | ||
97 | /* pre-create memory-error message */ | ||
98 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); | ||
99 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ | ||
100 | for (i = 0; i < STRCACHE_SIZE; i++) | ||
101 | g->strcache[i] = g->memerrmsg; /* fill cache with valid strings */ | ||
102 | } | ||
103 | |||
104 | |||
88 | 105 | ||
89 | /* | 106 | /* |
90 | ** creates a new string object | 107 | ** creates a new string object |
@@ -163,10 +180,21 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
163 | 180 | ||
164 | 181 | ||
165 | /* | 182 | /* |
166 | ** new zero-terminated string | 183 | ** Create or reuse a zero-terminated string, first checking in the |
184 | ** cache (using the string address as a key). The cache can contain | ||
185 | ** only zero-terminated strings, so it is safe to use 'strcmp' to | ||
186 | ** check hits. | ||
167 | */ | 187 | */ |
168 | TString *luaS_new (lua_State *L, const char *str) { | 188 | TString *luaS_new (lua_State *L, const char *str) { |
169 | return luaS_newlstr(L, str, strlen(str)); | 189 | unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */ |
190 | TString **p = &G(L)->strcache[i]; | ||
191 | if (strcmp(str, getstr(*p)) == 0) /* hit? */ | ||
192 | return *p; /* that it is */ | ||
193 | else { /* normal route */ | ||
194 | TString *s = luaS_newlstr(L, str, strlen(str)); | ||
195 | *p = s; | ||
196 | return s; | ||
197 | } | ||
170 | } | 198 | } |
171 | 199 | ||
172 | 200 | ||