aboutsummaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-29 14:38:48 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-29 14:38:48 -0200
commitc5fa0895e98a3e915f01003f8ae29cde31243cc8 (patch)
tree873574e37491f10215ce8ee349795ea282036aa5 /lmem.c
parentd015f1fc02e03864b0ed3ad668a6e0660417a718 (diff)
downloadlua-c5fa0895e98a3e915f01003f8ae29cde31243cc8.tar.gz
lua-c5fa0895e98a3e915f01003f8ae29cde31243cc8.tar.bz2
lua-c5fa0895e98a3e915f01003f8ae29cde31243cc8.zip
table sizes don't need to be primes; power of 2 gives the same performance.
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c21
1 files changed, 6 insertions, 15 deletions
diff --git a/lmem.c b/lmem.c
index cfdf7ac6..7caa0f3e 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.19 1999/10/19 13:33:22 roberto Exp roberto $ 2** $Id: lmem.c,v 1.20 1999/11/22 13:12:07 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,6 +10,7 @@
10#define LUA_REENTRANT 10#define LUA_REENTRANT
11 11
12#include "lmem.h" 12#include "lmem.h"
13#include "lobject.h"
13#include "lstate.h" 14#include "lstate.h"
14#include "lua.h" 15#include "lua.h"
15 16
@@ -24,27 +25,17 @@
24#endif 25#endif
25 26
26 27
27#define MINSIZE 8 /* minimum size for "growing" vectors */
28 28
29 29
30 30void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
31 31 int inc, int size, const char *errormsg, unsigned long limit) {
32static unsigned long power2 (unsigned long n) {
33 unsigned long p = MINSIZE;
34 while (p<=n) p<<=1;
35 return p;
36}
37
38
39void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size,
40 const char *errormsg, unsigned long limit) {
41 unsigned long newn = nelems+inc; 32 unsigned long newn = nelems+inc;
42 if (newn >= limit) lua_error(L, errormsg); 33 if (newn >= limit) lua_error(L, errormsg);
43 if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */ 34 if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
44 (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */ 35 (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
45 return block; /* do not need to reallocate */ 36 return block; /* do not need to reallocate */
46 else /* it crossed a power of 2 boundary; grow to next power */ 37 else /* it crossed a power of 2 boundary; grow to next power */
47 return luaM_realloc(L, block, power2(newn)*size); 38 return luaM_realloc(L, block, luaO_power2(newn)*size);
48} 39}
49 40
50 41