lua/lstring.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lstring.c,v 2.19 2011/05/03 16:01:57 roberto 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 #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   /* cannot resize while GC is traversing strings */
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   /* rehash */
00032   for (i=0; i<tb->size; i++) {
00033     GCObject *p = tb->hash[i];
00034     tb->hash[i] = NULL;
00035     while (p) {  /* for each node in the list */
00036       GCObject *next = gch(p)->next;  /* save next */
00037       unsigned int h = lmod(gco2ts(p)->hash, newsize);  /* new position */
00038       gch(p)->next = tb->hash[h];  /* chain it */
00039       tb->hash[h] = p;
00040       resetoldbit(p);  /* see MOVE OLD rule */
00041       p = next;
00042     }
00043   }
00044   if (newsize < tb->size) {
00045     /* shrinking slice must be empty */
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;  /* total size of TString object */
00056   GCObject **list;  /* (pointer to) list where it will be inserted */
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);  /* too crowded */
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';  /* ending 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);  /* seed */
00079   size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
00080   size_t l1;
00081   for (l1=l; l1>=step; l1-=step)  /* compute hash */
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))  /* string is dead (but was not collected yet)? */
00091         changewhite(o);  /* resurrect it */
00092       return ts;
00093     }
00094   }
00095   return newlstr(L, str, l, h);  /* not found; create a new string */
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 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:04 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs