From 96253ed8ceb38afa50887ccb5500442b5b220f08 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Nov 2000 15:39:56 -0200 Subject: [PATCH] better support for 64-bit machines (avoid excessive use of longs) --- lapi.c | 4 ++-- ldo.c | 5 +++-- lgc.c | 6 +++--- llimits.h | 12 ++++++++---- lmem.c | 20 ++++++++++---------- lmem.h | 16 ++++++++-------- lobject.c | 6 +++--- lobject.h | 6 +++--- lstate.h | 8 ++++---- lstring.c | 19 +++++++++++-------- lstring.h | 6 +++--- lstrlib.c | 22 ++++++++++++---------- ltable.c | 17 ++++++++++------- ltable.h | 4 ++-- lvm.c | 6 +++--- 15 files changed, 85 insertions(+), 72 deletions(-) diff --git a/lapi.c b/lapi.c index 70581f44..de73b3bb 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.109 2000/10/26 12:47:05 roberto Exp roberto $ +** $Id: lapi.c,v 1.110 2000/10/30 12:50:09 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -389,7 +389,7 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) { /* GC values are expressed in Kbytes: #bytes/2^10 */ #define GCscale(x) ((int)((x)>>10)) -#define GCunscale(x) ((unsigned long)(x)<<10) +#define GCunscale(x) ((mem_int)(x)<<10) LUA_API int lua_getgcthreshold (lua_State *L) { return GCscale(L->GCthreshold); diff --git a/ldo.c b/ldo.c index 0aedd8fb..140cff45 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.108 2000/10/20 16:39:03 roberto Exp roberto $ +** $Id: ldo.c,v 1.109 2000/10/30 12:38:50 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -241,7 +241,7 @@ static void f_parser (lua_State *L, void *ud) { static int protectedparser (lua_State *L, ZIO *z, int bin) { struct ParserS p; - unsigned long old_blocks; + mem_int old_blocks; int status; p.z = z; p.bin = bin; luaC_checkGC(L); @@ -249,6 +249,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) { status = luaD_runprotected(L, f_parser, &p); if (status == 0) { /* add new memory to threshold (as it probably will stay) */ + LUA_ASSERT(L->nblocks >= old_blocks, "cannot reduce memory usage here"); L->GCthreshold += (L->nblocks - old_blocks); } else if (status == LUA_ERRRUN) /* an error occurred: correct error code */ diff --git a/lgc.c b/lgc.c index 3ffc331d..6949b061 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.71 2000/10/05 13:00:17 roberto Exp roberto $ +** $Id: lgc.c,v 1.72 2000/10/26 12:47:05 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -229,7 +229,7 @@ static void collecttable (lua_State *L) { static void checktab (lua_State *L, stringtable *tb) { - if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10) + if (tb->nuse < (luint32)(tb->size/4) && tb->size > 10) luaS_resize(L, tb, tb->size/2); /* table is too big */ } @@ -286,7 +286,7 @@ static void collectudata (lua_State *L, int all) { static void checkMbuffer (lua_State *L) { if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */ size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */ - L->nblocks += (newsize - L->Mbuffsize)*sizeof(char); + L->nblocks -= (L->Mbuffsize - newsize)*sizeof(char); L->Mbuffsize = newsize; luaM_reallocvector(L, L->Mbuffer, newsize, char); } diff --git a/llimits.h b/llimits.h index ca1619ba..4a53fc7b 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.18 2000/10/09 13:47:32 roberto Exp roberto $ +** $Id: llimits.h,v 1.19 2000/10/26 12:47:05 roberto Exp roberto $ ** Limits, basic types, and some other "installation-dependent" definitions ** See Copyright Notice in lua.h */ @@ -50,7 +50,11 @@ typedef LUA_NUM_TYPE Number; -typedef unsigned long lint32; /* unsigned int with at least 32 bits */ +typedef unsigned long luint32; /* unsigned int with at least 32 bits */ +typedef long lint32; /* signed int with at least 32 bits */ + +/* an unsigned integer big enough to count the total memory used by Lua */ +typedef unsigned long mem_int; #define MAX_SIZET ((size_t)(~(size_t)0)-2) @@ -62,7 +66,7 @@ typedef unsigned long lint32; /* unsigned int with at least 32 bits */ ** conversion of pointer to int (for hashing only) ** (the shift removes bits that are usually 0 because of alignment) */ -#define IntPoint(p) (((unsigned long)(p)) >> 3) +#define IntPoint(p) (((luint32)(p)) >> 3) @@ -87,7 +91,7 @@ union L_Umaxalign { double d; char *s; long l; }; ** For a very small machine, you may change that to 2 bytes (and adjust ** the following limits accordingly) */ -typedef unsigned long Instruction; +typedef luint32 Instruction; /* diff --git a/lmem.c b/lmem.c index 612d5899..3a219f27 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.38 2000/10/26 12:47:05 roberto Exp roberto $ +** $Id: lmem.c,v 1.39 2000/10/30 16:29:59 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -41,17 +41,17 @@ #define MARK 0x55 /* 01010101 (a nice pattern) */ -#define blocksize(b) ((unsigned long *)((char *)(b) - HEADER)) +#define blocksize(b) ((size_t *)((char *)(b) - HEADER)) -unsigned long memdebug_numblocks = 0; -unsigned long memdebug_total = 0; -unsigned long memdebug_maxmem = 0; -unsigned long memdebug_memlimit = LONG_MAX; +mem_int memdebug_numblocks = 0; +mem_int memdebug_total = 0; +mem_int memdebug_maxmem = 0; +mem_int memdebug_memlimit = LONG_MAX; static void *checkblock (void *block) { - unsigned long *b = blocksize(block); - unsigned long size = *b; + size_t *b = blocksize(block); + size_t size = *b; int i; for (i=0;i memdebug_maxmem) memdebug_maxmem = memdebug_total; memdebug_numblocks++; - *(unsigned long *)newblock = size; + *(size_t *)newblock = size; for (i=0;i>5)|1; /* if string is too long, don't hash all its chars */ for (; l>=step; l-=step) h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); @@ -62,7 +62,7 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) { TString *p = tb->hash[i]; while (p) { /* for each node in the list */ TString *next = p->nexthash; /* save next */ - unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); + luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value); int h1 = h&(newsize-1); /* new position */ LUA_ASSERT(h%newsize == (h&(newsize-1)), "a&(x-1) == a%x, for x power of 2"); @@ -72,7 +72,10 @@ void luaS_resize (lua_State *L, stringtable *tb, int newsize) { } } luaM_free(L, tb->hash); - L->nblocks += (newsize - tb->size)*sizeof(TString *); + if (newsize > tb->size) /* avoid "unsigned negative" values */ + L->nblocks += (newsize - tb->size)*sizeof(TString *); + else + L->nblocks -= (tb->size - newsize)*sizeof(TString *); tb->size = newsize; tb->hash = newhash; } @@ -82,14 +85,14 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) { ts->nexthash = tb->hash[h]; /* chain new entry */ tb->hash[h] = ts; tb->nuse++; - if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */ + if (tb->nuse > (luint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */ luaS_resize(L, tb, tb->size*2); } TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { - unsigned long h = hash_s(str, l); + luint32 h = hash_s(str, l); int h1 = h & (L->strt.size-1); TString *ts; for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) { @@ -113,7 +116,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { TString *luaS_newudata (lua_State *L, size_t s, void *udata) { union L_UTString *uts = (union L_UTString *)luaM_malloc(L, - (lint32)sizeof(union L_UTString)+s); + (luint32)sizeof(union L_UTString)+s); TString *ts = &uts->ts; ts->marked = 0; ts->nexthash = NULL; diff --git a/lstring.h b/lstring.h index b940fba6..af733b74 100644 --- a/lstring.h +++ b/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.23 2000/10/26 12:47:05 roberto Exp roberto $ +** $Id: lstring.h,v 1.24 2000/10/30 17:49:19 roberto Exp roberto $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -20,8 +20,8 @@ #define RESERVEDMARK 3 -#define sizestring(l) ((long)sizeof(TString) + \ - ((long)(l+1)-TSPACK)*(long)sizeof(char)) +#define sizestring(l) ((lint32)sizeof(TString) + \ + ((lint32)(l+1)-TSPACK)*(lint32)sizeof(char)) void luaS_init (lua_State *L); diff --git a/lstrlib.c b/lstrlib.c index 9b960190..5cc0e23b 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.56 2000/10/27 16:15:53 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.57 2000/11/23 13:49:35 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -17,6 +17,8 @@ #include "lualib.h" +typedef long sint32; /* a "signed" version for size_t */ + static int str_len (lua_State *L) { size_t l; @@ -26,19 +28,19 @@ static int str_len (lua_State *L) { } -static long posrelat (long pos, size_t len) { +static sint32 posrelat (sint32 pos, size_t len) { /* relative string position: negative means back from end */ - return (pos>=0) ? pos : (long)len+pos+1; + return (pos>=0) ? pos : (sint32)len+pos+1; } static int str_sub (lua_State *L) { size_t l; const char *s = luaL_check_lstr(L, 1, &l); - long start = posrelat(luaL_check_long(L, 2), l); - long end = posrelat(luaL_opt_long(L, 3, -1), l); + sint32 start = posrelat(luaL_check_long(L, 2), l); + sint32 end = posrelat(luaL_opt_long(L, 3, -1), l); if (start < 1) start = 1; - if (end > (long)l) end = l; + if (end > (sint32)l) end = l; if (start <= end) lua_pushlstring(L, s+start-1, end-start+1); else lua_pushstring(L, ""); @@ -87,7 +89,7 @@ static int str_rep (lua_State *L) { static int str_byte (lua_State *L) { size_t l; const char *s = luaL_check_lstr(L, 1, &l); - long pos = posrelat(luaL_opt_long(L, 2, 1), l); + sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l); luaL_arg_check(L, 0src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) i++; /* keeps trying to match with the maximum repetitions */ @@ -399,7 +401,7 @@ static int str_find (lua_State *L) { size_t l1, l2; const char *s = luaL_check_lstr(L, 1, &l1); const char *p = luaL_check_lstr(L, 2, &l2); - long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; + sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range"); if (lua_gettop(L) > 3 || /* extra argument? */ strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ diff --git a/ltable.c b/ltable.c index 4aef89c0..8782d33e 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.57 2000/10/05 12:14:08 roberto Exp roberto $ +** $Id: ltable.c,v 1.58 2000/10/26 12:47:05 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -40,10 +40,10 @@ ** of its hash value) */ Node *luaH_mainposition (const Hash *t, const TObject *key) { - unsigned long h; + luint32 h; switch (ttype(key)) { case LUA_TNUMBER: - h = (unsigned long)(long)nvalue(key); + h = (luint32)(lint32)nvalue(key); break; case LUA_TSTRING: h = tsvalue(key)->u.s.hash; @@ -82,7 +82,7 @@ static const TObject *luaH_getany (lua_State *L, const Hash *t, /* specialized version for numbers */ const TObject *luaH_getnum (const Hash *t, Number key) { - Node *n = &t->node[(unsigned long)(long)key&(t->size-1)]; + Node *n = &t->node[(luint32)(lint32)key&(t->size-1)]; do { if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key) return &n->val; @@ -158,7 +158,7 @@ void luaH_remove (Hash *t, TObject *key) { } -static void setnodevector (lua_State *L, Hash *t, lint32 size) { +static void setnodevector (lua_State *L, Hash *t, luint32 size) { int i; if (size > MAX_INT) lua_error(L, "table overflow"); @@ -167,7 +167,10 @@ static void setnodevector (lua_State *L, Hash *t, lint32 size) { ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL; t->node[i].next = NULL; } - L->nblocks += gcsize(L, size) - gcsize(L, t->size); + if ((int)size > t->size) /* avoid "unsigned negative" values */ + L->nblocks += gcsize(L, size) - gcsize(L, t->size); + else + L->nblocks -= gcsize(L, t->size) - gcsize(L, size); t->size = size; t->firstfree = &t->node[size-1]; /* first free position to be used */ } @@ -214,7 +217,7 @@ static void rehash (lua_State *L, Hash *t) { int i; LUA_ASSERT(nelems<=oldsize, "wrong count"); if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */ - setnodevector(L, t, (lint32)oldsize*2); + setnodevector(L, t, (luint32)oldsize*2); else if (nelems <= oldsize/4 && /* less than 1/4? */ oldsize > MINPOWER2) setnodevector(L, t, oldsize/2); diff --git a/ltable.h b/ltable.h index 00dfd1c0..be147e41 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $ +** $Id: ltable.h,v 1.24 2000/08/31 14:08:27 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -24,7 +24,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key); Node * luaH_next (lua_State *L, const Hash *t, const TObject *r); TObject *luaH_setint (lua_State *L, Hash *t, int key); void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val); -unsigned long luaH_hash (lua_State *L, const TObject *key); +luint32 luaH_hash (lua_State *L, const TObject *key); const TObject *luaH_getglobal (lua_State *L, const char *name); /* exported only for debugging */ diff --git a/lvm.c b/lvm.c index 71741cb5..8c9407e7 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.145 2000/10/06 12:45:25 roberto Exp roberto $ +** $Id: lvm.c,v 1.146 2000/10/26 12:47:05 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -295,8 +295,8 @@ void luaV_strconc (lua_State *L, int total, StkId top) { } else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */ /* at least two string values; get as many as possible */ - lint32 tl = (lint32)tsvalue(top-1)->len + - (lint32)tsvalue(top-2)->len; + luint32 tl = (luint32)tsvalue(top-1)->len + + (luint32)tsvalue(top-2)->len; char *buffer; int i; while (n < total && !tostring(L, top-n-1)) { /* collect total length */