block must always have a power-of-2 size (even at the limit)

This commit is contained in:
Roberto Ierusalimschy 1999-05-11 11:18:40 -03:00
parent 73308c7605
commit c390f73e3b
1 changed files with 5 additions and 10 deletions

15
lmem.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.c,v 1.13 1999/02/26 15:50:10 roberto Exp roberto $ ** $Id: lmem.c,v 1.14 1999/03/01 17:49:13 roberto Exp roberto $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -22,7 +22,7 @@
#endif #endif
#define MINSIZE 16 /* minimum size for "growing" vectors */ #define MINSIZE 8 /* minimum size for "growing" vectors */
@ -39,17 +39,12 @@ static unsigned long power2 (unsigned long n) {
void *luaM_growaux (void *block, unsigned long nelems, int inc, int size, void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
char *errormsg, unsigned long limit) { char *errormsg, unsigned long limit) {
unsigned long newn = nelems+inc; unsigned long newn = nelems+inc;
if (newn >= limit) lua_error(errormsg);
if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */ if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
(nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */ (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
return block; /* do not need to reallocate */ return block; /* do not need to reallocate */
else { /* it crossed a power of 2 boundary; grow to next power */ else /* it crossed a power of 2 boundary; grow to next power */
if (newn >= limit) return luaM_realloc(block, power2(newn)*size);
lua_error(errormsg);
newn = power2(newn);
if (newn > limit)
newn = limit;
return luaM_realloc(block, newn*size);
}
} }