LCOV - code coverage report
Current view: directory - src - lstring.c Found Hit Coverage
Test: Lua 5.1.4 Lines: 62 59 95.2 %
Date: 2009-09-13
Colors: not hit hit

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

Generated by: LCOV version 1.7