Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ceguilua/src/lua-5.0.3/lua/lstring.c @ 1808

Last change on this file since 1808 was 1803, checked in by rgrieder, 16 years ago

added files for lua 5.1.3, lua 5.0.3, CEGUILua-0.6.1 and CEGUILua-0.5.0b

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1/*
2** $Id: lstring.c,v 1.78 2002/12/04 17:38:31 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
12#include "lua.h"
13
14#include "lmem.h"
15#include "lobject.h"
16#include "lstate.h"
17#include "lstring.h"
18
19
20
21void luaS_freeall (lua_State *L) {
22  lua_assert(G(L)->strt.nuse==0);
23  luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
24}
25
26
27void luaS_resize (lua_State *L, int newsize) {
28  GCObject **newhash = luaM_newvector(L, newsize, GCObject *);
29  stringtable *tb = &G(L)->strt;
30  int i;
31  for (i=0; i<newsize; i++) newhash[i] = NULL;
32  /* rehash */
33  for (i=0; i<tb->size; i++) {
34    GCObject *p = tb->hash[i];
35    while (p) {  /* for each node in the list */
36      GCObject *next = p->gch.next;  /* save next */
37      lu_hash h = gcotots(p)->tsv.hash;
38      int h1 = lmod(h, newsize);  /* new position */
39      lua_assert(cast(int, h%newsize) == lmod(h, newsize));
40      p->gch.next = newhash[h1];  /* chain it */
41      newhash[h1] = p;
42      p = next;
43    }
44  }
45  luaM_freearray(L, tb->hash, tb->size, TString *);
46  tb->size = newsize;
47  tb->hash = newhash;
48}
49
50
51static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) {
52  TString *ts = cast(TString *, luaM_malloc(L, sizestring(l)));
53  stringtable *tb;
54  ts->tsv.len = l;
55  ts->tsv.hash = h;
56  ts->tsv.marked = 0;
57  ts->tsv.tt = LUA_TSTRING;
58  ts->tsv.reserved = 0;
59  memcpy(ts+1, str, l*sizeof(char));
60  ((char *)(ts+1))[l] = '\0';  /* ending 0 */
61  tb = &G(L)->strt;
62  h = lmod(h, tb->size);
63  ts->tsv.next = tb->hash[h];  /* chain new entry */
64  tb->hash[h] = valtogco(ts);
65  tb->nuse++;
66  if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2)
67    luaS_resize(L, tb->size*2);  /* too crowded */
68  return ts;
69}
70
71
72TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
73  GCObject *o;
74  lu_hash h = (lu_hash)l;  /* seed */
75  size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
76  size_t l1;
77  for (l1=l; l1>=step; l1-=step)  /* compute hash */
78    h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1]));
79  for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
80       o != NULL;
81       o = o->gch.next) {
82    TString *ts = gcotots(o);
83    if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
84      return ts;
85  }
86  return newlstr(L, str, l, h);  /* not found */
87}
88
89
90Udata *luaS_newudata (lua_State *L, size_t s) {
91  Udata *u;
92  u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
93  u->uv.marked = (1<<1);  /* is not finalized */
94  u->uv.tt = LUA_TUSERDATA;
95  u->uv.len = s;
96  u->uv.metatable = hvalue(defaultmeta(L));
97  /* chain it on udata list */
98  u->uv.next = G(L)->rootudata;
99  G(L)->rootudata = valtogco(u);
100  return u;
101}
102
Note: See TracBrowser for help on using the repository browser.