aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-01-22 15:28:00 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-01-22 15:28:00 -0200
commit758a38164481891acb1dd07003c1726554034d67 (patch)
treee3657c72da8d24ff0677bbea070cbe64d9f0f0c2
parenteec31aaca58eb4a92a00c28748e81ee773f51a88 (diff)
downloadlua-758a38164481891acb1dd07003c1726554034d67.tar.gz
lua-758a38164481891acb1dd07003c1726554034d67.tar.bz2
lua-758a38164481891acb1dd07003c1726554034d67.zip
"realloc" usually implements "malloc"; handle non ANSI case separately.
-rw-r--r--lmem.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/lmem.c b/lmem.c
index 854ad7d2..a2cd11fd 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.6 1998/06/19 16:14:09 roberto Exp roberto $ 2** $Id: lmem.c,v 1.7 1998/06/29 22:03:06 roberto Exp $
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*/
@@ -12,10 +12,19 @@
12#include "lua.h" 12#include "lua.h"
13 13
14 14
15/*
16** real ANSI systems do not need some of these tests,
17** since realloc(NULL, s)==malloc(s).
18** But some systems (Sun OS) are not that ANSI...
19*/
20#ifdef OLD_ANSI
21#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
22#endif
23
24
15 25
16int luaM_growaux (void **block, unsigned long nelems, int size, 26int luaM_growaux (void **block, unsigned long nelems, int size,
17 char *errormsg, unsigned long limit) 27 char *errormsg, unsigned long limit) {
18{
19 if (nelems >= limit) 28 if (nelems >= limit)
20 lua_error(errormsg); 29 lua_error(errormsg);
21 nelems = (nelems == 0) ? 32 : nelems*2; 30 nelems = (nelems == 0) ? 32 : nelems*2;
@@ -31,22 +40,16 @@ int luaM_growaux (void **block, unsigned long nelems, int size,
31 40
32/* 41/*
33** generic allocation routine. 42** generic allocation routine.
34** real ANSI systems do not need some of these tests,
35** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b).
36** But some systems (e.g. Sun OS) are not that ANSI...
37*/ 43*/
38void *luaM_realloc (void *block, unsigned long size) 44void *luaM_realloc (void *block, unsigned long size) {
39{
40 size_t s = (size_t)size; 45 size_t s = (size_t)size;
41 if (s != size) 46 if (s != size)
42 lua_error("Allocation Error: Block too big"); 47 lua_error("Allocation Error: Block too big");
43 if (size == 0) { 48 if (size == 0) {
44 if (block) { 49 free(block); /* block may be NULL, that is OK for free */
45 free(block);
46 }
47 return NULL; 50 return NULL;
48 } 51 }
49 block = block ? realloc(block, s) : malloc(s); 52 block = realloc(block, s);
50 if (block == NULL) 53 if (block == NULL)
51 lua_error(memEM); 54 lua_error(memEM);
52 return block; 55 return block;
@@ -68,8 +71,7 @@ unsigned long numblocks = 0;
68unsigned long totalmem = 0; 71unsigned long totalmem = 0;
69 72
70 73
71static void *checkblock (void *block) 74static void *checkblock (void *block) {
72{
73 unsigned long *b = (unsigned long *)((char *)block - HEADER); 75 unsigned long *b = (unsigned long *)((char *)block - HEADER);
74 unsigned long size = *b; 76 unsigned long size = *b;
75 LUA_ASSERT(*(((char *)b)+size+HEADER) == MARK, 77 LUA_ASSERT(*(((char *)b)+size+HEADER) == MARK,
@@ -80,26 +82,22 @@ static void *checkblock (void *block)
80} 82}
81 83
82 84
83void *luaM_realloc (void *block, unsigned long size) 85void *luaM_realloc (void *block, unsigned long size) {
84{
85 unsigned long realsize = HEADER+size+1; 86 unsigned long realsize = HEADER+size+1;
86 if (realsize != (size_t)realsize) 87 if (realsize != (size_t)realsize)
87 lua_error("Allocation Error: Block too big"); 88 lua_error("Allocation Error: Block too big");
88 if (size == 0) { /* ANSI dosen't need this, but some machines... */ 89 if (size == 0) {
89 if (block) { 90 if (block) {
90 unsigned long *b = (unsigned long *)((char *)block - HEADER); 91 unsigned long *b = (unsigned long *)((char *)block - HEADER);
91 memset(block, -1, *b); /* erase block */ 92 memset(block, -1, *b); /* erase block */
92 block = checkblock(block); 93 block = checkblock(block);
93 free(block);
94 } 94 }
95 free(block);
95 return NULL; 96 return NULL;
96 } 97 }
97 if (block) { 98 if (block)
98 block = checkblock(block); 99 block = checkblock(block);
99 block = (unsigned long *)realloc(block, realsize); 100 block = (unsigned long *)realloc(block, realsize);
100 }
101 else
102 block = (unsigned long *)malloc(realsize);
103 if (block == NULL) 101 if (block == NULL)
104 lua_error(memEM); 102 lua_error(memEM);
105 totalmem += size; 103 totalmem += size;