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
23 void luaS_resize (lua_State
*L
, int newsize
) {
27 if (G(L
)->gcstate
== GCSsweepstring
)
28 return; /* cannot resize during GC traverse */
29 newhash
= luaM_newvector(L
, newsize
, GCObject
*);
31 for (i
=0; i
<newsize
; i
++) newhash
[i
] = NULL
;
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 */
45 luaM_freearray(L
, tb
->hash
, tb
->size
, TString
*);
51 static TString
*newlstr (lua_State
*L
, const char *str
, size_t l
,
55 if (l
+1 > (MAX_SIZET
- sizeof(TString
))/sizeof(char))
57 ts
= cast(TString
*, luaM_malloc(L
, (l
+1)*sizeof(char)+sizeof(TString
)));
60 ts
->tsv
.marked
= luaC_white(G(L
));
61 ts
->tsv
.tt
= LUA_TSTRING
;
63 memcpy(ts
+1, str
, l
*sizeof(char));
64 ((char *)(ts
+1))[l
] = '\0'; /* ending 0 */
66 h
= lmod(h
, tb
->size
);
67 ts
->tsv
.next
= tb
->hash
[h
]; /* chain new entry */
68 tb
->hash
[h
] = obj2gco(ts
);
70 if (tb
->nuse
> cast(lu_int32
, tb
->size
) && tb
->size
<= MAX_INT
/2)
71 luaS_resize(L
, tb
->size
*2); /* too crowded */
76 TString
*luaS_newlstr (lua_State
*L
, const char *str
, size_t l
) {
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 */
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
)];
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
);
93 return newlstr(L
, str
, l
, h
); /* not found */
97 Udata
*luaS_newudata (lua_State
*L
, size_t s
, Table
*e
) {
99 if (s
> MAX_SIZET
- sizeof(Udata
))
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
;
105 u
->uv
.metatable
= NULL
;
107 /* chain it on udata list (after main thread) */
108 u
->uv
.next
= G(L
)->mainthread
->next
;
109 G(L
)->mainthread
->next
= obj2gco(u
);