1 /* $NetBSD: ltable.c,v 1.13 2023/06/08 21:12:08 nikita Exp $ */
2
3 /*
4 ** Id: ltable.c
5 ** Lua tables (hash)
6 ** See Copyright Notice in lua.h
7 */
8
9 #define ltable_c
10 #define LUA_CORE
11
12 #include "lprefix.h"
13
14
15 /*
16 ** Implementation of tables (aka arrays, objects, or hash tables).
17 ** Tables keep its elements in two parts: an array part and a hash part.
18 ** Non-negative integer keys are all candidates to be kept in the array
19 ** part. The actual size of the array is the largest 'n' such that
20 ** more than half the slots between 1 and n are in use.
21 ** Hash uses a mix of chained scatter table with Brent's variation.
22 ** A main invariant of these tables is that, if an element is not
23 ** in its main position (i.e. the 'original' position that its hash gives
24 ** to it), then the colliding element is in its own main position.
25 ** Hence even when the load factor reaches 100%, performance remains good.
26 */
27
28 #ifndef _KERNEL
29 #include <math.h>
30 #include <limits.h>
31 #endif /* _KERNEL */
32
33 #include "lua.h"
34
35 #include "ldebug.h"
36 #include "ldo.h"
37 #include "lgc.h"
38 #include "lmem.h"
39 #include "lobject.h"
40 #include "lstate.h"
41 #include "lstring.h"
42 #include "ltable.h"
43 #include "lvm.h"
44
45
46 /*
47 ** MAXABITS is the largest integer such that MAXASIZE fits in an
48 ** unsigned int.
49 */
50 #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1)
51
52
53 /*
54 ** MAXASIZE is the maximum size of the array part. It is the minimum
55 ** between 2^MAXABITS and the maximum size that, measured in bytes,
56 ** fits in a 'size_t'.
57 */
58 #define MAXASIZE luaM_limitN(1u << MAXABITS, TValue)
59
60 /*
61 ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a
62 ** signed int.
63 */
64 #define MAXHBITS (MAXABITS - 1)
65
66
67 /*
68 ** MAXHSIZE is the maximum size of the hash part. It is the minimum
69 ** between 2^MAXHBITS and the maximum size such that, measured in bytes,
70 ** it fits in a 'size_t'.
71 */
72 #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
73
74
75 /*
76 ** When the original hash value is good, hashing by a power of 2
77 ** avoids the cost of '%'.
78 */
79 #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
80
81 /*
82 ** for other types, it is better to avoid modulo by power of 2, as
83 ** they can have many 2 factors.
84 */
85 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
86
87
88 #define hashstr(t,str) hashpow2(t, (str)->hash)
89 #define hashboolean(t,p) hashpow2(t, p)
90
91
92 #define hashpointer(t,p) hashmod(t, point2uint(p))
93
94
95 #define dummynode (&dummynode_)
96
97 static const Node dummynode_ = {
98 {{NULL}, LUA_VEMPTY, /* value's value and type */
99 LUA_VNIL, 0, {NULL}} /* key type, next, and key value */
100 };
101
102
103 static const TValue absentkey = {ABSTKEYCONSTANT};
104
105
106 /*
107 ** Hash for integers. To allow a good hash, use the remainder operator
108 ** ('%'). If integer fits as a non-negative int, compute an int
109 ** remainder, which is faster. Otherwise, use an unsigned-integer
110 ** remainder, which uses all bits and ensures a non-negative result.
111 */
hashint(const Table * t,lua_Integer i)112 static Node *hashint (const Table *t, lua_Integer i) {
113 lua_Unsigned ui = l_castS2U(i);
114 if (ui <= cast_uint(INT_MAX))
115 return hashmod(t, cast_int(ui));
116 else
117 return hashmod(t, ui);
118 }
119
120
121 #ifndef _KERNEL
122 /*
123 ** Hash for floating-point numbers.
124 ** The main computation should be just
125 ** n = frexp(n, &i); return (n * INT_MAX) + i
126 ** but there are some numerical subtleties.
127 ** In a two-complement representation, INT_MAX does not has an exact
128 ** representation as a float, but INT_MIN does; because the absolute
129 ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the
130 ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal
131 ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when
132 ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with
133 ** INT_MIN.
134 */
135 #if !defined(l_hashfloat)
l_hashfloat(lua_Number n)136 static int l_hashfloat (lua_Number n) {
137 int i;
138 lua_Integer ni;
139 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
140 if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
141 lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
142 return 0;
143 }
144 else { /* normal case */
145 unsigned int u = cast_uint(i) + cast_uint(ni);
146 return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
147 }
148 }
149 #endif
150 #endif /* _KERNEL */
151
152
153 /*
154 ** returns the 'main' position of an element in a table (that is,
155 ** the index of its hash value).
156 */
mainpositionTV(const Table * t,const TValue * key)157 static Node *mainpositionTV (const Table *t, const TValue *key) {
158 switch (ttypetag(key)) {
159 case LUA_VNUMINT: {
160 lua_Integer i = ivalue(key);
161 return hashint(t, i);
162 }
163 #ifndef _KERNEL
164 case LUA_VNUMFLT: {
165 lua_Number n = fltvalue(key);
166 return hashmod(t, l_hashfloat(n));
167 }
168 #endif /* _KERNEL */
169 case LUA_VSHRSTR: {
170 TString *ts = tsvalue(key);
171 return hashstr(t, ts);
172 }
173 case LUA_VLNGSTR: {
174 TString *ts = tsvalue(key);
175 return hashpow2(t, luaS_hashlongstr(ts));
176 }
177 case LUA_VFALSE:
178 return hashboolean(t, 0);
179 case LUA_VTRUE:
180 return hashboolean(t, 1);
181 case LUA_VLIGHTUSERDATA: {
182 void *p = pvalue(key);
183 return hashpointer(t, p);
184 }
185 case LUA_VLCF: {
186 lua_CFunction f = fvalue(key);
187 return hashpointer(t, f);
188 }
189 default: {
190 GCObject *o = gcvalue(key);
191 return hashpointer(t, o);
192 }
193 }
194 }
195
196
mainpositionfromnode(const Table * t,Node * nd)197 l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) {
198 TValue key;
199 getnodekey(cast(lua_State *, NULL), &key, nd);
200 return mainpositionTV(t, &key);
201 }
202
203
204 /*
205 ** Check whether key 'k1' is equal to the key in node 'n2'. This
206 ** equality is raw, so there are no metamethods. Floats with integer
207 ** values have been normalized, so integers cannot be equal to
208 ** floats. It is assumed that 'eqshrstr' is simply pointer equality, so
209 ** that short strings are handled in the default case.
210 ** A true 'deadok' means to accept dead keys as equal to their original
211 ** values. All dead keys are compared in the default case, by pointer
212 ** identity. (Only collectable objects can produce dead keys.) Note that
213 ** dead long strings are also compared by identity.
214 ** Once a key is dead, its corresponding value may be collected, and
215 ** then another value can be created with the same address. If this
216 ** other value is given to 'next', 'equalkey' will signal a false
217 ** positive. In a regular traversal, this situation should never happen,
218 ** as all keys given to 'next' came from the table itself, and therefore
219 ** could not have been collected. Outside a regular traversal, we
220 ** have garbage in, garbage out. What is relevant is that this false
221 ** positive does not break anything. (In particular, 'next' will return
222 ** some other valid item on the table or nil.)
223 */
equalkey(const TValue * k1,const Node * n2,int deadok)224 static int equalkey (const TValue *k1, const Node *n2, int deadok) {
225 if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */
226 !(deadok && keyisdead(n2) && iscollectable(k1)))
227 return 0; /* cannot be same key */
228 switch (keytt(n2)) {
229 case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
230 return 1;
231 case LUA_VNUMINT:
232 return (ivalue(k1) == keyival(n2));
233 #ifndef _KERNEL
234 case LUA_VNUMFLT:
235 return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
236 #endif /* _KERNEL */
237 case LUA_VLIGHTUSERDATA:
238 return pvalue(k1) == pvalueraw(keyval(n2));
239 case LUA_VLCF:
240 return fvalue(k1) == fvalueraw(keyval(n2));
241 case ctb(LUA_VLNGSTR):
242 return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
243 default:
244 return gcvalue(k1) == gcvalueraw(keyval(n2));
245 }
246 }
247
248
249 /*
250 ** True if value of 'alimit' is equal to the real size of the array
251 ** part of table 't'. (Otherwise, the array part must be larger than
252 ** 'alimit'.)
253 */
254 #define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit))
255
256
257 /*
258 ** Returns the real size of the 'array' array
259 */
luaH_realasize(const Table * t)260 LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
261 if (limitequalsasize(t))
262 return t->alimit; /* this is the size */
263 else {
264 unsigned int size = t->alimit;
265 /* compute the smallest power of 2 not smaller than 'n' */
266 size |= (size >> 1);
267 size |= (size >> 2);
268 size |= (size >> 4);
269 size |= (size >> 8);
270 #if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */
271 size |= (size >> 16);
272 #if (UINT_MAX >> 30) > 3
273 size |= (size >> 32); /* unsigned int has more than 32 bits */
274 #endif
275 #endif
276 size++;
277 lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
278 return size;
279 }
280 }
281
282
283 /*
284 ** Check whether real size of the array is a power of 2.
285 ** (If it is not, 'alimit' cannot be changed to any other value
286 ** without changing the real size.)
287 */
ispow2realasize(const Table * t)288 static int ispow2realasize (const Table *t) {
289 return (!isrealasize(t) || ispow2(t->alimit));
290 }
291
292
setlimittosize(Table * t)293 static unsigned int setlimittosize (Table *t) {
294 t->alimit = luaH_realasize(t);
295 setrealasize(t);
296 return t->alimit;
297 }
298
299
300 #define limitasasize(t) check_exp(isrealasize(t), t->alimit)
301
302
303
304 /*
305 ** "Generic" get version. (Not that generic: not valid for integers,
306 ** which may be in array part, nor for floats with integral values.)
307 ** See explanation about 'deadok' in function 'equalkey'.
308 */
getgeneric(Table * t,const TValue * key,int deadok)309 static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
310 Node *n = mainpositionTV(t, key);
311 for (;;) { /* check whether 'key' is somewhere in the chain */
312 if (equalkey(key, n, deadok))
313 return gval(n); /* that's it */
314 else {
315 int nx = gnext(n);
316 if (nx == 0)
317 return &absentkey; /* not found */
318 n += nx;
319 }
320 }
321 }
322
323
324 /*
325 ** returns the index for 'k' if 'k' is an appropriate key to live in
326 ** the array part of a table, 0 otherwise.
327 */
arrayindex(lua_Integer k)328 static unsigned int arrayindex (lua_Integer k) {
329 if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */
330 return cast_uint(k); /* 'key' is an appropriate array index */
331 else
332 return 0;
333 }
334
335
336 /*
337 ** returns the index of a 'key' for table traversals. First goes all
338 ** elements in the array part, then elements in the hash part. The
339 ** beginning of a traversal is signaled by 0.
340 */
findindex(lua_State * L,Table * t,TValue * key,unsigned int asize)341 static unsigned int findindex (lua_State *L, Table *t, TValue *key,
342 unsigned int asize) {
343 unsigned int i;
344 if (ttisnil(key)) return 0; /* first iteration */
345 i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0;
346 if (i - 1u < asize) /* is 'key' inside array part? */
347 return i; /* yes; that's the index */
348 else {
349 const TValue *n = getgeneric(t, key, 1);
350 if (l_unlikely(isabstkey(n)))
351 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
352 i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
353 /* hash elements are numbered after array ones */
354 return (i + 1) + asize;
355 }
356 }
357
358
luaH_next(lua_State * L,Table * t,StkId key)359 int luaH_next (lua_State *L, Table *t, StkId key) {
360 unsigned int asize = luaH_realasize(t);
361 unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
362 for (; i < asize; i++) { /* try first array part */
363 if (!isempty(&t->array[i])) { /* a non-empty entry? */
364 setivalue(s2v(key), i + 1);
365 setobj2s(L, key + 1, &t->array[i]);
366 return 1;
367 }
368 }
369 for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */
370 if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
371 Node *n = gnode(t, i);
372 getnodekey(L, s2v(key), n);
373 setobj2s(L, key + 1, gval(n));
374 return 1;
375 }
376 }
377 return 0; /* no more elements */
378 }
379
380
freehash(lua_State * L,Table * t)381 static void freehash (lua_State *L, Table *t) {
382 if (!isdummy(t))
383 luaM_freearray(L, t->node, cast_sizet(sizenode(t)));
384 }
385
386
387 /*
388 ** {=============================================================
389 ** Rehash
390 ** ==============================================================
391 */
392
393 /*
394 ** Compute the optimal size for the array part of table 't'. 'nums' is a
395 ** "count array" where 'nums[i]' is the number of integers in the table
396 ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
397 ** integer keys in the table and leaves with the number of keys that
398 ** will go to the array part; return the optimal size. (The condition
399 ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.)
400 */
computesizes(unsigned int nums[],unsigned int * pna)401 static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
402 int i;
403 unsigned int twotoi; /* 2^i (candidate for optimal size) */
404 unsigned int a = 0; /* number of elements smaller than 2^i */
405 unsigned int na = 0; /* number of elements to go to array part */
406 unsigned int optimal = 0; /* optimal size for array part */
407 /* loop while keys can fill more than half of total size */
408 for (i = 0, twotoi = 1;
409 twotoi > 0 && *pna > twotoi / 2;
410 i++, twotoi *= 2) {
411 a += nums[i];
412 if (a > twotoi/2) { /* more than half elements present? */
413 optimal = twotoi; /* optimal size (till now) */
414 na = a; /* all elements up to 'optimal' will go to array part */
415 }
416 }
417 lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
418 *pna = na;
419 return optimal;
420 }
421
422
countint(lua_Integer key,unsigned int * nums)423 static int countint (lua_Integer key, unsigned int *nums) {
424 unsigned int k = arrayindex(key);
425 if (k != 0) { /* is 'key' an appropriate array index? */
426 nums[luaO_ceillog2(k)]++; /* count as such */
427 return 1;
428 }
429 else
430 return 0;
431 }
432
433
434 /*
435 ** Count keys in array part of table 't': Fill 'nums[i]' with
436 ** number of keys that will go into corresponding slice and return
437 ** total number of non-nil keys.
438 */
numusearray(const Table * t,unsigned int * nums)439 static unsigned int numusearray (const Table *t, unsigned int *nums) {
440 int lg;
441 unsigned int ttlg; /* 2^lg */
442 unsigned int ause = 0; /* summation of 'nums' */
443 unsigned int i = 1; /* count to traverse all array keys */
444 unsigned int asize = limitasasize(t); /* real array size */
445 /* traverse each slice */
446 for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
447 unsigned int lc = 0; /* counter */
448 unsigned int lim = ttlg;
449 if (lim > asize) {
450 lim = asize; /* adjust upper limit */
451 if (i > lim)
452 break; /* no more elements to count */
453 }
454 /* count elements in range (2^(lg - 1), 2^lg] */
455 for (; i <= lim; i++) {
456 if (!isempty(&t->array[i-1]))
457 lc++;
458 }
459 nums[lg] += lc;
460 ause += lc;
461 }
462 return ause;
463 }
464
465
numusehash(const Table * t,unsigned int * nums,unsigned int * pna)466 static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
467 int totaluse = 0; /* total number of elements */
468 int ause = 0; /* elements added to 'nums' (can go to array part) */
469 int i = sizenode(t);
470 while (i--) {
471 Node *n = &t->node[i];
472 if (!isempty(gval(n))) {
473 if (keyisinteger(n))
474 ause += countint(keyival(n), nums);
475 totaluse++;
476 }
477 }
478 *pna += ause;
479 return totaluse;
480 }
481
482
483 /*
484 ** Creates an array for the hash part of a table with the given
485 ** size, or reuses the dummy node if size is zero.
486 ** The computation for size overflow is in two steps: the first
487 ** comparison ensures that the shift in the second one does not
488 ** overflow.
489 */
setnodevector(lua_State * L,Table * t,unsigned int size)490 static void setnodevector (lua_State *L, Table *t, unsigned int size) {
491 if (size == 0) { /* no elements to hash part? */
492 t->node = cast(Node *, dummynode); /* use common 'dummynode' */
493 t->lsizenode = 0;
494 t->lastfree = NULL; /* signal that it is using dummy node */
495 }
496 else {
497 int i;
498 int lsize = luaO_ceillog2(size);
499 if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
500 luaG_runerror(L, "table overflow");
501 size = twoto(lsize);
502 t->node = luaM_newvector(L, size, Node);
503 for (i = 0; i < cast_int(size); i++) {
504 Node *n = gnode(t, i);
505 gnext(n) = 0;
506 setnilkey(n);
507 setempty(gval(n));
508 }
509 t->lsizenode = cast_byte(lsize);
510 t->lastfree = gnode(t, size); /* all positions are free */
511 }
512 }
513
514
515 /*
516 ** (Re)insert all elements from the hash part of 'ot' into table 't'.
517 */
reinsert(lua_State * L,Table * ot,Table * t)518 static void reinsert (lua_State *L, Table *ot, Table *t) {
519 int j;
520 int size = sizenode(ot);
521 for (j = 0; j < size; j++) {
522 Node *old = gnode(ot, j);
523 if (!isempty(gval(old))) {
524 /* doesn't need barrier/invalidate cache, as entry was
525 already present in the table */
526 TValue k;
527 getnodekey(L, &k, old);
528 luaH_set(L, t, &k, gval(old));
529 }
530 }
531 }
532
533
534 /*
535 ** Exchange the hash part of 't1' and 't2'.
536 */
exchangehashpart(Table * t1,Table * t2)537 static void exchangehashpart (Table *t1, Table *t2) {
538 lu_byte lsizenode = t1->lsizenode;
539 Node *node = t1->node;
540 Node *lastfree = t1->lastfree;
541 t1->lsizenode = t2->lsizenode;
542 t1->node = t2->node;
543 t1->lastfree = t2->lastfree;
544 t2->lsizenode = lsizenode;
545 t2->node = node;
546 t2->lastfree = lastfree;
547 }
548
549
550 /*
551 ** Resize table 't' for the new given sizes. Both allocations (for
552 ** the hash part and for the array part) can fail, which creates some
553 ** subtleties. If the first allocation, for the hash part, fails, an
554 ** error is raised and that is it. Otherwise, it copies the elements from
555 ** the shrinking part of the array (if it is shrinking) into the new
556 ** hash. Then it reallocates the array part. If that fails, the table
557 ** is in its original state; the function frees the new hash part and then
558 ** raises the allocation error. Otherwise, it sets the new hash part
559 ** into the table, initializes the new part of the array (if any) with
560 ** nils and reinserts the elements of the old hash back into the new
561 ** parts of the table.
562 */
luaH_resize(lua_State * L,Table * t,unsigned int newasize,unsigned int nhsize)563 void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
564 unsigned int nhsize) {
565 unsigned int i;
566 Table newt; /* to keep the new hash part */
567 unsigned int oldasize = setlimittosize(t);
568 TValue *newarray;
569 /* create new hash part with appropriate size into 'newt' */
570 setnodevector(L, &newt, nhsize);
571 if (newasize < oldasize) { /* will array shrink? */
572 t->alimit = newasize; /* pretend array has new size... */
573 exchangehashpart(t, &newt); /* and new hash */
574 /* re-insert into the new hash the elements from vanishing slice */
575 for (i = newasize; i < oldasize; i++) {
576 if (!isempty(&t->array[i]))
577 luaH_setint(L, t, i + 1, &t->array[i]);
578 }
579 t->alimit = oldasize; /* restore current size... */
580 exchangehashpart(t, &newt); /* and hash (in case of errors) */
581 }
582 /* allocate new array */
583 newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
584 if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
585 freehash(L, &newt); /* release new hash part */
586 luaM_error(L); /* raise error (with array unchanged) */
587 }
588 /* allocation ok; initialize new part of the array */
589 exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */
590 t->array = newarray; /* set new array part */
591 t->alimit = newasize;
592 for (i = oldasize; i < newasize; i++) /* clear new slice of the array */
593 setempty(&t->array[i]);
594 /* re-insert elements from old hash part into new parts */
595 reinsert(L, &newt, t); /* 'newt' now has the old hash */
596 freehash(L, &newt); /* free old hash part */
597 }
598
599
luaH_resizearray(lua_State * L,Table * t,unsigned int nasize)600 void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
601 int nsize = allocsizenode(t);
602 luaH_resize(L, t, nasize, nsize);
603 }
604
605 /*
606 ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
607 */
rehash(lua_State * L,Table * t,const TValue * ek)608 static void rehash (lua_State *L, Table *t, const TValue *ek) {
609 unsigned int asize; /* optimal size for array part */
610 unsigned int na; /* number of keys in the array part */
611 unsigned int nums[MAXABITS + 1];
612 int i;
613 int totaluse;
614 for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
615 setlimittosize(t);
616 na = numusearray(t, nums); /* count keys in array part */
617 totaluse = na; /* all those keys are integer keys */
618 totaluse += numusehash(t, nums, &na); /* count keys in hash part */
619 /* count extra key */
620 if (ttisinteger(ek))
621 na += countint(ivalue(ek), nums);
622 totaluse++;
623 /* compute new size for array part */
624 asize = computesizes(nums, &na);
625 /* resize the table to new computed sizes */
626 luaH_resize(L, t, asize, totaluse - na);
627 }
628
629
630
631 /*
632 ** }=============================================================
633 */
634
635
luaH_new(lua_State * L)636 Table *luaH_new (lua_State *L) {
637 GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
638 Table *t = gco2t(o);
639 t->metatable = NULL;
640 t->flags = cast_byte(maskflags); /* table has no metamethod fields */
641 t->array = NULL;
642 t->alimit = 0;
643 setnodevector(L, t, 0);
644 return t;
645 }
646
647
luaH_free(lua_State * L,Table * t)648 void luaH_free (lua_State *L, Table *t) {
649 freehash(L, t);
650 luaM_freearray(L, t->array, luaH_realasize(t));
651 luaM_free(L, t);
652 }
653
654
getfreepos(Table * t)655 static Node *getfreepos (Table *t) {
656 if (!isdummy(t)) {
657 while (t->lastfree > t->node) {
658 t->lastfree--;
659 if (keyisnil(t->lastfree))
660 return t->lastfree;
661 }
662 }
663 return NULL; /* could not find a free place */
664 }
665
666
667
668 /*
669 ** inserts a new key into a hash table; first, check whether key's main
670 ** position is free. If not, check whether colliding node is in its main
671 ** position or not: if it is not, move colliding node to an empty place and
672 ** put new key in its main position; otherwise (colliding node is in its main
673 ** position), new key goes to an empty position.
674 */
luaH_newkey(lua_State * L,Table * t,const TValue * key,TValue * value)675 void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) {
676 Node *mp;
677 #ifndef _KERNEL
678 TValue aux;
679 #endif /* _KERNEL */
680 if (l_unlikely(ttisnil(key)))
681 luaG_runerror(L, "table index is nil");
682 #ifndef _KERNEL
683 else if (ttisfloat(key)) {
684 lua_Number f = fltvalue(key);
685 lua_Integer k;
686 if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
687 setivalue(&aux, k);
688 key = &aux; /* insert it as an integer */
689 }
690 else if (l_unlikely(luai_numisnan(f)))
691 luaG_runerror(L, "table index is NaN");
692 }
693 #endif /* _KERNEL */
694 if (ttisnil(value))
695 return; /* do not insert nil values */
696 mp = mainpositionTV(t, key);
697 if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
698 Node *othern;
699 Node *f = getfreepos(t); /* get a free place */
700 if (f == NULL) { /* cannot find a free place? */
701 rehash(L, t, key); /* grow table */
702 /* whatever called 'newkey' takes care of TM cache */
703 luaH_set(L, t, key, value); /* insert key into grown table */
704 return;
705 }
706 lua_assert(!isdummy(t));
707 othern = mainpositionfromnode(t, mp);
708 if (othern != mp) { /* is colliding node out of its main position? */
709 /* yes; move colliding node into free position */
710 while (othern + gnext(othern) != mp) /* find previous */
711 othern += gnext(othern);
712 gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
713 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
714 if (gnext(mp) != 0) {
715 gnext(f) += cast_int(mp - f); /* correct 'next' */
716 gnext(mp) = 0; /* now 'mp' is free */
717 }
718 setempty(gval(mp));
719 }
720 else { /* colliding node is in its own main position */
721 /* new node will go into free position */
722 if (gnext(mp) != 0)
723 gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
724 else lua_assert(gnext(f) == 0);
725 gnext(mp) = cast_int(f - mp);
726 mp = f;
727 }
728 }
729 setnodekey(L, mp, key);
730 luaC_barrierback(L, obj2gco(t), key);
731 lua_assert(isempty(gval(mp)));
732 setobj2t(L, gval(mp), value);
733 }
734
735
736 /*
737 ** Search function for integers. If integer is inside 'alimit', get it
738 ** directly from the array part. Otherwise, if 'alimit' is not equal to
739 ** the real size of the array, key still can be in the array part. In
740 ** this case, try to avoid a call to 'luaH_realasize' when key is just
741 ** one more than the limit (so that it can be incremented without
742 ** changing the real size of the array).
743 */
luaH_getint(Table * t,lua_Integer key)744 const TValue *luaH_getint (Table *t, lua_Integer key) {
745 if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */
746 return &t->array[key - 1];
747 else if (!limitequalsasize(t) && /* key still may be in the array part? */
748 (l_castS2U(key) == t->alimit + 1 ||
749 l_castS2U(key) - 1u < luaH_realasize(t))) {
750 t->alimit = cast_uint(key); /* probably '#t' is here now */
751 return &t->array[key - 1];
752 }
753 else {
754 Node *n = hashint(t, key);
755 for (;;) { /* check whether 'key' is somewhere in the chain */
756 if (keyisinteger(n) && keyival(n) == key)
757 return gval(n); /* that's it */
758 else {
759 int nx = gnext(n);
760 if (nx == 0) break;
761 n += nx;
762 }
763 }
764 return &absentkey;
765 }
766 }
767
768
769 /*
770 ** search function for short strings
771 */
luaH_getshortstr(Table * t,TString * key)772 const TValue *luaH_getshortstr (Table *t, TString *key) {
773 Node *n = hashstr(t, key);
774 lua_assert(key->tt == LUA_VSHRSTR);
775 for (;;) { /* check whether 'key' is somewhere in the chain */
776 if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
777 return gval(n); /* that's it */
778 else {
779 int nx = gnext(n);
780 if (nx == 0)
781 return &absentkey; /* not found */
782 n += nx;
783 }
784 }
785 }
786
787
luaH_getstr(Table * t,TString * key)788 const TValue *luaH_getstr (Table *t, TString *key) {
789 if (key->tt == LUA_VSHRSTR)
790 return luaH_getshortstr(t, key);
791 else { /* for long strings, use generic case */
792 TValue ko;
793 setsvalue(cast(lua_State *, NULL), &ko, key);
794 return getgeneric(t, &ko, 0);
795 }
796 }
797
798
799 /*
800 ** main search function
801 */
luaH_get(Table * t,const TValue * key)802 const TValue *luaH_get (Table *t, const TValue *key) {
803 switch (ttypetag(key)) {
804 case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key));
805 case LUA_VNUMINT: return luaH_getint(t, ivalue(key));
806 case LUA_VNIL: return &absentkey;
807 #ifndef _KERNEL
808 case LUA_VNUMFLT: {
809 lua_Integer k;
810 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
811 return luaH_getint(t, k); /* use specialized version */
812 /* else... */
813 } /* FALLTHROUGH */
814 #endif /* _KERNEL */
815 default:
816 return getgeneric(t, key, 0);
817 }
818 }
819
820
821 /*
822 ** Finish a raw "set table" operation, where 'slot' is where the value
823 ** should have been (the result of a previous "get table").
824 ** Beware: when using this function you probably need to check a GC
825 ** barrier and invalidate the TM cache.
826 */
luaH_finishset(lua_State * L,Table * t,const TValue * key,const TValue * slot,TValue * value)827 void luaH_finishset (lua_State *L, Table *t, const TValue *key,
828 const TValue *slot, TValue *value) {
829 if (isabstkey(slot))
830 luaH_newkey(L, t, key, value);
831 else
832 setobj2t(L, cast(TValue *, slot), value);
833 }
834
835
836 /*
837 ** beware: when using this function you probably need to check a GC
838 ** barrier and invalidate the TM cache.
839 */
luaH_set(lua_State * L,Table * t,const TValue * key,TValue * value)840 void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) {
841 const TValue *slot = luaH_get(t, key);
842 luaH_finishset(L, t, key, slot, value);
843 }
844
845
luaH_setint(lua_State * L,Table * t,lua_Integer key,TValue * value)846 void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
847 const TValue *p = luaH_getint(t, key);
848 if (isabstkey(p)) {
849 TValue k;
850 setivalue(&k, key);
851 luaH_newkey(L, t, &k, value);
852 }
853 else
854 setobj2t(L, cast(TValue *, p), value);
855 }
856
857
858 /*
859 ** Try to find a boundary in the hash part of table 't'. From the
860 ** caller, we know that 'j' is zero or present and that 'j + 1' is
861 ** present. We want to find a larger key that is absent from the
862 ** table, so that we can do a binary search between the two keys to
863 ** find a boundary. We keep doubling 'j' until we get an absent index.
864 ** If the doubling would overflow, we try LUA_MAXINTEGER. If it is
865 ** absent, we are ready for the binary search. ('j', being max integer,
866 ** is larger or equal to 'i', but it cannot be equal because it is
867 ** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a
868 ** boundary. ('j + 1' cannot be a present integer key because it is
869 ** not a valid integer in Lua.)
870 */
hash_search(Table * t,lua_Unsigned j)871 static lua_Unsigned hash_search (Table *t, lua_Unsigned j) {
872 lua_Unsigned i;
873 if (j == 0) j++; /* the caller ensures 'j + 1' is present */
874 do {
875 i = j; /* 'i' is a present index */
876 if (j <= l_castS2U(LUA_MAXINTEGER) / 2)
877 j *= 2;
878 else {
879 j = LUA_MAXINTEGER;
880 if (isempty(luaH_getint(t, j))) /* t[j] not present? */
881 break; /* 'j' now is an absent index */
882 else /* weird case */
883 return j; /* well, max integer is a boundary... */
884 }
885 } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */
886 /* i < j && t[i] present && t[j] absent */
887 while (j - i > 1u) { /* do a binary search between them */
888 lua_Unsigned m = (i + j) / 2;
889 if (isempty(luaH_getint(t, m))) j = m;
890 else i = m;
891 }
892 return i;
893 }
894
895
binsearch(const TValue * array,unsigned int i,unsigned int j)896 static unsigned int binsearch (const TValue *array, unsigned int i,
897 unsigned int j) {
898 while (j - i > 1u) { /* binary search */
899 unsigned int m = (i + j) / 2;
900 if (isempty(&array[m - 1])) j = m;
901 else i = m;
902 }
903 return i;
904 }
905
906
907 /*
908 ** Try to find a boundary in table 't'. (A 'boundary' is an integer index
909 ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent
910 ** and 'maxinteger' if t[maxinteger] is present.)
911 ** (In the next explanation, we use Lua indices, that is, with base 1.
912 ** The code itself uses base 0 when indexing the array part of the table.)
913 ** The code starts with 'limit = t->alimit', a position in the array
914 ** part that may be a boundary.
915 **
916 ** (1) If 't[limit]' is empty, there must be a boundary before it.
917 ** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1'
918 ** is present. If so, it is a boundary. Otherwise, do a binary search
919 ** between 0 and limit to find a boundary. In both cases, try to
920 ** use this boundary as the new 'alimit', as a hint for the next call.
921 **
922 ** (2) If 't[limit]' is not empty and the array has more elements
923 ** after 'limit', try to find a boundary there. Again, try first
924 ** the special case (which should be quite frequent) where 'limit+1'
925 ** is empty, so that 'limit' is a boundary. Otherwise, check the
926 ** last element of the array part. If it is empty, there must be a
927 ** boundary between the old limit (present) and the last element
928 ** (absent), which is found with a binary search. (This boundary always
929 ** can be a new limit.)
930 **
931 ** (3) The last case is when there are no elements in the array part
932 ** (limit == 0) or its last element (the new limit) is present.
933 ** In this case, must check the hash part. If there is no hash part
934 ** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call
935 ** 'hash_search' to find a boundary in the hash part of the table.
936 ** (In those cases, the boundary is not inside the array part, and
937 ** therefore cannot be used as a new limit.)
938 */
luaH_getn(Table * t)939 lua_Unsigned luaH_getn (Table *t) {
940 unsigned int limit = t->alimit;
941 if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
942 /* there must be a boundary before 'limit' */
943 if (limit >= 2 && !isempty(&t->array[limit - 2])) {
944 /* 'limit - 1' is a boundary; can it be a new limit? */
945 if (ispow2realasize(t) && !ispow2(limit - 1)) {
946 t->alimit = limit - 1;
947 setnorealasize(t); /* now 'alimit' is not the real size */
948 }
949 return limit - 1;
950 }
951 else { /* must search for a boundary in [0, limit] */
952 unsigned int boundary = binsearch(t->array, 0, limit);
953 /* can this boundary represent the real size of the array? */
954 if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
955 t->alimit = boundary; /* use it as the new limit */
956 setnorealasize(t);
957 }
958 return boundary;
959 }
960 }
961 /* 'limit' is zero or present in table */
962 if (!limitequalsasize(t)) { /* (2)? */
963 /* 'limit' > 0 and array has more elements after 'limit' */
964 if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
965 return limit; /* this is the boundary */
966 /* else, try last element in the array */
967 limit = luaH_realasize(t);
968 if (isempty(&t->array[limit - 1])) { /* empty? */
969 /* there must be a boundary in the array after old limit,
970 and it must be a valid new limit */
971 unsigned int boundary = binsearch(t->array, t->alimit, limit);
972 t->alimit = boundary;
973 return boundary;
974 }
975 /* else, new limit is present in the table; check the hash part */
976 }
977 /* (3) 'limit' is the last element and either is zero or present in table */
978 lua_assert(limit == luaH_realasize(t) &&
979 (limit == 0 || !isempty(&t->array[limit - 1])));
980 if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
981 return limit; /* 'limit + 1' is absent */
982 else /* 'limit + 1' is also present */
983 return hash_search(t, limit);
984 }
985
986
987
988 #if defined(LUA_DEBUG)
989
990 /* export these functions for the test library */
991
luaH_mainposition(const Table * t,const TValue * key)992 Node *luaH_mainposition (const Table *t, const TValue *key) {
993 return mainpositionTV(t, key);
994 }
995
996 #endif
997