summaryrefslogtreecommitdiffstats
path: root/lua/ltable.c
diff options
context:
space:
mode:
Diffstat (limited to 'lua/ltable.c')
-rw-r--r--lua/ltable.c193
1 files changed, 92 insertions, 101 deletions
diff --git a/lua/ltable.c b/lua/ltable.c
index ec84f4f..9581add 100644
--- a/lua/ltable.c
+++ b/lua/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
+** $Id: ltable.c,v 2.67 2011/11/30 12:41:45 roberto Exp $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -18,7 +18,6 @@
** Hence even when the load factor reaches 100%, performance remains good.
*/
-#include <math.h>
#include <string.h>
#define ltable_c
@@ -32,14 +31,16 @@
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
+#include "lstring.h"
#include "ltable.h"
+#include "lvm.h"
/*
** max size of array part is 2^MAXBITS
*/
-#if LUAI_BITSINT > 26
-#define MAXBITS 26
+#if LUAI_BITSINT >= 32
+#define MAXBITS 30
#else
#define MAXBITS (LUAI_BITSINT-2)
#endif
@@ -48,7 +49,7 @@
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
-
+
#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
#define hashboolean(t,p) hashpow2(t, p)
@@ -63,18 +64,13 @@
#define hashpointer(t,p) hashmod(t, IntPoint(p))
-/*
-** number of ints inside a lua_Number
-*/
-#define numints cast_int(sizeof(lua_Number)/sizeof(int))
-
-
-
#define dummynode (&dummynode_)
+#define isdummy(n) ((n) == dummynode)
+
static const Node dummynode_ = {
- {{NULL}, LUA_TNIL}, /* value */
- {{{NULL}, LUA_TNIL, NULL}} /* key */
+ {NILCONSTANT}, /* value */
+ {{NILCONSTANT, NULL}} /* key */
};
@@ -82,13 +78,14 @@ static const Node dummynode_ = {
** hash for lua_Numbers
*/
static Node *hashnum (const Table *t, lua_Number n) {
- unsigned int a[numints];
int i;
- if (luai_numeq(n, 0)) /* avoid problems with -0 */
- return gnode(t, 0);
- memcpy(a, &n, sizeof(a));
- for (i = 1; i < numints; i++) a[0] += a[i];
- return hashmod(t, a[0]);
+ luai_hashnum(i, n);
+ if (i < 0) {
+ if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
+ i = 0; /* handle INT_MIN */
+ i = -i; /* must be a positive value */
+ }
+ return hashmod(t, i);
}
@@ -107,6 +104,8 @@ static Node *mainposition (const Table *t, const TValue *key) {
return hashboolean(t, bvalue(key));
case LUA_TLIGHTUSERDATA:
return hashpointer(t, pvalue(key));
+ case LUA_TLCF:
+ return hashpointer(t, fvalue(key));
default:
return hashpointer(t, gcvalue(key));
}
@@ -132,7 +131,7 @@ static int arrayindex (const TValue *key) {
/*
** returns the index of a `key' for table traversals. First goes all
** elements in the array part, then elements in the hash part. The
-** beginning of a traversal is signalled by -1.
+** beginning of a traversal is signaled by -1.
*/
static int findindex (lua_State *L, Table *t, StkId key) {
int i;
@@ -142,19 +141,19 @@ static int findindex (lua_State *L, Table *t, StkId key) {
return i-1; /* yes; that's the index (corrected to C) */
else {
Node *n = mainposition(t, key);
- do { /* check whether `key' is somewhere in the chain */
+ for (;;) { /* check whether `key' is somewhere in the chain */
/* key may be dead already, but it is ok to use it in `next' */
- if (luaO_rawequalObj(key2tval(n), key) ||
- (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
- gcvalue(gkey(n)) == gcvalue(key))) {
+ if (luaV_rawequalobj(gkey(n), key) ||
+ (ttisdeadkey(gkey(n)) && iscollectable(key) &&
+ deadvalue(gkey(n)) == gcvalue(key))) {
i = cast_int(n - gnode(t, 0)); /* key index in hash table */
/* hash elements are numbered after array ones */
return i + t->sizearray;
}
else n = gnext(n);
- } while (n);
- luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
- return 0; /* to avoid warnings */
+ if (n == NULL)
+ luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
+ }
}
}
@@ -170,7 +169,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
}
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
- setobj2s(L, key, key2tval(gnode(t, i)));
+ setobj2s(L, key, gkey(gnode(t, i)));
setobj2s(L, key+1, gval(gnode(t, i)));
return 1;
}
@@ -211,7 +210,7 @@ static int computesizes (int nums[], int *narray) {
static int countint (const TValue *key, int *nums) {
int k = arrayindex(key);
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
- nums[ceillog2(k)]++; /* count as such */
+ nums[luaO_ceillog2(k)]++; /* count as such */
return 1;
}
else
@@ -251,7 +250,7 @@ static int numusehash (const Table *t, int *nums, int *pnasize) {
while (i--) {
Node *n = &t->node[i];
if (!ttisnil(gval(n))) {
- ause += countint(key2tval(n), nums);
+ ause += countint(gkey(n), nums);
totaluse++;
}
}
@@ -277,7 +276,7 @@ static void setnodevector (lua_State *L, Table *t, int size) {
}
else {
int i;
- lsize = ceillog2(size);
+ lsize = luaO_ceillog2(size);
if (lsize > MAXBITS)
luaG_runerror(L, "table overflow");
size = twoto(lsize);
@@ -294,7 +293,7 @@ static void setnodevector (lua_State *L, Table *t, int size) {
}
-static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
+void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
int i;
int oldasize = t->sizearray;
int oldhsize = t->lsizenode;
@@ -302,13 +301,13 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize);
/* create new hash part with appropriate size */
- setnodevector(L, t, nhsize);
+ setnodevector(L, t, nhsize);
if (nasize < oldasize) { /* array part must shrink? */
t->sizearray = nasize;
/* re-insert elements from vanishing slice */
for (i=nasize; i<oldasize; i++) {
if (!ttisnil(&t->array[i]))
- setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
+ luaH_setint(L, t, i + 1, &t->array[i]);
}
/* shrink array */
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
@@ -316,23 +315,26 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
/* re-insert elements from hash part */
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
Node *old = nold+i;
- if (!ttisnil(gval(old)))
- setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
+ if (!ttisnil(gval(old))) {
+ /* doesn't need barrier/invalidate cache, as entry was
+ already present in the table */
+ setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
+ }
}
- if (nold != dummynode)
- luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
+ if (!isdummy(nold))
+ luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */
}
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
- int nsize = (t->node == dummynode) ? 0 : sizenode(t);
- resize(L, t, nasize, nsize);
+ int nsize = isdummy(t->node) ? 0 : sizenode(t);
+ luaH_resize(L, t, nasize, nsize);
}
static void rehash (lua_State *L, Table *t, const TValue *ek) {
int nasize, na;
- int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
+ int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
int i;
int totaluse;
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
@@ -345,7 +347,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
/* compute new size for array part */
na = computesizes(nums, &nasize);
/* resize the table to new computed sizes */
- resize(L, t, nasize, totaluse - na);
+ luaH_resize(L, t, nasize, totaluse - na);
}
@@ -355,32 +357,28 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
*/
-Table *luaH_new (lua_State *L, int narray, int nhash) {
- Table *t = luaM_new(L, Table);
- luaC_link(L, obj2gco(t), LUA_TTABLE);
+Table *luaH_new (lua_State *L) {
+ Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
t->metatable = NULL;
t->flags = cast_byte(~0);
- /* temporary values (kept only if some malloc fails) */
t->array = NULL;
t->sizearray = 0;
- t->lsizenode = 0;
- t->node = cast(Node *, dummynode);
- setarrayvector(L, t, narray);
- setnodevector(L, t, nhash);
+ setnodevector(L, t, 0);
return t;
}
void luaH_free (lua_State *L, Table *t) {
- if (t->node != dummynode)
- luaM_freearray(L, t->node, sizenode(t), Node);
- luaM_freearray(L, t->array, t->sizearray, TValue);
+ if (!isdummy(t->node))
+ luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
+ luaM_freearray(L, t->array, t->sizearray);
luaM_free(L, t);
}
static Node *getfreepos (Table *t) {
- while (t->lastfree-- > t->node) {
+ while (t->lastfree > t->node) {
+ t->lastfree--;
if (ttisnil(gkey(t->lastfree)))
return t->lastfree;
}
@@ -390,23 +388,28 @@ static Node *getfreepos (Table *t) {
/*
-** inserts a new key into a hash table; first, check whether key's main
-** position is free. If not, check whether colliding node is in its main
-** position or not: if it is not, move colliding node to an empty place and
-** put new key in its main position; otherwise (colliding node is in its main
-** position), new key goes to an empty position.
+** inserts a new key into a hash table; first, check whether key's main
+** position is free. If not, check whether colliding node is in its main
+** position or not: if it is not, move colliding node to an empty place and
+** put new key in its main position; otherwise (colliding node is in its main
+** position), new key goes to an empty position.
*/
-static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
- Node *mp = mainposition(t, key);
- if (!ttisnil(gval(mp)) || mp == dummynode) {
+TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
+ Node *mp;
+ if (ttisnil(key)) luaG_runerror(L, "table index is nil");
+ else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
+ luaG_runerror(L, "table index is NaN");
+ mp = mainposition(t, key);
+ if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
Node *othern;
Node *n = getfreepos(t); /* get a free place */
if (n == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */
- return luaH_set(L, t, key); /* re-insert key into grown table */
+ /* whatever called 'newkey' take care of TM cache and GC barrier */
+ return luaH_set(L, t, key); /* insert key into grown table */
}
- lua_assert(n != dummynode);
- othern = mainposition(t, key2tval(mp));
+ lua_assert(!isdummy(n));
+ othern = mainposition(t, gkey(mp));
if (othern != mp) { /* is colliding node out of its main position? */
/* yes; move colliding node into free position */
while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
@@ -422,8 +425,8 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
mp = n;
}
}
- gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
- luaC_barriert(L, t, key);
+ setobj2t(L, gkey(mp), key);
+ luaC_barrierback(L, obj2gco(t), key);
lua_assert(ttisnil(gval(mp)));
return gval(mp);
}
@@ -432,7 +435,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
/*
** search function for integers
*/
-const TValue *luaH_getnum (Table *t, int key) {
+const TValue *luaH_getint (Table *t, int key) {
/* (1 <= key && key <= t->sizearray) */
if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
return &t->array[key-1];
@@ -455,7 +458,7 @@ const TValue *luaH_getnum (Table *t, int key) {
const TValue *luaH_getstr (Table *t, TString *key) {
Node *n = hashstr(t, key);
do { /* check whether `key' is somewhere in the chain */
- if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
+ if (ttisstring(gkey(n)) && eqstr(rawtsvalue(gkey(n)), key))
return gval(n); /* that's it */
else n = gnext(n);
} while (n);
@@ -467,7 +470,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
** main search function
*/
const TValue *luaH_get (Table *t, const TValue *key) {
- switch (ttype(key)) {
+ switch (ttypenv(key)) {
case LUA_TNIL: return luaO_nilobject;
case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
case LUA_TNUMBER: {
@@ -475,13 +478,13 @@ const TValue *luaH_get (Table *t, const TValue *key) {
lua_Number n = nvalue(key);
lua_number2int(k, n);
if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
- return luaH_getnum(t, k); /* use specialized version */
+ return luaH_getint(t, k); /* use specialized version */
/* else go through */
}
default: {
Node *n = mainposition(t, key);
do { /* check whether `key' is somewhere in the chain */
- if (luaO_rawequalObj(key2tval(n), key))
+ if (luaV_rawequalobj(gkey(n), key))
return gval(n); /* that's it */
else n = gnext(n);
} while (n);
@@ -491,41 +494,29 @@ const TValue *luaH_get (Table *t, const TValue *key) {
}
+/*
+** beware: when using this function you probably need to check a GC
+** barrier and invalidate the TM cache.
+*/
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
const TValue *p = luaH_get(t, key);
- t->flags = 0;
if (p != luaO_nilobject)
return cast(TValue *, p);
- else {
- if (ttisnil(key)) luaG_runerror(L, "table index is nil");
- else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
- luaG_runerror(L, "table index is NaN");
- return newkey(L, t, key);
- }
+ else return luaH_newkey(L, t, key);
}
-TValue *luaH_setnum (lua_State *L, Table *t, int key) {
- const TValue *p = luaH_getnum(t, key);
+void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
+ const TValue *p = luaH_getint(t, key);
+ TValue *cell;
if (p != luaO_nilobject)
- return cast(TValue *, p);
+ cell = cast(TValue *, p);
else {
TValue k;
setnvalue(&k, cast_num(key));
- return newkey(L, t, &k);
- }
-}
-
-
-TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
- const TValue *p = luaH_getstr(t, key);
- if (p != luaO_nilobject)
- return cast(TValue *, p);
- else {
- TValue k;
- setsvalue(L, &k, key);
- return newkey(L, t, &k);
+ cell = luaH_newkey(L, t, &k);
}
+ setobj2t(L, cell, value);
}
@@ -533,20 +524,20 @@ static int unbound_search (Table *t, unsigned int j) {
unsigned int i = j; /* i is zero or a present index */
j++;
/* find `i' and `j' such that i is present and j is not */
- while (!ttisnil(luaH_getnum(t, j))) {
+ while (!ttisnil(luaH_getint(t, j))) {
i = j;
j *= 2;
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
/* table was built with bad purposes: resort to linear search */
i = 1;
- while (!ttisnil(luaH_getnum(t, i))) i++;
+ while (!ttisnil(luaH_getint(t, i))) i++;
return i - 1;
}
}
/* now do a binary search between them */
while (j - i > 1) {
unsigned int m = (i+j)/2;
- if (ttisnil(luaH_getnum(t, m))) j = m;
+ if (ttisnil(luaH_getint(t, m))) j = m;
else i = m;
}
return i;
@@ -570,7 +561,7 @@ int luaH_getn (Table *t) {
return i;
}
/* else must find a boundary in hash part */
- else if (t->node == dummynode) /* hash part is empty? */
+ else if (isdummy(t->node)) /* hash part is empty? */
return j; /* that is easy... */
else return unbound_search(t, j);
}
@@ -583,6 +574,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) {
return mainposition(t, key);
}
-int luaH_isdummy (Node *n) { return n == dummynode; }
+int luaH_isdummy (Node *n) { return isdummy(n); }
#endif