** 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
| 714 | ** part. |
| 715 | */ |
| 716 | void luaH_resize (lua_State *L, Table *t, unsigned newasize, |
| 717 | unsigned nhsize) { |
| 718 | Table newt; /* to keep the new hash part */ |
| 719 | unsigned oldasize = t->asize; |
| 720 | Value *newarray; |
| 721 | if (newasize > MAXASIZE) |
| 722 | luaG_runerror(L, "table overflow"); |
| 723 | /* create new hash part with appropriate size into 'newt' */ |
| 724 | newt.flags = 0; |
| 725 | setnodevector(L, &newt, nhsize); |
| 726 | if (newasize < oldasize) { /* will array shrink? */ |
| 727 | /* re-insert into the new hash the elements from vanishing slice */ |
| 728 | exchangehashpart(t, &newt); /* pretend table has new hash */ |
| 729 | reinsertOldSlice(t, oldasize, newasize); |
| 730 | exchangehashpart(t, &newt); /* restore old hash (in case of errors) */ |
| 731 | } |
| 732 | /* allocate new array */ |
| 733 | newarray = resizearray(L, t, oldasize, newasize); |
| 734 | if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ |
| 735 | freehash(L, &newt); /* release new hash part */ |
| 736 | luaM_error(L); /* raise error (with array unchanged) */ |
| 737 | } |
| 738 | /* allocation ok; initialize new part of the array */ |
| 739 | exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ |
| 740 | t->array = newarray; /* set new array part */ |
| 741 | t->asize = newasize; |
| 742 | if (newarray != NULL) |
| 743 | *lenhint(t) = newasize / 2u; /* set an initial hint */ |
| 744 | clearNewSlice(t, oldasize, newasize); |
| 745 | /* re-insert elements from old hash part into new parts */ |
| 746 | reinserthash(L, &newt, t); /* 'newt' now has the old hash */ |
| 747 | freehash(L, &newt); /* free old hash part */ |
| 748 | } |
| 749 | |
| 750 | |
| 751 | void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { |
no test coverage detected