diff src/lua/lstring.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/lstring.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/lstring.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 +** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** String table (keeps all strings handled by Lua)
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <string.h>
    1.12 +
    1.13 +#define lstring_c
    1.14 +#define LUA_CORE
    1.15 +
    1.16 +#include "lua.h"
    1.17 +
    1.18 +#include "lmem.h"
    1.19 +#include "lobject.h"
    1.20 +#include "lstate.h"
    1.21 +#include "lstring.h"
    1.22 +
    1.23 +
    1.24 +
    1.25 +void luaS_resize (lua_State *L, int newsize) {
    1.26 +  GCObject **newhash;
    1.27 +  stringtable *tb;
    1.28 +  int i;
    1.29 +  if (G(L)->gcstate == GCSsweepstring)
    1.30 +    return;  /* cannot resize during GC traverse */
    1.31 +  newhash = luaM_newvector(L, newsize, GCObject *);
    1.32 +  tb = &G(L)->strt;
    1.33 +  for (i=0; i<newsize; i++) newhash[i] = NULL;
    1.34 +  /* rehash */
    1.35 +  for (i=0; i<tb->size; i++) {
    1.36 +    GCObject *p = tb->hash[i];
    1.37 +    while (p) {  /* for each node in the list */
    1.38 +      GCObject *next = p->gch.next;  /* save next */
    1.39 +      unsigned int h = gco2ts(p)->hash;
    1.40 +      int h1 = lmod(h, newsize);  /* new position */
    1.41 +      lua_assert(cast_int(h%newsize) == lmod(h, newsize));
    1.42 +      p->gch.next = newhash[h1];  /* chain it */
    1.43 +      newhash[h1] = p;
    1.44 +      p = next;
    1.45 +    }
    1.46 +  }
    1.47 +  luaM_freearray(L, tb->hash, tb->size, TString *);
    1.48 +  tb->size = newsize;
    1.49 +  tb->hash = newhash;
    1.50 +}
    1.51 +
    1.52 +
    1.53 +static TString *newlstr (lua_State *L, const char *str, size_t l,
    1.54 +                                       unsigned int h) {
    1.55 +  TString *ts;
    1.56 +  stringtable *tb;
    1.57 +  if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
    1.58 +    luaM_toobig(L);
    1.59 +  ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
    1.60 +  ts->tsv.len = l;
    1.61 +  ts->tsv.hash = h;
    1.62 +  ts->tsv.marked = luaC_white(G(L));
    1.63 +  ts->tsv.tt = LUA_TSTRING;
    1.64 +  ts->tsv.reserved = 0;
    1.65 +  memcpy(ts+1, str, l*sizeof(char));
    1.66 +  ((char *)(ts+1))[l] = '\0';  /* ending 0 */
    1.67 +  tb = &G(L)->strt;
    1.68 +  h = lmod(h, tb->size);
    1.69 +  ts->tsv.next = tb->hash[h];  /* chain new entry */
    1.70 +  tb->hash[h] = obj2gco(ts);
    1.71 +  tb->nuse++;
    1.72 +  if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
    1.73 +    luaS_resize(L, tb->size*2);  /* too crowded */
    1.74 +  return ts;
    1.75 +}
    1.76 +
    1.77 +
    1.78 +TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
    1.79 +  GCObject *o;
    1.80 +  unsigned int h = cast(unsigned int, l);  /* seed */
    1.81 +  size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
    1.82 +  size_t l1;
    1.83 +  for (l1=l; l1>=step; l1-=step)  /* compute hash */
    1.84 +    h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
    1.85 +  for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
    1.86 +       o != NULL;
    1.87 +       o = o->gch.next) {
    1.88 +    TString *ts = rawgco2ts(o);
    1.89 +    if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
    1.90 +      /* string may be dead */
    1.91 +      if (isdead(G(L), o)) changewhite(o);
    1.92 +      return ts;
    1.93 +    }
    1.94 +  }
    1.95 +  return newlstr(L, str, l, h);  /* not found */
    1.96 +}
    1.97 +
    1.98 +
    1.99 +Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
   1.100 +  Udata *u;
   1.101 +  if (s > MAX_SIZET - sizeof(Udata))
   1.102 +    luaM_toobig(L);
   1.103 +  u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
   1.104 +  u->uv.marked = luaC_white(G(L));  /* is not finalized */
   1.105 +  u->uv.tt = LUA_TUSERDATA;
   1.106 +  u->uv.len = s;
   1.107 +  u->uv.metatable = NULL;
   1.108 +  u->uv.env = e;
   1.109 +  /* chain it on udata list (after main thread) */
   1.110 +  u->uv.next = G(L)->mainthread->next;
   1.111 +  G(L)->mainthread->next = obj2gco(u);
   1.112 +  return u;
   1.113 +}
   1.114 +