00001
00002
00003
00004
00005
00006
00007
00008 #include <string.h>
00009
00010 #define lstring_c
00011
00012 #include "lua.h"
00013
00014 #include "lmem.h"
00015 #include "lobject.h"
00016 #include "lstate.h"
00017 #include "lstring.h"
00018
00019
00020
00021 void luaS_freeall (lua_State *L) {
00022 lua_assert(G(L)->strt.nuse==0);
00023 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
00024 }
00025
00026
00027 void luaS_resize (lua_State *L, int newsize) {
00028 GCObject **newhash = luaM_newvector(L, newsize, GCObject *);
00029 stringtable *tb = &G(L)->strt;
00030 int i;
00031 for (i=0; i<newsize; i++) newhash[i] = NULL;
00032
00033 for (i=0; i<tb->size; i++) {
00034 GCObject *p = tb->hash[i];
00035 while (p) {
00036 GCObject *next = p->gch.next;
00037 lu_hash h = gcotots(p)->tsv.hash;
00038 int h1 = lmod(h, newsize);
00039 lua_assert(cast(int, h%newsize) == lmod(h, newsize));
00040 p->gch.next = newhash[h1];
00041 newhash[h1] = p;
00042 p = next;
00043 }
00044 }
00045 luaM_freearray(L, tb->hash, tb->size, TString *);
00046 tb->size = newsize;
00047 tb->hash = newhash;
00048 }
00049
00050
00051
00052 static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h)
00053
00054 {
00055 TString *ts = cast(TString *, luaM_malloc(L, sizestring(l)));
00056 stringtable *tb;
00057 ts->tsv.len = l;
00058 ts->tsv.hash = h;
00059 ts->tsv.marked = 0;
00060 ts->tsv.tt = LUA_TSTRING;
00061 ts->tsv.reserved = 0;
00062 memcpy(ts+1, str, l*sizeof(char));
00063 ((char *)(ts+1))[l] = '\0';
00064 tb = &G(L)->strt;
00065 h = lmod(h, tb->size);
00066 ts->tsv.next = tb->hash[h];
00067 tb->hash[h] = valtogco(ts);
00068 tb->nuse++;
00069 if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2)
00070 luaS_resize(L, tb->size*2);
00071 return ts;
00072 }
00073
00074
00075 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
00076 GCObject *o;
00077 lu_hash h = (lu_hash)l;
00078 size_t step = (l>>5)+1;
00079 size_t l1;
00080 for (l1=l; l1>=step; l1-=step)
00081 h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1]));
00082 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
00083 o != NULL;
00084 o = o->gch.next) {
00085 TString *ts = gcotots(o);
00086 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
00087 return ts;
00088 }
00089 return newlstr(L, str, l, h);
00090 }
00091
00092
00093 Udata *luaS_newudata (lua_State *L, size_t s) {
00094 Udata *u;
00095 u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
00096 u->uv.marked = (1<<1);
00097 u->uv.tt = LUA_TUSERDATA;
00098 u->uv.len = s;
00099 u->uv.metatable = hvalue(defaultmeta(L));
00100
00101 u->uv.next = G(L)->rootudata;
00102 G(L)->rootudata = valtogco(u);
00103 return u;
00104 }
00105