diff --git a/engine/lua/lmem.cpp b/engine/lua/lmem.cpp index 32cb465ca49..773b218c7aa 100644 --- a/engine/lua/lmem.cpp +++ b/engine/lua/lmem.cpp @@ -50,7 +50,7 @@ int32 numblocks = 0; int32 totalmem = 0; static void *checkblock(void *block) { - int32 *b = (uint32 *)((char *)block - HEADER); + int32 *b = (int32 *)((char *)block - HEADER); int32 size = *b; LUA_ASSERT(*(((char *)b) + size + HEADER) == MARK, "corrupted block"); numblocks--; diff --git a/engine/lua/lobject.h b/engine/lua/lobject.h index 098cf461cfd..a34d0ee7312 100644 --- a/engine/lua/lobject.h +++ b/engine/lua/lobject.h @@ -90,7 +90,7 @@ typedef struct GCnode { typedef struct TaggedString { GCnode head; - unsigned long hash; + uint32 hash; int32 constindex; // hint to reuse constants (= -1 if this is a userdata) union { struct { diff --git a/engine/lua/lstring.cpp b/engine/lua/lstring.cpp index 788aa1803e7..671c067f6cc 100644 --- a/engine/lua/lstring.cpp +++ b/engine/lua/lstring.cpp @@ -4,6 +4,7 @@ ** See Copyright Notice in lua.h */ +#include "common/util.h" #include "engine/lua/lmem.h" #include "engine/lua/lobject.h" @@ -29,7 +30,7 @@ void luaS_init() { static uint32 hash_s(const char *s, int32 l) { uint32 h = 0; while (l--) - h = ((h << 5) - h) ^ (byte) * (s++); + h = ((h << 5) - h) ^ (byte)*(s++); return h; } @@ -103,14 +104,14 @@ static TaggedString *insert_s (const char *str, int32 l, stringtable *tb) { int32 size = tb->size; int32 i; int32 j = -1; - if ((int32)tb->nuse * 3 >= size * 2) { + if (tb->nuse * 3 >= size * 2) { grow(tb); size = tb->size; } for (i = h % size; (ts = tb->hash[i]); ) { if (ts == &EMPTY) j = i; - else if (ts->constindex >= 0 && ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) + else if (ts->constindex >= 0 && ts->u.s.len == l && (memcmp(str, ts->str, MAX(l, ts->u.s.len)) == 0)) return ts; if (++i == size) i = 0; @@ -126,7 +127,11 @@ static TaggedString *insert_s (const char *str, int32 l, stringtable *tb) { static TaggedString *insert_u(void *buff, int32 tag, stringtable *tb) { TaggedString *ts; - unsigned long h = (unsigned long)buff; +#ifdef TARGET_64BITS + uint32 h = (uint64)buff; +#else + uint32 h = (uint32)buff; +#endif int32 size = tb->size; int32 i; int32 j = -1; @@ -147,12 +152,16 @@ static TaggedString *insert_u(void *buff, int32 tag, stringtable *tb) { i = j; else tb->nuse++; - ts = tb->hash[i] = newone_u((char*)buff, tag, h); + ts = tb->hash[i] = newone_u((char *)buff, tag, h); return ts; } TaggedString *luaS_createudata(void *udata, int32 tag) { - return insert_u(udata, tag, &L->string_root[(unsigned long)udata % NUM_HASHS]); +#ifdef TARGET_64BITS + return insert_u(udata, tag, &L->string_root[(uint64)udata % NUM_HASHS]); +#else + return insert_u(udata, tag, &L->string_root[(uint32)udata % NUM_HASHS]); +#endif } TaggedString *luaS_newlstr(const char *str, int32 l) { diff --git a/engine/lua/ltable.cpp b/engine/lua/ltable.cpp index 52a1b651cba..52ee66c69d3 100644 --- a/engine/lua/ltable.cpp +++ b/engine/lua/ltable.cpp @@ -17,30 +17,32 @@ #define REHASH_LIMIT 0.70 // avoid more than this % full #define TagDefault LUA_T_ARRAY; -static long int hashindex(TObject *ref) { - long int h; +#ifdef TARGET_64BITS +static int64 int hashindex(TObject *ref) { + int64 h; + switch (ttype(ref)) { case LUA_T_NUMBER: - h = (long int)nvalue(ref); + h = (int64)nvalue(ref); break; case LUA_T_STRING: case LUA_T_USERDATA: - h = (long int)tsvalue(ref); + h = (int64)tsvalue(ref); break; case LUA_T_ARRAY: - h = (long int)avalue(ref); + h = (int64)avalue(ref); break; case LUA_T_PROTO: - h = (long int)tfvalue(ref); + h = (int64)tfvalue(ref); break; case LUA_T_CPROTO: - h = (long int)fvalue(ref); + h = (int64)fvalue(ref); break; case LUA_T_CLOSURE: - h = (long int)clvalue(ref); + h = (int64)clvalue(ref); break; case LUA_T_TASK: - h = (long int)nvalue(ref); + h = (int64)nvalue(ref); break; default: lua_error("unexpected type to index table"); @@ -49,9 +51,46 @@ static long int hashindex(TObject *ref) { return (h >= 0 ? h : -(h + 1)); } +#else + +static int32 hashindex(TObject *ref) { + int32 h; + + switch (ttype(ref)) { + case LUA_T_NUMBER: + h = (int32)nvalue(ref); + break; + case LUA_T_STRING: + case LUA_T_USERDATA: + h = (int32)tsvalue(ref); + break; + case LUA_T_ARRAY: + h = (int32)avalue(ref); + break; + case LUA_T_PROTO: + h = (int32)tfvalue(ref); + break; + case LUA_T_CPROTO: + h = (int32)fvalue(ref); + break; + case LUA_T_CLOSURE: + h = (int32)clvalue(ref); + break; + case LUA_T_TASK: + h = (int32)nvalue(ref); + break; + default: + lua_error("unexpected type to index table"); + h = 0; // to avoid warnings + } + return (h >= 0 ? h : -(h + 1)); +} + +#endif + int32 present(Hash *t, TObject *key) { int32 tsize = nhash(t); - long int h = hashindex(key); + int32 h = (int32)hashindex(key); int32 h1 = h % tsize; TObject *rf = ref(node(t, h1)); if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {