915resolution: Fix build failure with GCC 9
[grub-extras.git] / lua / lstring.c
blobbfdf1908a0f55ca08832611393bff8e076d6a94a
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 */
7 #if 0
8 #include <string.h>
9 #endif
11 #define lstring_c
12 #define LUA_CORE
14 #include "lua.h"
16 #include "lmem.h"
17 #include "lobject.h"
18 #include "lstate.h"
19 #include "lstring.h"
23 void luaS_resize (lua_State *L, int newsize) {
24 GCObject **newhash;
25 stringtable *tb;
26 int i;
27 if (G(L)->gcstate == GCSsweepstring)
28 return; /* cannot resize during GC traverse */
29 newhash = luaM_newvector(L, newsize, GCObject *);
30 tb = &G(L)->strt;
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 unsigned int h = gco2ts(p)->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;
45 luaM_freearray(L, tb->hash, tb->size, TString *);
46 tb->size = newsize;
47 tb->hash = newhash;
51 static TString *newlstr (lua_State *L, const char *str, size_t l,
52 unsigned int h) {
53 TString *ts;
54 stringtable *tb;
55 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
56 luaM_toobig(L);
57 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
58 ts->tsv.len = l;
59 ts->tsv.hash = h;
60 ts->tsv.marked = luaC_white(G(L));
61 ts->tsv.tt = LUA_TSTRING;
62 ts->tsv.reserved = 0;
63 memcpy(ts+1, str, l*sizeof(char));
64 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
65 tb = &G(L)->strt;
66 h = lmod(h, tb->size);
67 ts->tsv.next = tb->hash[h]; /* chain new entry */
68 tb->hash[h] = obj2gco(ts);
69 tb->nuse++;
70 if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
71 luaS_resize(L, tb->size*2); /* too crowded */
72 return ts;
76 TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
77 GCObject *o;
78 unsigned int h = cast(unsigned int, l); /* seed */
79 size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
80 size_t l1;
81 for (l1=l; l1>=step; l1-=step) /* compute hash */
82 h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
83 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
84 o != NULL;
85 o = o->gch.next) {
86 TString *ts = rawgco2ts(o);
87 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
88 /* string may be dead */
89 if (isdead(G(L), o)) changewhite(o);
90 return ts;
93 return newlstr(L, str, l, h); /* not found */
97 Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
98 Udata *u;
99 if (s > MAX_SIZET - sizeof(Udata))
100 luaM_toobig(L);
101 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
102 u->uv.marked = luaC_white(G(L)); /* is not finalized */
103 u->uv.tt = LUA_TUSERDATA;
104 u->uv.len = s;
105 u->uv.metatable = NULL;
106 u->uv.env = e;
107 /* chain it on udata list (after main thread) */
108 u->uv.next = G(L)->mainthread->next;
109 G(L)->mainthread->next = obj2gco(u);
110 return u;