Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <string.h>
00009
00010 #define lstring_c
00011 #define LUA_CORE
00012
00013 #include "lua.h"
00014
00015 #include "lmem.h"
00016 #include "lobject.h"
00017 #include "lstate.h"
00018 #include "lstring.h"
00019
00020
00021
00022 void luaS_resize (lua_State *L, int newsize) {
00023 int i;
00024 stringtable *tb = &G(L)->strt;
00025
00026 luaC_runtilstate(L, ~bitmask(GCSsweepstring));
00027 if (newsize > tb->size) {
00028 luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
00029 for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
00030 }
00031
00032 for (i=0; i<tb->size; i++) {
00033 GCObject *p = tb->hash[i];
00034 tb->hash[i] = NULL;
00035 while (p) {
00036 GCObject *next = gch(p)->next;
00037 unsigned int h = lmod(gco2ts(p)->hash, newsize);
00038 gch(p)->next = tb->hash[h];
00039 tb->hash[h] = p;
00040 resetoldbit(p);
00041 p = next;
00042 }
00043 }
00044 if (newsize < tb->size) {
00045
00046 lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
00047 luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
00048 }
00049 tb->size = newsize;
00050 }
00051
00052
00053 static TString *newlstr (lua_State *L, const char *str, size_t l,
00054 unsigned int h) {
00055 size_t totalsize;
00056 GCObject **list;
00057 TString *ts;
00058 stringtable *tb = &G(L)->strt;
00059 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
00060 luaM_toobig(L);
00061 if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
00062 luaS_resize(L, tb->size*2);
00063 totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
00064 list = &tb->hash[lmod(h, tb->size)];
00065 ts = &luaC_newobj(L, LUA_TSTRING, totalsize, list, 0)->ts;
00066 ts->tsv.len = l;
00067 ts->tsv.hash = h;
00068 ts->tsv.reserved = 0;
00069 memcpy(ts+1, str, l*sizeof(char));
00070 ((char *)(ts+1))[l] = '\0';
00071 tb->nuse++;
00072 return ts;
00073 }
00074
00075
00076 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
00077 GCObject *o;
00078 unsigned int h = cast(unsigned int, l);
00079 size_t step = (l>>5)+1;
00080 size_t l1;
00081 for (l1=l; l1>=step; l1-=step)
00082 h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
00083 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
00084 o != NULL;
00085 o = gch(o)->next) {
00086 TString *ts = rawgco2ts(o);
00087 if (h == ts->tsv.hash &&
00088 ts->tsv.len == l &&
00089 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
00090 if (isdead(G(L), o))
00091 changewhite(o);
00092 return ts;
00093 }
00094 }
00095 return newlstr(L, str, l, h);
00096 }
00097
00098
00099 TString *luaS_new (lua_State *L, const char *str) {
00100 return luaS_newlstr(L, str, strlen(str));
00101 }
00102
00103
00104 Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
00105 Udata *u;
00106 if (s > MAX_SIZET - sizeof(Udata))
00107 luaM_toobig(L);
00108 u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
00109 u->uv.len = s;
00110 u->uv.metatable = NULL;
00111 u->uv.env = e;
00112 return u;
00113 }
00114