aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-09-08 12:41:05 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-09-08 12:41:05 -0300
commit41964648eea1427d53934b886abb68cc8457b019 (patch)
treeb0388dfebe6614d5d49306193faf78f8b9e1a6a1 /lstring.c
parent502214f8a551cd01d94677f98a40aa51531ef71d (diff)
downloadlua-41964648eea1427d53934b886abb68cc8457b019.tar.gz
lua-41964648eea1427d53934b886abb68cc8457b019.tar.bz2
lua-41964648eea1427d53934b886abb68cc8457b019.zip
long strings are created directly in final position when possible
(instead of using an auxiliar buffer to first create the string and then allocate the final string and copy result there)
Diffstat (limited to 'lstring.c')
-rw-r--r--lstring.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/lstring.c b/lstring.c
index c53325d0..588ee546 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 2.49 2015/06/01 16:34:37 roberto Exp roberto $ 2** $Id: lstring.c,v 2.50 2015/06/18 14:20:32 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*/
@@ -119,8 +119,7 @@ void luaS_init (lua_State *L) {
119/* 119/*
120** creates a new string object 120** creates a new string object
121*/ 121*/
122static TString *createstrobj (lua_State *L, const char *str, size_t l, 122static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
123 int tag, unsigned int h) {
124 TString *ts; 123 TString *ts;
125 GCObject *o; 124 GCObject *o;
126 size_t totalsize; /* total size of TString object */ 125 size_t totalsize; /* total size of TString object */
@@ -129,12 +128,18 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
129 ts = gco2ts(o); 128 ts = gco2ts(o);
130 ts->hash = h; 129 ts->hash = h;
131 ts->extra = 0; 130 ts->extra = 0;
132 memcpy(getaddrstr(ts), str, l * sizeof(char));
133 getaddrstr(ts)[l] = '\0'; /* ending 0 */ 131 getaddrstr(ts)[l] = '\0'; /* ending 0 */
134 return ts; 132 return ts;
135} 133}
136 134
137 135
136TString *luaS_createlngstrobj (lua_State *L, size_t l) {
137 TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
138 ts->u.lnglen = l;
139 return ts;
140}
141
142
138void luaS_remove (lua_State *L, TString *ts) { 143void luaS_remove (lua_State *L, TString *ts) {
139 stringtable *tb = &G(L)->strt; 144 stringtable *tb = &G(L)->strt;
140 TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 145 TString **p = &tb->hash[lmod(ts->hash, tb->size)];
@@ -166,7 +171,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
166 luaS_resize(L, g->strt.size * 2); 171 luaS_resize(L, g->strt.size * 2);
167 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ 172 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
168 } 173 }
169 ts = createstrobj(L, str, l, LUA_TSHRSTR, h); 174 ts = createstrobj(L, l, LUA_TSHRSTR, h);
175 memcpy(getaddrstr(ts), str, l * sizeof(char));
170 ts->shrlen = cast_byte(l); 176 ts->shrlen = cast_byte(l);
171 ts->u.hnext = *list; 177 ts->u.hnext = *list;
172 *list = ts; 178 *list = ts;
@@ -185,8 +191,8 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
185 TString *ts; 191 TString *ts;
186 if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) 192 if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
187 luaM_toobig(L); 193 luaM_toobig(L);
188 ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); 194 ts = luaS_createlngstrobj(L, l);
189 ts->u.lnglen = l; 195 memcpy(getaddrstr(ts), str, l * sizeof(char));
190 return ts; 196 return ts;
191 } 197 }
192} 198}