Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/VM/src/lstring.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
3
#include "lstring.h"
4
5
#include "lgc.h"
6
#include "lmem.h"
7
8
#include <string.h>
9
10
unsigned int luaS_hash(const char* str, size_t len)
11
{
12
// Note that this hashing algorithm is replicated in BytecodeBuilder.cpp, BytecodeBuilder::getStringHash
13
unsigned int a = 0, b = 0;
14
unsigned int h = unsigned(len);
15
16
// hash prefix in 12b chunks (using aligned reads) with ARX based hash (LuaJIT v2.1, lookup3)
17
// note that we stop at length<32 to maintain compatibility with Lua 5.1
18
while (len >= 32)
19
{
20
#define rol(x, s) ((x >> s) | (x << (32 - s)))
21
#define mix(u, v, w) a ^= h, a -= rol(h, u), b ^= a, b -= rol(a, v), h ^= b, h -= rol(b, w)
22
23
// should compile into fast unaligned reads
24
uint32_t block[3];
25
memcpy(block, str, 12);
26
27
a += block[0];
28
b += block[1];
29
h += block[2];
30
mix(14, 11, 25);
31
str += 12;
32
len -= 12;
33
34
#undef mix
35
#undef rol
36
}
37
38
// original Lua 5.1 hash for compatibility (exact match when len<32)
39
for (size_t i = len; i > 0; --i)
40
h ^= (h << 5) + (h >> 2) + (uint8_t)str[i - 1];
41
42
return h;
43
}
44
45
void luaS_resize(lua_State* L, int newsize)
46
{
47
TString** newhash = luaM_newarray(L, newsize, TString*, 0);
48
stringtable* tb = &L->global->strt;
49
for (int i = 0; i < newsize; i++)
50
newhash[i] = NULL;
51
// rehash
52
for (int i = 0; i < tb->size; i++)
53
{
54
TString* p = tb->hash[i];
55
while (p)
56
{ // for each node in the list
57
TString* next = p->next; // save next
58
unsigned int h = p->hash;
59
int h1 = lmod(h, newsize); // new position
60
LUAU_ASSERT(cast_int(h % newsize) == lmod(h, newsize));
61
p->next = newhash[h1]; // chain it
62
newhash[h1] = p;
63
p = next;
64
}
65
}
66
luaM_freearray(L, tb->hash, tb->size, TString*, 0);
67
tb->size = newsize;
68
tb->hash = newhash;
69
}
70
71
static TString* newlstr(lua_State* L, const char* str, size_t l, unsigned int h)
72
{
73
if (l > MAXSSIZE)
74
luaM_toobig(L);
75
76
TString* ts = luaM_newgco(L, TString, sizestring(l), L->activememcat);
77
luaC_init(L, ts, LUA_TSTRING);
78
ts->atom = ATOM_UNDEF;
79
ts->hash = h;
80
ts->len = unsigned(l);
81
82
memcpy(ts->data, str, l);
83
ts->data[l] = '\0'; // ending 0
84
85
stringtable* tb = &L->global->strt;
86
h = lmod(h, tb->size);
87
ts->next = tb->hash[h]; // chain new entry
88
tb->hash[h] = ts;
89
90
tb->nuse++;
91
if (tb->nuse > cast_to(uint32_t, tb->size) && tb->size <= INT_MAX / 2)
92
luaS_resize(L, tb->size * 2); // too crowded
93
94
return ts;
95
}
96
97
TString* luaS_bufstart(lua_State* L, size_t size)
98
{
99
if (size > MAXSSIZE)
100
luaM_toobig(L);
101
102
TString* ts = luaM_newgco(L, TString, sizestring(size), L->activememcat);
103
luaC_init(L, ts, LUA_TSTRING);
104
ts->atom = ATOM_UNDEF;
105
ts->hash = 0; // computed in luaS_buffinish
106
ts->len = unsigned(size);
107
108
ts->next = NULL;
109
110
return ts;
111
}
112
113
TString* luaS_buffinish(lua_State* L, TString* ts)
114
{
115
unsigned int h = luaS_hash(ts->data, ts->len);
116
stringtable* tb = &L->global->strt;
117
int bucket = lmod(h, tb->size);
118
119
// search if we already have this string in the hash table
120
for (TString* el = tb->hash[bucket]; el != NULL; el = el->next)
121
{
122
if (el->len == ts->len && memcmp(el->data, ts->data, ts->len) == 0)
123
{
124
// string may be dead
125
if (isdead(L->global, obj2gco(el)))
126
changewhite(obj2gco(el));
127
128
return el;
129
}
130
}
131
132
LUAU_ASSERT(ts->next == NULL);
133
134
ts->hash = h;
135
ts->data[ts->len] = '\0'; // ending 0
136
ts->atom = ATOM_UNDEF;
137
ts->next = tb->hash[bucket]; // chain new entry
138
tb->hash[bucket] = ts;
139
140
tb->nuse++;
141
if (tb->nuse > cast_to(uint32_t, tb->size) && tb->size <= INT_MAX / 2)
142
luaS_resize(L, tb->size * 2); // too crowded
143
144
return ts;
145
}
146
147
TString* luaS_newlstr(lua_State* L, const char* str, size_t l)
148
{
149
unsigned int h = luaS_hash(str, l);
150
for (TString* el = L->global->strt.hash[lmod(h, L->global->strt.size)]; el != NULL; el = el->next)
151
{
152
if (el->len == l && (memcmp(str, getstr(el), l) == 0))
153
{
154
// string may be dead
155
if (isdead(L->global, obj2gco(el)))
156
changewhite(obj2gco(el));
157
return el;
158
}
159
}
160
return newlstr(L, str, l, h); // not found
161
}
162
163
static bool unlinkstr(lua_State* L, TString* ts)
164
{
165
global_State* g = L->global;
166
167
TString** p = &g->strt.hash[lmod(ts->hash, g->strt.size)];
168
169
while (TString* curr = *p)
170
{
171
if (curr == ts)
172
{
173
*p = curr->next;
174
return true;
175
}
176
else
177
{
178
p = &curr->next;
179
}
180
}
181
182
return false;
183
}
184
185
void luaS_free(lua_State* L, TString* ts, lua_Page* page)
186
{
187
if (unlinkstr(L, ts))
188
L->global->strt.nuse--;
189
else
190
LUAU_ASSERT(ts->next == NULL); // orphaned string buffer
191
192
luaM_freegco(L, ts, sizestring(ts->len), ts->memcat, page);
193
}
194
195