1997-09-17 03:25:59 +08:00
|
|
|
/*
|
1998-06-20 00:14:09 +08:00
|
|
|
** $Id: lstring.c,v 1.12 1998/03/06 16:54:42 roberto Exp roberto $
|
1997-12-09 21:50:08 +08:00
|
|
|
** String table (keeps all strings handled by Lua)
|
1997-09-17 03:25:59 +08:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#include "lstate.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define NUM_HASHS 61
|
|
|
|
|
1997-09-26 23:02:26 +08:00
|
|
|
|
1997-12-09 21:50:08 +08:00
|
|
|
#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
|
1997-10-24 00:26:37 +08:00
|
|
|
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
1998-03-07 00:54:42 +08:00
|
|
|
static TaggedString EMPTY = {{NULL, 2}, 0L, 0,
|
|
|
|
{{{LUA_T_NIL, {NULL}}, 0L}}, {0}};
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
1997-11-04 23:27:53 +08:00
|
|
|
void luaS_init (void)
|
|
|
|
{
|
|
|
|
int i;
|
1997-11-20 01:29:23 +08:00
|
|
|
L->string_root = luaM_newvector(NUM_HASHS, stringtable);
|
1997-11-04 23:27:53 +08:00
|
|
|
for (i=0; i<NUM_HASHS; i++) {
|
1997-11-20 01:29:23 +08:00
|
|
|
L->string_root[i].size = 0;
|
|
|
|
L->string_root[i].nuse = 0;
|
|
|
|
L->string_root[i].hash = NULL;
|
1997-11-04 23:27:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
1998-03-07 00:54:42 +08:00
|
|
|
static unsigned long hash_s (char *s, long l)
|
1997-09-17 03:25:59 +08:00
|
|
|
{
|
1998-03-07 00:54:42 +08:00
|
|
|
unsigned long h = 0;
|
|
|
|
while (l--)
|
|
|
|
h = ((h<<5)-h)^(unsigned char)*(s++);
|
1997-09-17 03:25:59 +08:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:50:33 +08:00
|
|
|
static int newsize (stringtable *tb)
|
|
|
|
{
|
|
|
|
int size = tb->size;
|
|
|
|
int realuse = 0;
|
|
|
|
int i;
|
|
|
|
/* count how many entries are really in use */
|
|
|
|
for (i=0; i<size; i++)
|
|
|
|
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
|
|
|
realuse++;
|
|
|
|
if (2*(realuse+1) <= size) /* +1 is the new element */
|
|
|
|
return size; /* don't need to grow, just rehash to clear EMPTYs */
|
|
|
|
else
|
|
|
|
return luaO_redimension(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
static void grow (stringtable *tb)
|
|
|
|
{
|
1998-01-29 00:50:33 +08:00
|
|
|
|
|
|
|
int ns = newsize(tb);
|
|
|
|
TaggedString **newhash = luaM_newvector(ns, TaggedString *);
|
1997-09-17 03:25:59 +08:00
|
|
|
int i;
|
1998-01-29 00:50:33 +08:00
|
|
|
for (i=0; i<ns; i++)
|
1997-09-17 03:25:59 +08:00
|
|
|
newhash[i] = NULL;
|
|
|
|
/* rehash */
|
|
|
|
tb->nuse = 0;
|
|
|
|
for (i=0; i<tb->size; i++) {
|
|
|
|
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
|
1998-01-29 00:50:33 +08:00
|
|
|
int h = tb->hash[i]->hash%ns;
|
1997-09-17 03:25:59 +08:00
|
|
|
while (newhash[h])
|
1998-01-29 00:50:33 +08:00
|
|
|
h = (h+1)%ns;
|
1997-09-17 03:25:59 +08:00
|
|
|
newhash[h] = tb->hash[i];
|
|
|
|
tb->nuse++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
luaM_free(tb->hash);
|
1998-01-29 00:50:33 +08:00
|
|
|
tb->size = ns;
|
1997-09-17 03:25:59 +08:00
|
|
|
tb->hash = newhash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-03-07 00:54:42 +08:00
|
|
|
static TaggedString *newone_s (char *str, long l, unsigned long h)
|
1997-09-17 03:25:59 +08:00
|
|
|
{
|
1998-03-07 00:54:42 +08:00
|
|
|
TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
|
|
|
|
memcpy(ts->str, str, l);
|
|
|
|
ts->str[l] = 0; /* ending 0 */
|
|
|
|
ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
|
|
|
|
ts->u.s.len = l;
|
|
|
|
ts->constindex = 0;
|
|
|
|
L->nblocks += gcsizestring(l);
|
1997-09-26 23:02:26 +08:00
|
|
|
ts->head.marked = 0;
|
|
|
|
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
|
|
|
ts->hash = h;
|
1997-09-17 03:25:59 +08:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
1998-03-07 00:54:42 +08:00
|
|
|
static TaggedString *newone_u (char *buff, int tag, unsigned long h)
|
|
|
|
{
|
|
|
|
TaggedString *ts = luaM_new(TaggedString);
|
|
|
|
ts->u.d.v = buff;
|
|
|
|
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
|
|
|
|
ts->constindex = -1; /* tag -> this is a userdata */
|
|
|
|
L->nblocks++;
|
|
|
|
ts->head.marked = 0;
|
|
|
|
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
|
|
|
ts->hash = h;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TaggedString *insert_s (char *str, long l, stringtable *tb)
|
|
|
|
{
|
|
|
|
TaggedString *ts;
|
|
|
|
unsigned long h = hash_s(str, l);
|
|
|
|
int size = tb->size;
|
|
|
|
int i;
|
|
|
|
int j = -1;
|
|
|
|
if ((long)tb->nuse*3 >= (long)size*2) {
|
|
|
|
grow(tb);
|
|
|
|
size = tb->size;
|
|
|
|
}
|
|
|
|
for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
|
|
|
|
if (ts == &EMPTY)
|
|
|
|
j = i;
|
|
|
|
else if (ts->constindex >= 0 &&
|
|
|
|
ts->u.s.len == l &&
|
|
|
|
(memcmp(str, ts->str, l) == 0))
|
|
|
|
return ts;
|
|
|
|
if (++i == size) i=0;
|
|
|
|
}
|
|
|
|
/* not found */
|
|
|
|
if (j != -1) /* is there an EMPTY space? */
|
|
|
|
i = j;
|
|
|
|
else
|
|
|
|
tb->nuse++;
|
|
|
|
ts = tb->hash[i] = newone_s(str, l, h);
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TaggedString *insert_u (void *buff, int tag, stringtable *tb)
|
1997-09-17 03:25:59 +08:00
|
|
|
{
|
|
|
|
TaggedString *ts;
|
1998-03-07 00:54:42 +08:00
|
|
|
unsigned long h = (unsigned long)buff;
|
1998-01-14 02:06:27 +08:00
|
|
|
int size = tb->size;
|
1997-09-17 03:25:59 +08:00
|
|
|
int i;
|
|
|
|
int j = -1;
|
1998-01-14 02:06:27 +08:00
|
|
|
if ((long)tb->nuse*3 >= (long)size*2) {
|
1997-09-17 03:25:59 +08:00
|
|
|
grow(tb);
|
1998-01-14 02:06:27 +08:00
|
|
|
size = tb->size;
|
|
|
|
}
|
|
|
|
for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
|
1997-09-17 03:25:59 +08:00
|
|
|
if (ts == &EMPTY)
|
|
|
|
j = i;
|
1998-03-07 00:54:42 +08:00
|
|
|
else if (ts->constindex < 0 && /* is a udata? */
|
|
|
|
(tag == ts->u.d.tag || tag == LUA_ANYTAG) &&
|
|
|
|
buff == ts->u.d.v)
|
1997-09-17 03:25:59 +08:00
|
|
|
return ts;
|
1998-01-14 02:06:27 +08:00
|
|
|
if (++i == size) i=0;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
/* not found */
|
|
|
|
if (j != -1) /* is there an EMPTY space? */
|
|
|
|
i = j;
|
|
|
|
else
|
|
|
|
tb->nuse++;
|
1998-03-07 00:54:42 +08:00
|
|
|
ts = tb->hash[i] = newone_u(buff, tag, h);
|
1997-09-17 03:25:59 +08:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
TaggedString *luaS_createudata (void *udata, int tag)
|
|
|
|
{
|
1998-03-07 00:54:42 +08:00
|
|
|
return insert_u(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TaggedString *luaS_newlstr (char *str, long l)
|
|
|
|
{
|
|
|
|
int i = (l==0)?0:(unsigned char)str[0];
|
|
|
|
return insert_s(str, l, &L->string_root[i%NUM_HASHS]);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TaggedString *luaS_new (char *str)
|
|
|
|
{
|
1998-03-07 00:54:42 +08:00
|
|
|
return luaS_newlstr(str, strlen(str));
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TaggedString *luaS_newfixedstring (char *str)
|
|
|
|
{
|
|
|
|
TaggedString *ts = luaS_new(str);
|
1997-09-26 23:02:26 +08:00
|
|
|
if (ts->head.marked == 0)
|
|
|
|
ts->head.marked = 2; /* avoid GC */
|
1997-09-17 03:25:59 +08:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaS_free (TaggedString *l)
|
|
|
|
{
|
|
|
|
while (l) {
|
1997-09-26 23:02:26 +08:00
|
|
|
TaggedString *next = (TaggedString *)l->head.next;
|
1998-03-07 00:54:42 +08:00
|
|
|
L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len);
|
1997-09-17 03:25:59 +08:00
|
|
|
luaM_free(l);
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-09-26 23:02:26 +08:00
|
|
|
** Garbage collection functions.
|
1997-09-17 03:25:59 +08:00
|
|
|
*/
|
1997-09-26 23:02:26 +08:00
|
|
|
|
|
|
|
static void remove_from_list (GCnode *l)
|
|
|
|
{
|
|
|
|
while (l) {
|
|
|
|
GCnode *next = l->next;
|
|
|
|
while (next && !next->marked)
|
|
|
|
next = l->next = next->next;
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
TaggedString *luaS_collector (void)
|
|
|
|
{
|
|
|
|
TaggedString *frees = NULL;
|
|
|
|
int i;
|
1997-11-20 01:29:23 +08:00
|
|
|
remove_from_list(&(L->rootglobal));
|
1997-09-17 03:25:59 +08:00
|
|
|
for (i=0; i<NUM_HASHS; i++) {
|
1997-11-20 01:29:23 +08:00
|
|
|
stringtable *tb = &L->string_root[i];
|
1997-09-17 03:25:59 +08:00
|
|
|
int j;
|
|
|
|
for (j=0; j<tb->size; j++) {
|
|
|
|
TaggedString *t = tb->hash[j];
|
|
|
|
if (t == NULL) continue;
|
1997-09-26 23:02:26 +08:00
|
|
|
if (t->head.marked == 1)
|
|
|
|
t->head.marked = 0;
|
|
|
|
else if (!t->head.marked) {
|
|
|
|
t->head.next = (GCnode *)frees;
|
1997-09-17 03:25:59 +08:00
|
|
|
frees = t;
|
|
|
|
tb->hash[j] = &EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return frees;
|
|
|
|
}
|
|
|
|
|
1997-09-26 23:02:26 +08:00
|
|
|
|
1997-12-02 04:31:25 +08:00
|
|
|
TaggedString *luaS_collectudata (void)
|
|
|
|
{
|
|
|
|
TaggedString *frees = NULL;
|
|
|
|
int i;
|
|
|
|
L->rootglobal.next = NULL; /* empty list of globals */
|
|
|
|
for (i=0; i<NUM_HASHS; i++) {
|
|
|
|
stringtable *tb = &L->string_root[i];
|
|
|
|
int j;
|
|
|
|
for (j=0; j<tb->size; j++) {
|
|
|
|
TaggedString *t = tb->hash[j];
|
|
|
|
if (t == NULL || t == &EMPTY || t->constindex != -1)
|
1998-06-20 00:14:09 +08:00
|
|
|
continue; /* get only user data */
|
1997-12-02 04:31:25 +08:00
|
|
|
t->head.next = (GCnode *)frees;
|
|
|
|
frees = t;
|
|
|
|
tb->hash[j] = &EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return frees;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaS_freeall (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<NUM_HASHS; i++) {
|
|
|
|
stringtable *tb = &L->string_root[i];
|
|
|
|
int j;
|
|
|
|
for (j=0; j<tb->size; j++) {
|
|
|
|
TaggedString *t = tb->hash[j];
|
|
|
|
if (t == &EMPTY) continue;
|
|
|
|
luaM_free(t);
|
|
|
|
}
|
|
|
|
luaM_free(tb->hash);
|
|
|
|
}
|
|
|
|
luaM_free(L->string_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-26 23:02:26 +08:00
|
|
|
void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
|
|
|
|
{
|
1998-03-07 00:54:42 +08:00
|
|
|
ts->u.s.globalval = *newval;
|
1997-09-26 23:02:26 +08:00
|
|
|
if (ts->head.next == (GCnode *)ts) { /* is not in list? */
|
1997-11-20 01:29:23 +08:00
|
|
|
ts->head.next = L->rootglobal.next;
|
|
|
|
L->rootglobal.next = (GCnode *)ts;
|
1997-09-26 23:02:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *luaS_travsymbol (int (*fn)(TObject *))
|
|
|
|
{
|
|
|
|
TaggedString *g;
|
1997-11-20 01:29:23 +08:00
|
|
|
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
|
1998-03-07 00:54:42 +08:00
|
|
|
if (fn(&g->u.s.globalval))
|
1997-09-26 23:02:26 +08:00
|
|
|
return g->str;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaS_globaldefined (char *name)
|
|
|
|
{
|
|
|
|
TaggedString *ts = luaS_new(name);
|
1998-03-07 00:54:42 +08:00
|
|
|
return ts->u.s.globalval.ttype != LUA_T_NIL;
|
1997-09-26 23:02:26 +08:00
|
|
|
}
|
|
|
|
|