summaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-12-26 16:46:09 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-12-26 16:46:09 -0200
commit8c49e198654567f770a7d5081b886a7c35201d81 (patch)
tree8c1de3e885ef138574e51a8868f175feeaab71e2 /lmem.c
parent6af005ec20323defd2a5ead01e2d389462884d04 (diff)
downloadlua-8c49e198654567f770a7d5081b886a7c35201d81.tar.gz
lua-8c49e198654567f770a7d5081b886a7c35201d81.tar.bz2
lua-8c49e198654567f770a7d5081b886a7c35201d81.zip
explicit control of size for growing vectors
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/lmem.c b/lmem.c
index 3a219f27..830f6f1d 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.39 2000/10/30 16:29:59 roberto Exp roberto $ 2** $Id: lmem.c,v 1.40 2000/11/24 17:39:56 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*/
@@ -116,15 +116,20 @@ static void *debug_realloc (void *block, size_t size) {
116#endif 116#endif
117 117
118 118
119void *luaM_growaux (lua_State *L, void *block, size_t nelems, 119void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
120 int inc, size_t size, const char *errormsg, size_t limit) { 120 int limit, const char *errormsg) {
121 size_t newn = nelems+inc; 121 void *newblock;
122 if (nelems >= limit-inc) lua_error(L, errormsg); 122 int newsize = (*size)*2;
123 if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */ 123 if (newsize < MINPOWER2)
124 (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */ 124 newsize = MINPOWER2; /* minimum size */
125 return block; /* do not need to reallocate */ 125 else if (*size >= limit/2) { /* cannot double it? */
126 else /* it crossed a power-of-2 boundary; grow to next power */ 126 if (*size < limit - MINPOWER2) /* try something smaller... */
127 return luaM_realloc(L, block, luaO_power2(newn)*size); 127 newsize = limit; /* still have at least MINPOWER2 free places */
128 else lua_error(L, errormsg);
129 }
130 newblock = luaM_realloc(L, block, (luint32)newsize*(luint32)size_elems);
131 *size = newsize; /* update only when everything else is OK */
132 return newblock;
128} 133}
129 134
130 135