diff options
Diffstat (limited to 'lmem.c')
-rw-r--r-- | lmem.c | 17 |
1 files changed, 9 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.c,v 1.24 2000/01/13 16:30:47 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.25 2000/02/08 16:34:31 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 | */ |
@@ -43,7 +43,9 @@ | |||
43 | #define free(b) debug_realloc(b, 0) | 43 | #define free(b) debug_realloc(b, 0) |
44 | 44 | ||
45 | 45 | ||
46 | #define HEADER (sizeof(double)) /* maximum alignment */ | 46 | /* ensures maximum alignment for HEADER */ |
47 | #define HEADER (sizeof(double)>sizeof(long) ? sizeof(double) : sizeof(long)) | ||
48 | |||
47 | #define MARKSIZE 16 | 49 | #define MARKSIZE 16 |
48 | #define MARK 0x55 /* 01010101 (a nice pattern) */ | 50 | #define MARK 0x55 /* 01010101 (a nice pattern) */ |
49 | 51 | ||
@@ -77,21 +79,21 @@ static void freeblock (void *block) { | |||
77 | 79 | ||
78 | 80 | ||
79 | static void *debug_realloc (void *block, size_t size) { | 81 | static void *debug_realloc (void *block, size_t size) { |
80 | size_t realsize = HEADER+size+MARKSIZE; | ||
81 | if (size == 0) { | 82 | if (size == 0) { |
82 | freeblock(block); | 83 | freeblock(block); |
83 | return NULL; | 84 | return NULL; |
84 | } | 85 | } |
85 | else { | 86 | else { |
87 | size_t realsize = HEADER+size+MARKSIZE; | ||
86 | char *newblock = (char *)(malloc)(realsize); /* alloc a new block */ | 88 | char *newblock = (char *)(malloc)(realsize); /* alloc a new block */ |
87 | int i; | 89 | int i; |
90 | if (newblock == NULL) return NULL; | ||
88 | if (block) { | 91 | if (block) { |
89 | size_t oldsize = *blocksize(block); | 92 | size_t oldsize = *blocksize(block); |
90 | if (oldsize > size) oldsize = size; | 93 | if (oldsize > size) oldsize = size; |
91 | memcpy(newblock+HEADER, block, oldsize); | 94 | memcpy(newblock+HEADER, block, oldsize); |
92 | freeblock(block); /* erase (and check) old copy */ | 95 | freeblock(block); /* erase (and check) old copy */ |
93 | } | 96 | } |
94 | if (newblock == NULL) return NULL; | ||
95 | memdebug_total += size; | 97 | memdebug_total += size; |
96 | memdebug_numblocks++; | 98 | memdebug_numblocks++; |
97 | *(unsigned long *)newblock = size; | 99 | *(unsigned long *)newblock = size; |
@@ -123,14 +125,13 @@ void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, | |||
123 | ** generic allocation routine. | 125 | ** generic allocation routine. |
124 | */ | 126 | */ |
125 | void *luaM_realloc (lua_State *L, void *block, unsigned long size) { | 127 | void *luaM_realloc (lua_State *L, void *block, unsigned long size) { |
126 | size_t s = (size_t)size; | ||
127 | if (s != size) | ||
128 | lua_error(L, "memory allocation error: block too big"); | ||
129 | if (size == 0) { | 128 | if (size == 0) { |
130 | free(block); /* block may be NULL; that is OK for free */ | 129 | free(block); /* block may be NULL; that is OK for free */ |
131 | return NULL; | 130 | return NULL; |
132 | } | 131 | } |
133 | block = realloc(block, s); | 132 | else if ((size_t)size != size) |
133 | lua_error(L, "memory allocation error: block too big"); | ||
134 | block = realloc(block, size); | ||
134 | if (block == NULL) | 135 | if (block == NULL) |
135 | lua_error(L, memEM); | 136 | lua_error(L, memEM); |
136 | return block; | 137 | return block; |