diff options
| author | William Wilgus <me.theuser@yahoo.com> | 2018-11-11 18:27:19 -0500 |
|---|---|---|
| committer | William Wilgus <me.theuser@yahoo.com> | 2019-07-11 00:31:41 +0200 |
| commit | c6fcb1cf45b249b059c2d53a062f9a6c7f449047 (patch) | |
| tree | 229eacd12461605a8320eb7590ff83eea13538d6 /apps/plugins/lua/lstring.c | |
| parent | 42240f6990156fb907a7dce7258be2f1235fab44 (diff) | |
| download | rockbox-c6fcb1cf45b249b059c2d53a062f9a6c7f449047.zip rockbox-c6fcb1cf45b249b059c2d53a062f9a6c7f449047.tar.gz rockbox-c6fcb1cf45b249b059c2d53a062f9a6c7f449047.tar.bz2 rockbox-c6fcb1cf45b249b059c2d53a062f9a6c7f449047.tar.xz | |
lua inbinary strings
Allows saving of ram by reusing strings already stored in the binary
and storing a pointer instead of malloc and copy to get them inside
the lua state
Saves about 1.5K overall
Derivative of work by bogdanm
RAM optimizations: pseudo RO strings, functions in Flash
https://github.com/elua/elua/commit/d54659b5723bcd2b1e3900362398c72c18a9aa0b
Change-Id: I21d6dcfa32523877efd9f70fb0f88f2a02872649
Diffstat (limited to 'apps/plugins/lua/lstring.c')
| -rw-r--r-- | apps/plugins/lua/lstring.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/apps/plugins/lua/lstring.c b/apps/plugins/lua/lstring.c index 4911315..bf0536e 100644 --- a/apps/plugins/lua/lstring.c +++ b/apps/plugins/lua/lstring.c @@ -2,6 +2,8 @@ ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h +** luaS_newllocstr is adapted from "elua -- pseudo RO strings" +** by bogdanm, distributed under a MIT license. */ @@ -48,19 +50,26 @@ void luaS_resize (lua_State *L, int newsize) { static TString *newlstr (lua_State *L, const char *str, size_t l, - unsigned int h) { + unsigned int h, char type) { TString *ts; stringtable *tb; - if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) + if (l > ((MAX_SIZET - sizeof(TString))/sizeof(char)) - sizeof("")) luaM_toobig(L); - ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); + ts = cast(TString *, luaM_malloc(L, sizetstring(type, l))); ts->tsv.len = l; ts->tsv.hash = h; ts->tsv.marked = luaC_white(G(L)); + if (testbits(type, TSTR_FIXED)) + luaS_fix(ts); ts->tsv.tt = LUA_TSTRING; ts->tsv.reserved = 0; - memcpy(ts+1, str, l*sizeof(char)); - ((char *)(ts+1))[l] = '\0'; /* ending 0 */ + ts->tsv.type = cast_byte(type); + if (testbits(type, TSTR_INBIN)) /* ROCKLUA ADDED */ + *(const char **)(ts+1) = str; /* store a pointer to the string instead */ + else { + memcpy(ts+1, str, l*sizeof(char)); + ((char *)(ts+1))[l] = '\0'; /* ending 0 */ + } tb = &G(L)->strt; h = lmod(h, tb->size); ts->tsv.next = tb->hash[h]; /* chain new entry */ @@ -72,8 +81,16 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, } -TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { +TString *luaS_newllocstr (lua_State *L, const char *str, size_t l, char type) { GCObject *o; + if (testbits(type, TSTR_CHKSZ)) + l = strlen(str); +#ifdef INBINARYSTRINGS + else if (!testbits(type, TSTR_ISLIT)) +#else + if (true) +#endif + resetbits(type, TSTR_INBIN); /* only whole strings can be used inbin */ unsigned int h = cast(unsigned int, l); /* seed */ size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ size_t l1; @@ -86,10 +103,12 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { /* string may be dead */ if (isdead(G(L), o)) changewhite(o); + if (testbits(type, TSTR_FIXED)) + luaS_fix(ts); return ts; } } - return newlstr(L, str, l, h); /* not found */ + return newlstr(L, str, l, h, type); /* not found */ } |