annotate src/lua/lstring.c @ 301:1de6aada1cb7

inserted backwards jump into bootstrap program to fit it into item-pc space
author Robert McIntyre <rlm@mit.edu>
date Fri, 30 Mar 2012 23:39:58 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
rlm@1 3 ** String table (keeps all strings handled by Lua)
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <string.h>
rlm@1 9
rlm@1 10 #define lstring_c
rlm@1 11 #define LUA_CORE
rlm@1 12
rlm@1 13 #include "lua.h"
rlm@1 14
rlm@1 15 #include "lmem.h"
rlm@1 16 #include "lobject.h"
rlm@1 17 #include "lstate.h"
rlm@1 18 #include "lstring.h"
rlm@1 19
rlm@1 20
rlm@1 21
rlm@1 22 void luaS_resize (lua_State *L, int newsize) {
rlm@1 23 GCObject **newhash;
rlm@1 24 stringtable *tb;
rlm@1 25 int i;
rlm@1 26 if (G(L)->gcstate == GCSsweepstring)
rlm@1 27 return; /* cannot resize during GC traverse */
rlm@1 28 newhash = luaM_newvector(L, newsize, GCObject *);
rlm@1 29 tb = &G(L)->strt;
rlm@1 30 for (i=0; i<newsize; i++) newhash[i] = NULL;
rlm@1 31 /* rehash */
rlm@1 32 for (i=0; i<tb->size; i++) {
rlm@1 33 GCObject *p = tb->hash[i];
rlm@1 34 while (p) { /* for each node in the list */
rlm@1 35 GCObject *next = p->gch.next; /* save next */
rlm@1 36 unsigned int h = gco2ts(p)->hash;
rlm@1 37 int h1 = lmod(h, newsize); /* new position */
rlm@1 38 lua_assert(cast_int(h%newsize) == lmod(h, newsize));
rlm@1 39 p->gch.next = newhash[h1]; /* chain it */
rlm@1 40 newhash[h1] = p;
rlm@1 41 p = next;
rlm@1 42 }
rlm@1 43 }
rlm@1 44 luaM_freearray(L, tb->hash, tb->size, TString *);
rlm@1 45 tb->size = newsize;
rlm@1 46 tb->hash = newhash;
rlm@1 47 }
rlm@1 48
rlm@1 49
rlm@1 50 static TString *newlstr (lua_State *L, const char *str, size_t l,
rlm@1 51 unsigned int h) {
rlm@1 52 TString *ts;
rlm@1 53 stringtable *tb;
rlm@1 54 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
rlm@1 55 luaM_toobig(L);
rlm@1 56 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
rlm@1 57 ts->tsv.len = l;
rlm@1 58 ts->tsv.hash = h;
rlm@1 59 ts->tsv.marked = luaC_white(G(L));
rlm@1 60 ts->tsv.tt = LUA_TSTRING;
rlm@1 61 ts->tsv.reserved = 0;
rlm@1 62 memcpy(ts+1, str, l*sizeof(char));
rlm@1 63 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
rlm@1 64 tb = &G(L)->strt;
rlm@1 65 h = lmod(h, tb->size);
rlm@1 66 ts->tsv.next = tb->hash[h]; /* chain new entry */
rlm@1 67 tb->hash[h] = obj2gco(ts);
rlm@1 68 tb->nuse++;
rlm@1 69 if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
rlm@1 70 luaS_resize(L, tb->size*2); /* too crowded */
rlm@1 71 return ts;
rlm@1 72 }
rlm@1 73
rlm@1 74
rlm@1 75 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
rlm@1 76 GCObject *o;
rlm@1 77 unsigned int h = cast(unsigned int, l); /* seed */
rlm@1 78 size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
rlm@1 79 size_t l1;
rlm@1 80 for (l1=l; l1>=step; l1-=step) /* compute hash */
rlm@1 81 h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
rlm@1 82 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
rlm@1 83 o != NULL;
rlm@1 84 o = o->gch.next) {
rlm@1 85 TString *ts = rawgco2ts(o);
rlm@1 86 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
rlm@1 87 /* string may be dead */
rlm@1 88 if (isdead(G(L), o)) changewhite(o);
rlm@1 89 return ts;
rlm@1 90 }
rlm@1 91 }
rlm@1 92 return newlstr(L, str, l, h); /* not found */
rlm@1 93 }
rlm@1 94
rlm@1 95
rlm@1 96 Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
rlm@1 97 Udata *u;
rlm@1 98 if (s > MAX_SIZET - sizeof(Udata))
rlm@1 99 luaM_toobig(L);
rlm@1 100 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
rlm@1 101 u->uv.marked = luaC_white(G(L)); /* is not finalized */
rlm@1 102 u->uv.tt = LUA_TUSERDATA;
rlm@1 103 u->uv.len = s;
rlm@1 104 u->uv.metatable = NULL;
rlm@1 105 u->uv.env = e;
rlm@1 106 /* chain it on udata list (after main thread) */
rlm@1 107 u->uv.next = G(L)->mainthread->next;
rlm@1 108 G(L)->mainthread->next = obj2gco(u);
rlm@1 109 return u;
rlm@1 110 }
rlm@1 111