MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / luaH_resize

Function luaH_resize

3rd/lua-5.4.3/src/ltable.c:542–576  ·  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

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

Tested by

no test coverage detected