diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-06 14:01:29 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-06 14:01:29 -0200 |
commit | 1f917e709ca3fe41cf07fd4bf99a080883521394 (patch) | |
tree | 00e89f7218a38e028bebe111dbf682925fe4cca0 /lmem.c | |
parent | d444153dbea006f5e0547f27a11256af96ba1829 (diff) | |
download | lua-1f917e709ca3fe41cf07fd4bf99a080883521394.tar.gz lua-1f917e709ca3fe41cf07fd4bf99a080883521394.tar.bz2 lua-1f917e709ca3fe41cf07fd4bf99a080883521394.zip |
better use of extra include files (both for tests and for old_ansi)
Diffstat (limited to 'lmem.c')
-rw-r--r-- | lmem.c | 115 |
1 files changed, 6 insertions, 109 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.c,v 1.44 2001/01/24 15:45:33 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.45 2001/02/05 19:08:01 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 | */ |
@@ -16,115 +16,12 @@ | |||
16 | 16 | ||
17 | 17 | ||
18 | 18 | ||
19 | 19 | #ifndef l_realloc | |
20 | #ifdef LUA_DEBUG | 20 | #define l_realloc(b,os,s) realloc(b,s) |
21 | /* | 21 | #define l_free(b,s) free(b) |
22 | ** {====================================================================== | ||
23 | ** Controlled version for realloc. | ||
24 | ** ======================================================================= | ||
25 | */ | ||
26 | |||
27 | |||
28 | #include <assert.h> | ||
29 | #include <limits.h> | ||
30 | #include <string.h> | ||
31 | |||
32 | #define basicrealloc(b, os, s) debug_realloc(b, os, s) | ||
33 | #define basicfree(b, s) debug_realloc(b, s, 0) | ||
34 | |||
35 | |||
36 | /* ensures maximum alignment for HEADER */ | ||
37 | #define HEADER (sizeof(union L_Umaxalign)) | ||
38 | |||
39 | #define MARKSIZE 32 | ||
40 | #define MARK 0x55 /* 01010101 (a nice pattern) */ | ||
41 | |||
42 | |||
43 | #define blocksize(b) ((size_t *)((char *)(b) - HEADER)) | ||
44 | |||
45 | unsigned long memdebug_numblocks = 0; | ||
46 | unsigned long memdebug_total = 0; | ||
47 | unsigned long memdebug_maxmem = 0; | ||
48 | unsigned long memdebug_memlimit = ULONG_MAX; | ||
49 | |||
50 | |||
51 | static void *checkblock (void *block) { | ||
52 | size_t *b = blocksize(block); | ||
53 | size_t size = *b; | ||
54 | int i; | ||
55 | for (i=0;i<MARKSIZE;i++) | ||
56 | assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */ | ||
57 | return b; | ||
58 | } | ||
59 | |||
60 | |||
61 | static void freeblock (void *block) { | ||
62 | if (block) { | ||
63 | size_t size = *blocksize(block); | ||
64 | block = checkblock(block); | ||
65 | memset(block, -1, size+HEADER+MARKSIZE); /* erase block */ | ||
66 | free(block); /* free original block */ | ||
67 | memdebug_numblocks--; | ||
68 | memdebug_total -= size; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | |||
73 | static void *debug_realloc (void *block, size_t oldsize, size_t size) { | ||
74 | assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block)); | ||
75 | if (size == 0) { | ||
76 | freeblock(block); | ||
77 | return NULL; | ||
78 | } | ||
79 | else if (memdebug_total+size > memdebug_memlimit) | ||
80 | return NULL; /* to test memory allocation errors */ | ||
81 | else { | ||
82 | char *newblock; | ||
83 | int i; | ||
84 | size_t realsize = HEADER+size+MARKSIZE; | ||
85 | if (realsize < size) return NULL; /* overflow! */ | ||
86 | newblock = (char *)malloc(realsize); /* alloc a new block */ | ||
87 | if (newblock == NULL) return NULL; | ||
88 | if (oldsize > size) oldsize = size; | ||
89 | if (block) { | ||
90 | memcpy(newblock+HEADER, block, oldsize); | ||
91 | freeblock(block); /* erase (and check) old copy */ | ||
92 | } | ||
93 | /* initialize new part of the block with something `weird' */ | ||
94 | memset(newblock+HEADER+oldsize, -MARK, size-oldsize); | ||
95 | memdebug_total += size; | ||
96 | if (memdebug_total > memdebug_maxmem) | ||
97 | memdebug_maxmem = memdebug_total; | ||
98 | memdebug_numblocks++; | ||
99 | *(size_t *)newblock = size; | ||
100 | for (i=0;i<MARKSIZE;i++) | ||
101 | *(newblock+HEADER+size+i) = (char)(MARK+i); | ||
102 | return newblock+HEADER; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | |||
107 | /* }====================================================================== */ | ||
108 | |||
109 | #else | ||
110 | /* no debug */ | ||
111 | |||
112 | /* | ||
113 | ** Real ISO (ANSI) systems do not need these tests; | ||
114 | ** but some systems (Sun OS) are not that ISO... | ||
115 | */ | ||
116 | #ifdef OLD_ANSI | ||
117 | #define basicrealloc(b,os,s) ((b) == NULL ? malloc(s) : realloc(b, s)) | ||
118 | #define basicfree(b,s) if (b) free(b) | ||
119 | #else | ||
120 | #define basicrealloc(b,os,s) realloc(b,s) | ||
121 | #define basicfree(b,s) free(b) | ||
122 | |||
123 | #endif | 22 | #endif |
124 | 23 | ||
125 | 24 | ||
126 | #endif | ||
127 | |||
128 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | 25 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, |
129 | int limit, const char *errormsg) { | 26 | int limit, const char *errormsg) { |
130 | void *newblock; | 27 | void *newblock; |
@@ -148,13 +45,13 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, | |||
148 | */ | 45 | */ |
149 | void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) { | 46 | void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) { |
150 | if (size == 0) { | 47 | if (size == 0) { |
151 | basicfree(block, oldsize); /* block may be NULL; that is OK for free */ | 48 | l_free(block, oldsize); /* block may be NULL; that is OK for free */ |
152 | block = NULL; | 49 | block = NULL; |
153 | } | 50 | } |
154 | else if (size >= MAX_SIZET) | 51 | else if (size >= MAX_SIZET) |
155 | luaD_error(L, "memory allocation error: block too big"); | 52 | luaD_error(L, "memory allocation error: block too big"); |
156 | else { | 53 | else { |
157 | block = basicrealloc(block, oldsize, size); | 54 | block = l_realloc(block, oldsize, size); |
158 | if (block == NULL) { | 55 | if (block == NULL) { |
159 | if (L) | 56 | if (L) |
160 | luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ | 57 | luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */ |