lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 96253ed8ceb38afa50887ccb5500442b5b220f08
parent 35d6b1505702b0f4a2eee0e6d2f8dfc50943a1a7
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Fri, 24 Nov 2000 15:39:34 -0200

better support for 64-bit machines (avoid excessive use of longs)

Diffstat:
Mlapi.c | 4++--
Mldo.c | 5+++--
Mlgc.c | 6+++---
Mllimits.h | 12++++++++----
Mlmem.c | 20++++++++++----------
Mlmem.h | 16++++++++--------
Mlobject.c | 6+++---
Mlobject.h | 6+++---
Mlstate.h | 8++++----
Mlstring.c | 19+++++++++++--------
Mlstring.h | 6+++---
Mlstrlib.c | 22++++++++++++----------
Mltable.c | 17++++++++++-------
Mltable.h | 4++--
Mlvm.c | 6+++---
15 files changed, 85 insertions(+), 72 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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<MARKSIZE;i++) assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */ @@ -93,7 +93,7 @@ static void *debug_realloc (void *block, size_t size) { memdebug_total += size; if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total; memdebug_numblocks++; - *(unsigned long *)newblock = size; + *(size_t *)newblock = size; for (i=0;i<MARKSIZE;i++) *(newblock+HEADER+size+i) = (char)(MARK+i); return newblock+HEADER; @@ -131,7 +131,7 @@ void *luaM_growaux (lua_State *L, void *block, size_t nelems, /* ** generic allocation routine. */ -void *luaM_realloc (lua_State *L, void *block, lint32 size) { +void *luaM_realloc (lua_State *L, void *block, luint32 size) { if (size == 0) { free(block); /* block may be NULL; that is OK for free */ return NULL; diff --git a/lmem.h b/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.15 2000/08/07 18:39:16 roberto Exp roberto $ +** $Id: lmem.h,v 1.16 2000/10/30 16:29:59 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -13,7 +13,7 @@ #include "llimits.h" #include "lua.h" -void *luaM_realloc (lua_State *L, void *oldblock, lint32 size); +void *luaM_realloc (lua_State *L, void *oldblock, luint32 size); void *luaM_growaux (lua_State *L, void *block, size_t nelems, int inc, size_t size, const char *errormsg, size_t limit); @@ -21,20 +21,20 @@ void *luaM_growaux (lua_State *L, void *block, size_t nelems, #define luaM_free(L, b) luaM_realloc(L, (b), 0) #define luaM_malloc(L, t) luaM_realloc(L, NULL, (t)) #define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t))) -#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(lint32)sizeof(t))) +#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(luint32)sizeof(t))) #define luaM_growvector(L, v,nelems,inc,t,e,l) \ ((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l)) #define luaM_reallocvector(L, v,n,t) \ - ((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t))) + ((v)=(t *)luaM_realloc(L, v,(n)*(luint32)sizeof(t))) #ifdef LUA_DEBUG -extern unsigned long memdebug_numblocks; -extern unsigned long memdebug_total; -extern unsigned long memdebug_maxmem; -extern unsigned long memdebug_memlimit; +extern mem_int memdebug_numblocks; +extern mem_int memdebug_total; +extern mem_int memdebug_maxmem; +extern mem_int memdebug_memlimit; #endif diff --git a/lobject.c b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 1.54 2000/10/10 19:53:20 roberto Exp roberto $ +** $Id: lobject.c,v 1.55 2000/10/20 16:36:32 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -30,8 +30,8 @@ const char *const luaO_typenames[] = { /* ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2) */ -lint32 luaO_power2 (lint32 n) { - lint32 p = MINPOWER2; +luint32 luaO_power2 (luint32 n) { + luint32 p = MINPOWER2; while (p<=n) p<<=1; return p; } diff --git a/lobject.h b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.81 2000/10/30 16:29:59 roberto Exp roberto $ +** $Id: lobject.h,v 1.82 2000/10/30 17:49:19 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -82,7 +82,7 @@ typedef struct lua_TObject { typedef struct TString { union { struct { /* for strings */ - unsigned long hash; + luint32 hash; int constindex; /* hint to reuse constants */ } s; struct { /* for userdata */ @@ -191,7 +191,7 @@ extern const char *const luaO_typenames[]; #define luaO_typename(o) (luaO_typenames[ttype(o)]) -lint32 luaO_power2 (lint32 n); +luint32 luaO_power2 (luint32 n); char *luaO_openspace (lua_State *L, size_t n); int luaO_equalObj (const TObject *t1, const TObject *t2); diff --git a/lstate.h b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 1.40 2000/09/29 12:42:13 roberto Exp roberto $ +** $Id: lstate.h,v 1.41 2000/10/05 13:00:17 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -37,7 +37,7 @@ struct TM; /* defined in ltm.h */ typedef struct stringtable { int size; - lint32 nuse; /* number of elements */ + luint32 nuse; /* number of elements */ TString **hash; } stringtable; @@ -65,8 +65,8 @@ struct lua_State { struct Ref *refArray; /* locked objects */ int refSize; /* size of refArray */ int refFree; /* list of free positions in refArray */ - unsigned long GCthreshold; - unsigned long nblocks; /* number of `bytes' currently allocated */ + mem_int GCthreshold; + mem_int nblocks; /* number of `bytes' currently allocated */ lua_Hook callhook; lua_Hook linehook; int allowhooks; diff --git a/lstring.c b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 1.44 2000/10/26 12:47:05 roberto Exp roberto $ +** $Id: lstring.c,v 1.45 2000/10/30 17:49:19 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -44,8 +44,8 @@ void luaS_freeall (lua_State *L) { } -static unsigned long hash_s (const char *s, size_t l) { - unsigned long h = l; /* seed */ +static luint32 hash_s (const char *s, size_t l) { + luint32 h = l; /* seed */ size_t step = (l>>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 @@ -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 @@ -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, 0<pos && (size_t)pos<=l, 2, "out of range"); lua_pushnumber(L, (unsigned char)s[pos-1]); return 1; @@ -126,7 +128,7 @@ typedef struct MatchState { int level; /* total number of captures (finished or unfinished) */ struct { const char *init; - long len; /* -1 signals unfinished capture */ + sint32 len; /* -1 signals unfinished capture */ } capture[MAX_CAPTURES]; lua_State *L; } MatchState; @@ -251,7 +253,7 @@ static const char *matchbalance (MatchState *ms, const char *s, const char *p) { static const char *max_expand (MatchState *ms, const char *s, const char *p, const char *ep) { - long i = 0; /* counts maximum expand for item */ + sint32 i = 0; /* counts maximum expand for item */ while ((s+i)<ms->src_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 @@ -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 @@ -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 @@ -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 */