Lua 5.1.4: lstring.c
L0001
L0006
L0007
L0008 #include <string.h>
L0009
L0010 #define lstring_c
L0011 #define LUA_CORE
L0012
L0013 #include "lua.h"
L0014
L0015 #include "lmem.h"
L0016 #include "lobject.h"
L0017 #include "lstate.h"
L0018 #include "lstring.h"
L0019
L0020
L0021
L0022 void luaS_resize (lua_State *L, int newsize) {
L0023 GCObject **newhash;
L0024 stringtable *tb;
L0025 int i;
L0026 if (G(L)->gcstate == GCSsweepstring)
L0027 return;
L0028 newhash = luaM_newvector(L, newsize, GCObject *);
L0029 tb = &G(L)->strt;
L0030 for (i=0; i<newsize; i++) newhash[i] = NULL;
L0031
L0032 for (i=0; i<tb->size; i++) {
L0033 GCObject *p = tb->hash[i];
L0034 while (p) {
L0035 GCObject *next = p->gch.next;
L0036 unsigned int h = gco2ts(p)->hash;
L0037 int h1 = lmod(h, newsize);
L0038 lua_assert(cast_int(h%newsize) == lmod(h, newsize));
L0039 p->gch.next = newhash[h1];
L0040 newhash[h1] = p;
L0041 p = next;
L0042 }
L0043 }
L0044 luaM_freearray(L, tb->hash, tb->size, TString *);
L0045 tb->size = newsize;
L0046 tb->hash = newhash;
L0047 }
L0048
L0049
L0050 static TString *newlstr (lua_State *L, const char *str, size_t l,
L0051 unsigned int h) {
L0052 TString *ts;
L0053 stringtable *tb;
L0054 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
L0055 luaM_toobig(L);
L0056 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
L0057 ts->tsv.len = l;
L0058 ts->tsv.hash = h;
L0059 ts->tsv.marked = luaC_white(G(L));
L0060 ts->tsv.tt = LUA_TSTRING;
L0061 ts->tsv.reserved = 0;
L0062 memcpy(ts+1, str, l*sizeof(char));
L0063 ((char *)(ts+1))[l] = '\0';
L0064 tb = &G(L)->strt;
L0065 h = lmod(h, tb->size);
L0066 ts->tsv.next = tb->hash[h];
L0067 tb->hash[h] = obj2gco(ts);
L0068 tb->nuse++;
L0069 if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
L0070 luaS_resize(L, tb->size*2);
L0071 return ts;
L0072 }
L0073
L0074
L0075 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
L0076 GCObject *o;
L0077 unsigned int h = cast(unsigned int, l);
L0078 size_t step = (l>>5)+1;
L0079 size_t l1;
L0080 for (l1=l; l1>=step; l1-=step)
L0081 h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
L0082 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
L0083 o != NULL;
L0084 o = o->gch.next) {
L0085 TString *ts = rawgco2ts(o);
L0086 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
L0087
L0088 if (isdead(G(L), o)) changewhite(o);
L0089 return ts;
L0090 }
L0091 }
L0092 return newlstr(L, str, l, h);
L0093 }
L0094
L0095
L0096 Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
L0097 Udata *u;
L0098 if (s > MAX_SIZET - sizeof(Udata))
L0099 luaM_toobig(L);
L0100 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
L0101 u->uv.marked = luaC_white(G(L));
L0102 u->uv.tt = LUA_TUSERDATA;
L0103 u->uv.len = s;
L0104 u->uv.metatable = NULL;
L0105 u->uv.env = e;
L0106
L0107 u->uv.next = G(L)->mainthread->next;
L0108 G(L)->mainthread->next = obj2gco(u);
L0109 return u;
L0110 }
L0111
Generated by pretty.lua