aboutsummaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/lmem.c b/lmem.c
index a1379f24..b4947b6c 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.9 1999/01/22 18:08:57 roberto Exp roberto $ 2** $Id: lmem.c,v 1.10 1999/02/24 17:55:51 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*/
@@ -23,21 +23,36 @@
23#endif 23#endif
24 24
25 25
26#define MINSIZE 16 /* minimum size for "growing" vectors */
26 27
27#ifndef DEBUG
28 28
29int luaM_growaux (void **block, unsigned long nelems, int size, 29static unsigned long power2 (unsigned long n) {
30 unsigned long p = MINSIZE;
31 while (p<=n) p<<=1;
32 return p;
33}
34
35
36void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
30 char *errormsg, unsigned long limit) { 37 char *errormsg, unsigned long limit) {
31 if (nelems >= limit) 38 unsigned long newn = nelems+inc;
32 lua_error(errormsg); 39 if ((newn ^ nelems) > nelems) { /* cross a power of 2 boundary? */
33 nelems = (nelems == 0) ? 32 : nelems*2; 40 if (newn >= limit)
34 if (nelems > limit) 41 lua_error(errormsg);
35 nelems = limit; 42 newn = power2(newn);
36 *block = luaM_realloc(*block, nelems*size); 43 if (newn > limit)
37 return (int)nelems; 44 newn = limit;
45 return luaM_realloc(block, newn*size);
46 }
47 else {
48 LUA_ASSERT(power2(nelems) == power2(newn), "bad arithmetic");
49 return block;
50 }
38} 51}
39 52
40 53
54#ifndef DEBUG
55
41/* 56/*
42** generic allocation routine. 57** generic allocation routine.
43*/ 58*/
@@ -63,18 +78,6 @@ void *luaM_realloc (void *block, unsigned long size) {
63#include <string.h> 78#include <string.h>
64 79
65 80
66int luaM_growaux (void **block, unsigned long nelems, int size,
67 char *errormsg, unsigned long limit) {
68 if (nelems >= limit)
69 lua_error(errormsg);
70 nelems = nelems+1;
71 if (nelems > limit)
72 nelems = limit;
73 *block = luaM_realloc(*block, nelems*size);
74 return (int)nelems;
75}
76
77
78#define HEADER (sizeof(double)) 81#define HEADER (sizeof(double))
79 82
80#define MARK 55 83#define MARK 55