lua/lstring.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lstring.c,v 1.2 2004/03/23 05:09:14 jbj Exp $
00003 ** String table (keeps all strings handled by Lua)
00004 ** See Copyright Notice in lua.h
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   /* rehash */
00033   for (i=0; i<tb->size; i++) {
00034     GCObject *p = tb->hash[i];
00035     while (p) {  /* for each node in the list */
00036       GCObject *next = p->gch.next;  /* save next */
00037       lu_hash h = gcotots(p)->tsv.hash;
00038       int h1 = lmod(h, newsize);  /* new position */
00039       lua_assert(cast(int, h%newsize) == lmod(h, newsize));
00040       p->gch.next = newhash[h1];  /* chain it */
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 /*@null@*/
00052 static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h)
00053         /*@modifies L @*/
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';  /* ending 0 */
00064   tb = &G(L)->strt;
00065   h = lmod(h, tb->size);
00066   ts->tsv.next = tb->hash[h];  /* chain new entry */
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);  /* too crowded */
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;  /* seed */
00078   size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
00079   size_t l1;
00080   for (l1=l; l1>=step; l1-=step)  /* compute hash */
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);  /* not found */
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);  /* is not finalized */
00097   u->uv.tt = LUA_TUSERDATA;
00098   u->uv.len = s;
00099   u->uv.metatable = hvalue(defaultmeta(L));
00100   /* chain it on udata list */
00101   u->uv.next = G(L)->rootudata;
00102   G(L)->rootudata = valtogco(u);
00103   return u;
00104 }
00105 

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2