MCPcopy Create free account
hub / github.com/SakuraEngine/SakuraEngine / luaH_resize

Function luaH_resize

xrepo/packages/l/lua/port/lua/src/ltable.c:551–585  ·  view source on GitHub ↗

** Resize table 't' for the new given sizes. Both allocations (for ** the hash part and for the array part) can fail, which creates some ** subtleties. If the first allocation, for the hash part, fails, an ** error is raised and that is it. Otherwise, it copies the elements from ** the shrinking part of the array (if it is shrinking) into the new ** hash. Then it reallocates the array part. If th

Source from the content-addressed store, hash-verified

549** parts of the table.
550*/
551void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
552 unsigned int nhsize) {
553 unsigned int i;
554 Table newt; /* to keep the new hash part */
555 unsigned int oldasize = setlimittosize(t);
556 TValue *newarray;
557 /* create new hash part with appropriate size into 'newt' */
558 setnodevector(L, &newt, nhsize);
559 if (newasize < oldasize) { /* will array shrink? */
560 t->alimit = newasize; /* pretend array has new size... */
561 exchangehashpart(t, &newt); /* and new hash */
562 /* re-insert into the new hash the elements from vanishing slice */
563 for (i = newasize; i < oldasize; i++) {
564 if (!isempty(&t->array[i]))
565 luaH_setint(L, t, i + 1, &t->array[i]);
566 }
567 t->alimit = oldasize; /* restore current size... */
568 exchangehashpart(t, &newt); /* and hash (in case of errors) */
569 }
570 /* allocate new array */
571 newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
572 if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
573 freehash(L, &newt); /* release new hash part */
574 luaM_error(L); /* raise error (with array unchanged) */
575 }
576 /* allocation ok; initialize new part of the array */
577 exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */
578 t->array = newarray; /* set new array part */
579 t->alimit = newasize;
580 for (i = oldasize; i < newasize; i++) /* clear new slice of the array */
581 setempty(&t->array[i]);
582 /* re-insert elements from old hash part into new parts */
583 reinsert(L, &newt, t); /* 'newt' now has the old hash */
584 freehash(L, &newt); /* free old hash part */
585}
586
587
588void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {

Callers 5

luaV_executeFunction · 0.85
init_registryFunction · 0.85
luaH_resizearrayFunction · 0.85
rehashFunction · 0.85
lua_createtableFunction · 0.85

Calls 6

setlimittosizeFunction · 0.85
exchangehashpartFunction · 0.85
luaH_setintFunction · 0.85
freehashFunction · 0.85
reinsertFunction · 0.85
setnodevectorFunction · 0.70

Tested by

no test coverage detected