mirror of https://github.com/rusefi/lua.git
avoids type punning for table keys
This commit is contained in:
parent
fa936f8fa9
commit
ffb798e1e2
14
lobject.h
14
lobject.h
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lobject.h,v 2.17 2005/06/13 14:19:00 roberto Exp roberto $
|
** $Id: lobject.h,v 2.18 2005/10/24 17:37:33 roberto Exp roberto $
|
||||||
** Type definitions for Lua objects
|
** Type definitions for Lua objects
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -323,9 +323,12 @@ typedef union Closure {
|
||||||
** Tables
|
** Tables
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef struct TKey {
|
typedef union TKey {
|
||||||
TValuefields;
|
struct {
|
||||||
struct Node *next; /* for chaining */
|
TValuefields;
|
||||||
|
struct Node *next; /* for chaining */
|
||||||
|
} nk;
|
||||||
|
TValue tvk;
|
||||||
} TKey;
|
} TKey;
|
||||||
|
|
||||||
|
|
||||||
|
@ -360,8 +363,9 @@ typedef struct Table {
|
||||||
#define sizenode(t) (twoto((t)->lsizenode))
|
#define sizenode(t) (twoto((t)->lsizenode))
|
||||||
|
|
||||||
|
|
||||||
|
#define luaO_nilobject (&luaO_nilobject_)
|
||||||
|
|
||||||
LUAI_DATA const TValue luaO_nilobject;
|
LUAI_DATA const TValue luaO_nilobject_;
|
||||||
|
|
||||||
#define ceillog2(x) (luaO_log2((x)-1) + 1)
|
#define ceillog2(x) (luaO_log2((x)-1) + 1)
|
||||||
|
|
||||||
|
|
49
ltable.c
49
ltable.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: ltable.c,v 2.28 2005/11/25 13:29:32 roberto Exp roberto $
|
** $Id: ltable.c,v 2.29 2005/12/22 16:19:56 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -70,9 +70,9 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const Node luaH_dummynode = {
|
const Node luaH_dummynode_ = {
|
||||||
{{NULL}, LUA_TNIL}, /* value */
|
{{NULL}, LUA_TNIL}, /* value */
|
||||||
{{NULL}, LUA_TNIL, NULL} /* key */
|
{{{NULL}, LUA_TNIL, NULL}} /* key */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -270,7 +270,7 @@ static void setarrayvector (lua_State *L, Table *t, int size) {
|
||||||
static void setnodevector (lua_State *L, Table *t, int size) {
|
static void setnodevector (lua_State *L, Table *t, int size) {
|
||||||
int lsize;
|
int lsize;
|
||||||
if (size == 0) { /* no elements to hash part? */
|
if (size == 0) { /* no elements to hash part? */
|
||||||
t->node = cast(Node *, &luaH_dummynode); /* use common `dummynode' */
|
t->node = cast(Node *, luaH_dummynode); /* use common `dummynode' */
|
||||||
lsize = 0;
|
lsize = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -281,9 +281,10 @@ static void setnodevector (lua_State *L, Table *t, int size) {
|
||||||
size = twoto(lsize);
|
size = twoto(lsize);
|
||||||
t->node = luaM_newvector(L, size, Node);
|
t->node = luaM_newvector(L, size, Node);
|
||||||
for (i=0; i<size; i++) {
|
for (i=0; i<size; i++) {
|
||||||
gnext(&t->node[i]) = NULL;
|
Node *n = gnode(t, i);
|
||||||
setnilvalue(gkey(gnode(t, i)));
|
gnext(n) = NULL;
|
||||||
setnilvalue(gval(gnode(t, i)));
|
setnilvalue(gkey(n));
|
||||||
|
setnilvalue(gval(n));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t->lsizenode = cast_byte(lsize);
|
t->lsizenode = cast_byte(lsize);
|
||||||
|
@ -316,13 +317,13 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||||
if (!ttisnil(gval(old)))
|
if (!ttisnil(gval(old)))
|
||||||
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
|
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
|
||||||
}
|
}
|
||||||
if (nold != &luaH_dummynode)
|
if (nold != luaH_dummynode)
|
||||||
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
|
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
|
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
|
||||||
int nsize = (t->node == &luaH_dummynode) ? 0 : sizenode(t);
|
int nsize = (t->node == luaH_dummynode) ? 0 : sizenode(t);
|
||||||
resize(L, t, nasize, nsize);
|
resize(L, t, nasize, nsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,7 +362,7 @@ Table *luaH_new (lua_State *L, int narray, int nhash) {
|
||||||
t->array = NULL;
|
t->array = NULL;
|
||||||
t->sizearray = 0;
|
t->sizearray = 0;
|
||||||
t->lsizenode = 0;
|
t->lsizenode = 0;
|
||||||
t->node = cast(Node *, &luaH_dummynode);
|
t->node = cast(Node *, luaH_dummynode);
|
||||||
setarrayvector(L, t, narray);
|
setarrayvector(L, t, narray);
|
||||||
setnodevector(L, t, nhash);
|
setnodevector(L, t, nhash);
|
||||||
return t;
|
return t;
|
||||||
|
@ -369,7 +370,7 @@ Table *luaH_new (lua_State *L, int narray, int nhash) {
|
||||||
|
|
||||||
|
|
||||||
void luaH_free (lua_State *L, Table *t) {
|
void luaH_free (lua_State *L, Table *t) {
|
||||||
if (t->node != &luaH_dummynode)
|
if (t->node != luaH_dummynode)
|
||||||
luaM_freearray(L, t->node, sizenode(t), Node);
|
luaM_freearray(L, t->node, sizenode(t), Node);
|
||||||
luaM_freearray(L, t->array, t->sizearray, TValue);
|
luaM_freearray(L, t->array, t->sizearray, TValue);
|
||||||
luaM_free(L, t);
|
luaM_free(L, t);
|
||||||
|
@ -395,14 +396,14 @@ static Node *getfreepos (Table *t) {
|
||||||
*/
|
*/
|
||||||
static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
|
static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
|
||||||
Node *mp = luaH_mainposition(t, key);
|
Node *mp = luaH_mainposition(t, key);
|
||||||
if (!ttisnil(gval(mp)) || mp == &luaH_dummynode) {
|
if (!ttisnil(gval(mp)) || mp == luaH_dummynode) {
|
||||||
Node *othern;
|
Node *othern;
|
||||||
Node *n = getfreepos(t); /* get a free place */
|
Node *n = getfreepos(t); /* get a free place */
|
||||||
if (n == NULL) { /* cannot find a free place? */
|
if (n == NULL) { /* cannot find a free place? */
|
||||||
rehash(L, t, key); /* grow table */
|
rehash(L, t, key); /* grow table */
|
||||||
return luaH_set(L, t, key); /* re-insert key into grown table */
|
return luaH_set(L, t, key); /* re-insert key into grown table */
|
||||||
}
|
}
|
||||||
lua_assert(n != &luaH_dummynode);
|
lua_assert(n != luaH_dummynode);
|
||||||
othern = luaH_mainposition(t, key2tval(mp));
|
othern = luaH_mainposition(t, key2tval(mp));
|
||||||
if (othern != mp) { /* is colliding node out of its main position? */
|
if (othern != mp) { /* is colliding node out of its main position? */
|
||||||
/* yes; move colliding node into free position */
|
/* yes; move colliding node into free position */
|
||||||
|
@ -441,7 +442,7 @@ const TValue *luaH_getnum (Table *t, int key) {
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else n = gnext(n);
|
else n = gnext(n);
|
||||||
} while (n);
|
} while (n);
|
||||||
return &luaO_nilobject;
|
return luaO_nilobject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -456,7 +457,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else n = gnext(n);
|
else n = gnext(n);
|
||||||
} while (n);
|
} while (n);
|
||||||
return &luaO_nilobject;
|
return luaO_nilobject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -465,7 +466,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
|
||||||
*/
|
*/
|
||||||
const TValue *luaH_get (Table *t, const TValue *key) {
|
const TValue *luaH_get (Table *t, const TValue *key) {
|
||||||
switch (ttype(key)) {
|
switch (ttype(key)) {
|
||||||
case LUA_TNIL: return &luaO_nilobject;
|
case LUA_TNIL: return luaO_nilobject;
|
||||||
case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
|
case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
|
||||||
case LUA_TNUMBER: {
|
case LUA_TNUMBER: {
|
||||||
int k;
|
int k;
|
||||||
|
@ -482,7 +483,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
||||||
return gval(n); /* that's it */
|
return gval(n); /* that's it */
|
||||||
else n = gnext(n);
|
else n = gnext(n);
|
||||||
} while (n);
|
} while (n);
|
||||||
return &luaO_nilobject;
|
return luaO_nilobject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -491,7 +492,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
||||||
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
||||||
const TValue *p = luaH_get(t, key);
|
const TValue *p = luaH_get(t, key);
|
||||||
t->flags = 0;
|
t->flags = 0;
|
||||||
if (p != &luaO_nilobject)
|
if (p != luaO_nilobject)
|
||||||
return cast(TValue *, p);
|
return cast(TValue *, p);
|
||||||
else {
|
else {
|
||||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||||
|
@ -504,7 +505,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
||||||
|
|
||||||
TValue *luaH_setnum (lua_State *L, Table *t, int key) {
|
TValue *luaH_setnum (lua_State *L, Table *t, int key) {
|
||||||
const TValue *p = luaH_getnum(t, key);
|
const TValue *p = luaH_getnum(t, key);
|
||||||
if (p != &luaO_nilobject)
|
if (p != luaO_nilobject)
|
||||||
return cast(TValue *, p);
|
return cast(TValue *, p);
|
||||||
else {
|
else {
|
||||||
TValue k;
|
TValue k;
|
||||||
|
@ -516,7 +517,7 @@ TValue *luaH_setnum (lua_State *L, Table *t, int key) {
|
||||||
|
|
||||||
TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
|
TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
|
||||||
const TValue *p = luaH_getstr(t, key);
|
const TValue *p = luaH_getstr(t, key);
|
||||||
if (p != &luaO_nilobject)
|
if (p != luaO_nilobject)
|
||||||
return cast(TValue *, p);
|
return cast(TValue *, p);
|
||||||
else {
|
else {
|
||||||
TValue k;
|
TValue k;
|
||||||
|
@ -528,11 +529,11 @@ TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
|
||||||
|
|
||||||
static int unbound_search (Table *t, unsigned int j) {
|
static int unbound_search (Table *t, unsigned int j) {
|
||||||
unsigned int i = j; /* i is zero or a present index */
|
unsigned int i = j; /* i is zero or a present index */
|
||||||
j = j+1;
|
j++;
|
||||||
/* find `i' and `j' such that i is present and j is not */
|
/* find `i' and `j' such that i is present and j is not */
|
||||||
while (!ttisnil(luaH_getnum(t, j))) {
|
while (!ttisnil(luaH_getnum(t, j))) {
|
||||||
i = j;
|
i = j;
|
||||||
j = i*2;
|
j *= 2;
|
||||||
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
|
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
|
||||||
/* table was built with bad purposes: resort to linear search */
|
/* table was built with bad purposes: resort to linear search */
|
||||||
i = 1;
|
i = 1;
|
||||||
|
@ -541,7 +542,7 @@ static int unbound_search (Table *t, unsigned int j) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* now do a binary search between them */
|
/* now do a binary search between them */
|
||||||
while (i < j-1) {
|
while (j - i > 1) {
|
||||||
unsigned int m = (i+j)/2;
|
unsigned int m = (i+j)/2;
|
||||||
if (ttisnil(luaH_getnum(t, m))) j = m;
|
if (ttisnil(luaH_getnum(t, m))) j = m;
|
||||||
else i = m;
|
else i = m;
|
||||||
|
@ -567,7 +568,7 @@ int luaH_getn (Table *t) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
/* else must find a boundary in hash part */
|
/* else must find a boundary in hash part */
|
||||||
else if (t->node == &luaH_dummynode) /* hash part is empty? */
|
else if (t->node == luaH_dummynode) /* hash part is empty? */
|
||||||
return j; /* that is easy... */
|
return j; /* that is easy... */
|
||||||
else return unbound_search(t, j);
|
else return unbound_search(t, j);
|
||||||
}
|
}
|
||||||
|
|
14
ltable.h
14
ltable.h
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: ltable.h,v 2.7 2005/04/25 19:24:10 roberto Exp roberto $
|
** $Id: ltable.h,v 2.8 2005/06/06 13:30:25 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -11,15 +11,13 @@
|
||||||
|
|
||||||
|
|
||||||
#define gnode(t,i) (&(t)->node[i])
|
#define gnode(t,i) (&(t)->node[i])
|
||||||
#define gkey(n) (&(n)->i_key)
|
#define gkey(n) (&(n)->i_key.nk)
|
||||||
#define gval(n) (&(n)->i_val)
|
#define gval(n) (&(n)->i_val)
|
||||||
#define gnext(n) ((n)->i_key.next)
|
#define gnext(n) ((n)->i_key.nk.next)
|
||||||
|
|
||||||
#define key2tval(n) (cast(const TValue *, gkey(n)))
|
#define key2tval(n) (&(n)->i_key.tvk)
|
||||||
|
|
||||||
|
|
||||||
LUAI_DATA const Node luaH_dummynode;
|
|
||||||
|
|
||||||
LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
|
LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
|
||||||
LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
|
LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
|
||||||
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
||||||
|
@ -35,5 +33,9 @@ LUAI_FUNC int luaH_getn (Table *t);
|
||||||
/* exported only for debugging */
|
/* exported only for debugging */
|
||||||
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
||||||
|
|
||||||
|
#define luaH_dummynode (&luaH_dummynode_)
|
||||||
|
|
||||||
|
LUAI_DATA const Node luaH_dummynode_;
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue