rpm
4.5
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
lua
lstring.c
Go to the documentation of this file.
1
/*
2
** $Id: lstring.c,v 1.1 2004/03/16 21:58:30 niemeyer 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
21
void
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
27
void
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
51
/*@null@*/
52
static
TString
*
newlstr
(
lua_State
*L,
const
char
*str,
size_t
l,
lu_hash
h)
53
/*@modifies L @*/
54
{
55
TString
*ts =
cast
(
TString
*,
luaM_malloc
(L,
sizestring
(l)));
56
stringtable
*tb;
57
ts->
tsv
.
len
= l;
58
ts->
tsv
.
hash
= h;
59
ts->
tsv
.
marked
= 0;
60
ts->
tsv
.
tt
= LUA_TSTRING;
61
ts->
tsv
.
reserved
= 0;
62
memcpy(ts+1, str, l*
sizeof
(
char
));
63
((
char
*)(ts+1))[l] =
'\0'
;
/* ending 0 */
64
tb = &
G
(L)->strt;
65
h =
lmod
(h, tb->
size
);
66
ts->
tsv
.
next
= tb->
hash
[h];
/* chain new entry */
67
tb->
hash
[h] =
valtogco
(ts);
68
tb->
nuse
++;
69
if
(tb->
nuse
>
cast
(
ls_nstr
, tb->
size
) && tb->
size
<=
MAX_INT
/2)
70
luaS_resize
(L, tb->
size
*2);
/* too crowded */
71
return
ts;
72
}
73
74
75
TString
*
luaS_newlstr
(
lua_State
*L,
const
char
*str,
size_t
l) {
76
GCObject
*o;
77
lu_hash
h = (
lu_hash
)l;
/* seed */
78
size_t
step = (l>>5)+1;
/* if string is too long, don't hash all its chars */
79
size_t
l1;
80
for
(l1=l; l1>=step; l1-=step)
/* compute hash */
81
h = h ^ ((h<<5)+(h>>2)+(
unsigned
char
)(str[l1-1]));
82
for
(o =
G
(L)->strt.hash[
lmod
(h,
G
(L)->strt.size)];
83
o != NULL;
84
o = o->
gch
.
next
) {
85
TString
*ts =
gcotots
(o);
86
if
(ts->
tsv
.
len
== l && (memcmp(str,
getstr
(ts), l) == 0))
87
return
ts;
88
}
89
return
newlstr
(L, str, l, h);
/* not found */
90
}
91
92
93
Udata
*
luaS_newudata
(
lua_State
*L,
size_t
s) {
94
Udata
*u;
95
u =
cast
(
Udata
*,
luaM_malloc
(L,
sizeudata
(s)));
96
u->
uv
.
marked
= (1<<1);
/* is not finalized */
97
u->
uv
.
tt
= LUA_TUSERDATA;
98
u->
uv
.
len
= s;
99
u->
uv
.
metatable
=
hvalue
(
defaultmeta
(L));
100
/* chain it on udata list */
101
u->
uv
.
next
=
G
(L)->rootudata;
102
G
(L)->rootudata =
valtogco
(u);
103
return
u;
104
}
105
Generated on Mon Oct 27 2014 16:03:32 for rpm by
1.8.3.1