** resizes the string table */
| 58 | ** resizes the string table |
| 59 | */ |
| 60 | void luaS_resize (lua_State *L, int newsize) { |
| 61 | int i; |
| 62 | stringtable *tb = &G(L)->strt; |
| 63 | if (newsize > tb->size) { /* grow table if needed */ |
| 64 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); |
| 65 | for (i = tb->size; i < newsize; i++) |
| 66 | tb->hash[i] = NULL; |
| 67 | } |
| 68 | for (i = 0; i < tb->size; i++) { /* rehash */ |
| 69 | TString *p = tb->hash[i]; |
| 70 | tb->hash[i] = NULL; |
| 71 | while (p) { /* for each node in the list */ |
| 72 | TString *hnext = p->hnext; /* save next */ |
| 73 | unsigned int h = lmod(p->hash, newsize); /* new position */ |
| 74 | p->hnext = tb->hash[h]; /* chain it */ |
| 75 | tb->hash[h] = p; |
| 76 | p = hnext; |
| 77 | } |
| 78 | } |
| 79 | if (newsize < tb->size) { /* shrink table if needed */ |
| 80 | /* vanishing slice should be empty */ |
| 81 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); |
| 82 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); |
| 83 | } |
| 84 | tb->size = newsize; |
| 85 | } |
| 86 | |
| 87 | |
| 88 |
no test coverage detected