diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | dadba4d6ed9f7432185816abcbb788125aa991ff (patch) | |
tree | 343ec90be7f956520f7e8ac9bb6d8eb73f01138d | |
parent | d600a6b5b358c28d482b01f10bfa3292b17f5d12 (diff) | |
download | lua-dadba4d6ed9f7432185816abcbb788125aa991ff.tar.gz lua-dadba4d6ed9f7432185816abcbb788125aa991ff.tar.bz2 lua-dadba4d6ed9f7432185816abcbb788125aa991ff.zip |
Interface to Memory Manager
-rw-r--r-- | lmem.c | 137 | ||||
-rw-r--r-- | lmem.h | 45 | ||||
-rw-r--r-- | luamem.c | 163 | ||||
-rw-r--r-- | luamem.h | 40 |
4 files changed, 182 insertions, 203 deletions
@@ -0,0 +1,137 @@ | |||
1 | /* | ||
2 | ** $Id: $ | ||
3 | ** Interface to Memory Manager | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | |||
8 | #include <stdlib.h> | ||
9 | |||
10 | #include "lmem.h" | ||
11 | #include "lua.h" | ||
12 | |||
13 | |||
14 | |||
15 | int luaM_growaux (void **block, unsigned long nelems, int size, | ||
16 | char *errormsg, unsigned long limit) | ||
17 | { | ||
18 | if (nelems >= limit) | ||
19 | lua_error(errormsg); | ||
20 | nelems = (nelems == 0) ? 32 : nelems*2; | ||
21 | if (nelems > limit) | ||
22 | nelems = limit; | ||
23 | *block = luaM_realloc(*block, nelems*size); | ||
24 | return (int)nelems; | ||
25 | } | ||
26 | |||
27 | |||
28 | static unsigned long Mbuffsize = 0; | ||
29 | static char *Mbuffer = NULL; | ||
30 | |||
31 | |||
32 | void *luaM_buffer (unsigned long size) | ||
33 | { | ||
34 | if (size > Mbuffsize) { | ||
35 | Mbuffsize = size; | ||
36 | Mbuffer = luaM_realloc(Mbuffer, Mbuffsize); | ||
37 | } | ||
38 | return Mbuffer; | ||
39 | } | ||
40 | |||
41 | |||
42 | void luaM_clearbuffer (void) | ||
43 | { | ||
44 | Mbuffsize /= 2; | ||
45 | Mbuffer = luaM_realloc(Mbuffer, Mbuffsize); | ||
46 | } | ||
47 | |||
48 | |||
49 | #ifndef DEBUG | ||
50 | |||
51 | /* | ||
52 | ** generic allocation routine. | ||
53 | ** real ANSI systems do not need some of these tests, | ||
54 | ** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b). | ||
55 | ** But some systems (e.g. Sun OS) are not that ANSI... | ||
56 | */ | ||
57 | void *luaM_realloc (void *block, unsigned long size) | ||
58 | { | ||
59 | size_t s = (size_t)size; | ||
60 | if (s != size) | ||
61 | lua_error("Allocation Error: Block too big"); | ||
62 | if (size == 0) { | ||
63 | if (block) { | ||
64 | free(block); | ||
65 | } | ||
66 | return NULL; | ||
67 | } | ||
68 | block = block ? realloc(block, s) : malloc(s); | ||
69 | if (block == NULL) | ||
70 | lua_error(memEM); | ||
71 | return block; | ||
72 | } | ||
73 | |||
74 | |||
75 | |||
76 | #else | ||
77 | /* DEBUG */ | ||
78 | |||
79 | #include <assert.h> | ||
80 | #include <string.h> | ||
81 | |||
82 | |||
83 | #define MARK 55 | ||
84 | |||
85 | static unsigned long numblocks = 0; | ||
86 | static unsigned long totalmem = 0; | ||
87 | |||
88 | |||
89 | |||
90 | void luaM_query (void) | ||
91 | { | ||
92 | lua_pushnumber(totalmem); | ||
93 | lua_pushnumber(numblocks); | ||
94 | } | ||
95 | |||
96 | |||
97 | static void *checkblock (void *block) | ||
98 | { | ||
99 | unsigned long *b = (unsigned long *)block - 1; | ||
100 | unsigned long size = *b; | ||
101 | assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK); | ||
102 | numblocks--; | ||
103 | totalmem -= size; | ||
104 | return b; | ||
105 | } | ||
106 | |||
107 | |||
108 | void *luaM_realloc (void *block, unsigned long size) | ||
109 | { | ||
110 | unsigned long realsize = sizeof(unsigned long)+size+sizeof(char); | ||
111 | if (realsize != (size_t)realsize) | ||
112 | lua_error("Allocation Error: Block too big"); | ||
113 | if (size == 0) { /* ANSI doen't need this, but some machines... */ | ||
114 | if (block) { | ||
115 | memset(block, -1, *((unsigned long *)block-1)); /* erase block */ | ||
116 | block = checkblock(block); | ||
117 | free(block); | ||
118 | } | ||
119 | return NULL; | ||
120 | } | ||
121 | if (block) { | ||
122 | block = checkblock(block); | ||
123 | block = (unsigned long *)realloc(block, realsize); | ||
124 | } | ||
125 | else | ||
126 | block = (unsigned long *)malloc(realsize); | ||
127 | if (block == NULL) | ||
128 | lua_error(memEM); | ||
129 | totalmem += size; | ||
130 | numblocks++; | ||
131 | *(unsigned long *)block = size; | ||
132 | *(((char *)block)+size+sizeof(unsigned long)) = MARK; | ||
133 | return (unsigned long *)block+1; | ||
134 | } | ||
135 | |||
136 | |||
137 | #endif | ||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | ** $Id: $ | ||
3 | ** Interface to Memory Manager | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #ifndef lmem_h | ||
8 | #define lmem_h | ||
9 | |||
10 | |||
11 | #ifndef NULL | ||
12 | #define NULL 0 | ||
13 | #endif | ||
14 | |||
15 | |||
16 | /* memory error messages */ | ||
17 | #define codeEM "code size overflow" | ||
18 | #define symbolEM "symbol table overflow" | ||
19 | #define constantEM "constant table overflow" | ||
20 | #define stackEM "stack size overflow" | ||
21 | #define lexEM "lex buffer overflow" | ||
22 | #define refEM "reference table overflow" | ||
23 | #define tableEM "table overflow" | ||
24 | #define memEM "not enough memory" | ||
25 | |||
26 | void *luaM_buffer (unsigned long size); | ||
27 | void luaM_clearbuffer (void); | ||
28 | void *luaM_realloc (void *oldblock, unsigned long size); | ||
29 | int luaM_growaux (void **block, unsigned long nelems, int size, | ||
30 | char *errormsg, unsigned long limit); | ||
31 | |||
32 | #define luaM_free(b) luaM_realloc((b), 0) | ||
33 | #define luaM_malloc(t) luaM_realloc(NULL, (t)) | ||
34 | #define luaM_new(t) ((t *)luaM_malloc(sizeof(t))) | ||
35 | #define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t))) | ||
36 | #define luaM_growvector(old,n,t,e,l) \ | ||
37 | (luaM_growaux((void**)old,n,sizeof(t),e,l)) | ||
38 | #define luaM_reallocvector(v,n,t) ((t *)luaM_realloc(v,(n)*sizeof(t))) | ||
39 | |||
40 | |||
41 | void luaM_query (void); /* only ifdef DEBUG */ | ||
42 | |||
43 | |||
44 | #endif | ||
45 | |||
diff --git a/luamem.c b/luamem.c deleted file mode 100644 index 812da490..00000000 --- a/luamem.c +++ /dev/null | |||
@@ -1,163 +0,0 @@ | |||
1 | /* | ||
2 | ** mem.c | ||
3 | ** TecCGraf - PUC-Rio | ||
4 | */ | ||
5 | |||
6 | char *rcs_luamem = "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $"; | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | |||
10 | #include "luamem.h" | ||
11 | #include "lua.h" | ||
12 | |||
13 | |||
14 | #define DEBUG 0 | ||
15 | |||
16 | #if !DEBUG | ||
17 | |||
18 | static void lfree (void *block) | ||
19 | { | ||
20 | if (block) | ||
21 | { | ||
22 | *((char *)block) = -1; /* to catch errors */ | ||
23 | free(block); | ||
24 | } | ||
25 | } | ||
26 | |||
27 | |||
28 | void *luaI_realloc (void *oldblock, unsigned long size) | ||
29 | { | ||
30 | void *block; | ||
31 | size_t s = (size_t)size; | ||
32 | if (s != size) | ||
33 | lua_error("Allocation Error: Block too big"); | ||
34 | if (size == 0) { /* ANSI doen't need this, but some machines... */ | ||
35 | lfree(oldblock); | ||
36 | return NULL; | ||
37 | } | ||
38 | block = oldblock ? realloc(oldblock, s) : malloc(s); | ||
39 | if (block == NULL) | ||
40 | lua_error(memEM); | ||
41 | return block; | ||
42 | } | ||
43 | |||
44 | |||
45 | int luaI_growvector (void **block, unsigned long nelems, int size, | ||
46 | char *errormsg, unsigned long limit) | ||
47 | { | ||
48 | if (nelems >= limit) | ||
49 | lua_error(errormsg); | ||
50 | nelems = (nelems == 0) ? 20 : nelems*2; | ||
51 | if (nelems > limit) | ||
52 | nelems = limit; | ||
53 | *block = luaI_realloc(*block, nelems*size); | ||
54 | return (int)nelems; | ||
55 | } | ||
56 | |||
57 | |||
58 | void* luaI_buffer (unsigned long size) | ||
59 | { | ||
60 | static unsigned long buffsize = 0; | ||
61 | static char* buffer = NULL; | ||
62 | if (size > buffsize) | ||
63 | buffer = luaI_realloc(buffer, buffsize=size); | ||
64 | return buffer; | ||
65 | } | ||
66 | |||
67 | #else | ||
68 | /* DEBUG */ | ||
69 | |||
70 | #include <stdio.h> | ||
71 | |||
72 | # define assert(ex) {if (!(ex)){(void)fprintf(stderr, \ | ||
73 | "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}} | ||
74 | |||
75 | #define MARK 55 | ||
76 | |||
77 | static unsigned long numblocks = 0; | ||
78 | static unsigned long totalmem = 0; | ||
79 | |||
80 | |||
81 | static void message (void) | ||
82 | { | ||
83 | #define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3)) | ||
84 | static int count = 0; | ||
85 | static unsigned long lastnumblocks = 0; | ||
86 | static unsigned long lasttotalmem = 0; | ||
87 | if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem) | ||
88 | || count++ >= 5000) | ||
89 | { | ||
90 | fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000); | ||
91 | count = 0; | ||
92 | lastnumblocks = numblocks; | ||
93 | lasttotalmem = totalmem; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | |||
98 | void luaI_free (void *block) | ||
99 | { | ||
100 | if (block) | ||
101 | { | ||
102 | unsigned long *b = (unsigned long *)block - 1; | ||
103 | unsigned long size = *b; | ||
104 | assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK); | ||
105 | numblocks--; | ||
106 | totalmem -= size; | ||
107 | free(b); | ||
108 | message(); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | |||
113 | void *luaI_realloc (void *oldblock, unsigned long size) | ||
114 | { | ||
115 | unsigned long *block; | ||
116 | unsigned long realsize = sizeof(unsigned long)+size+sizeof(char); | ||
117 | if (realsize != (size_t)realsize) | ||
118 | lua_error("Allocation Error: Block too big"); | ||
119 | if (oldblock) | ||
120 | { | ||
121 | unsigned long *b = (unsigned long *)oldblock - 1; | ||
122 | unsigned long oldsize = *b; | ||
123 | assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK); | ||
124 | totalmem -= oldsize; | ||
125 | numblocks--; | ||
126 | block = (unsigned long *)realloc(b, realsize); | ||
127 | } | ||
128 | else | ||
129 | block = (unsigned long *)malloc(realsize); | ||
130 | if (block == NULL) | ||
131 | lua_error("not enough memory"); | ||
132 | totalmem += size; | ||
133 | numblocks++; | ||
134 | *block = size; | ||
135 | *(((char *)block)+size+sizeof(unsigned long)) = MARK; | ||
136 | message(); | ||
137 | return block+1; | ||
138 | } | ||
139 | |||
140 | |||
141 | int luaI_growvector (void **block, unsigned long nelems, int size, | ||
142 | char *errormsg, unsigned long limit) | ||
143 | { | ||
144 | if (nelems >= limit) | ||
145 | lua_error(errormsg); | ||
146 | nelems = (nelems == 0) ? 20 : nelems*2; | ||
147 | if (nelems > limit) | ||
148 | nelems = limit; | ||
149 | *block = luaI_realloc(*block, nelems*size); | ||
150 | return (int)nelems; | ||
151 | } | ||
152 | |||
153 | |||
154 | void* luaI_buffer (unsigned long size) | ||
155 | { | ||
156 | static unsigned long buffsize = 0; | ||
157 | static char* buffer = NULL; | ||
158 | if (size > buffsize) | ||
159 | buffer = luaI_realloc(buffer, buffsize=size); | ||
160 | return buffer; | ||
161 | } | ||
162 | |||
163 | #endif | ||
diff --git a/luamem.h b/luamem.h deleted file mode 100644 index f9c573d7..00000000 --- a/luamem.h +++ /dev/null | |||
@@ -1,40 +0,0 @@ | |||
1 | /* | ||
2 | ** mem.c | ||
3 | ** memory manager for lua | ||
4 | ** $Id: luamem.h,v 1.10 1997/07/29 20:38:45 roberto Exp roberto $ | ||
5 | */ | ||
6 | |||
7 | #ifndef luamem_h | ||
8 | #define luamem_h | ||
9 | |||
10 | #ifndef NULL | ||
11 | #define NULL 0 | ||
12 | #endif | ||
13 | |||
14 | |||
15 | /* memory error messages */ | ||
16 | #define codeEM "code size overflow" | ||
17 | #define symbolEM "symbol table overflow" | ||
18 | #define constantEM "constant table overflow" | ||
19 | #define stackEM "stack size overflow" | ||
20 | #define lexEM "lex buffer overflow" | ||
21 | #define refEM "reference table overflow" | ||
22 | #define tableEM "table overflow" | ||
23 | #define memEM "not enough memory" | ||
24 | |||
25 | |||
26 | void *luaI_realloc (void *oldblock, unsigned long size); | ||
27 | void *luaI_buffer (unsigned long size); | ||
28 | int luaI_growvector (void **block, unsigned long nelems, int size, | ||
29 | char *errormsg, unsigned long limit); | ||
30 | |||
31 | #define luaI_free(b) luaI_realloc((b), 0) | ||
32 | #define luaI_malloc(s) luaI_realloc(NULL, (s)) | ||
33 | #define new(s) ((s *)luaI_malloc(sizeof(s))) | ||
34 | #define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) | ||
35 | #define growvector(old,n,s,e,l) \ | ||
36 | (luaI_growvector((void**)old,n,sizeof(s),e,l)) | ||
37 | #define shrinkvector(v,n,t) ((t *)luaI_realloc(v,(n)*sizeof(t))) | ||
38 | |||
39 | #endif | ||
40 | |||