-
Notifications
You must be signed in to change notification settings - Fork 1
/
memmgt.c
281 lines (262 loc) · 6.55 KB
/
memmgt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
memmgt.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "token.h"
#include <stdlib.h>
#include <string.h>
long total_mem_used;
long peak_mem_used;
long misc_pool_used;
typedef struct hdr
{
unsigned long size;
int line;
char *file;
#if defined(DEBUG_MALLOC)
void *caller;
struct hdr *next, *prev;
#endif
unsigned long magic;
} Hdr;
#define PRE_MAGIC 0x12345678
#define NOTALLOCED 0x00010000
#define POST_MAGIC 0x87654321
#define NFG ((char *)0)
#if defined(DEBUG_MALLOC)
static Hdr *top, *bottom;
#endif
static char* check(Hdr *hdr)
{
char *msg = 0;
unsigned long *end;
if ( hdr->magic != PRE_MAGIC )
{
if ( hdr->magic == (PRE_MAGIC | NOTALLOCED) )
{
msg = "%%%s-F-FATAL, %s:%d tried to free %08lX already free'd.\n";
}
else
{
msg = "%%%s-F-FATAL, %s:%d tried to free %08lX with corrupted header.\n";
}
}
else
{
end = (unsigned long *)((char *)(hdr + 1) + hdr->size);
if ( *end != POST_MAGIC )
{
msg = "%%%s-F-FATAL, %s:%d tried to free %08lX with corrupted tail.\n";
}
}
return msg;
}
#if defined(DEBUG_MALLOC)
static void check_all(void)
{
Hdr *h;
char *msg;
h = top;
while ( h )
{
msg = check(h);
if ( msg )
{
fprintf(stderr, msg, macxx_name, h->file, h->line, h + 1);
abort();
}
h = h->next;
if ( h == top )
break;
}
}
#endif
int mem_free(void *s, char *file, int line)
{
Hdr *hdr;
char *msg = NULL;
if ( s == 0 )
{
msg = "%s:%d tried to free %08lX.\n";
}
else
{
hdr = (Hdr *)s - 1;
msg = check(hdr);
}
if ( msg )
{
fprintf(stderr, msg, macxx_name, file, line, s);
abort();
}
#if defined(LLF) || defined(MACXX)
total_mem_used -= hdr->size + sizeof(Hdr) + sizeof(long);
#endif
#if defined(DEBUG_MALLOC)
check_all();
if ( hdr == top )
{
top = hdr->next;
}
if ( hdr == bottom )
{
bottom = hdr->prev;
}
if ( hdr->next )
hdr->next->prev = 0;
if ( hdr->prev )
hdr->prev->next = 0;
hdr->next = hdr->prev = 0;
#endif
free((char *)hdr);
return (0); /* assume it worked */
}
void *mem_alloc(int nbytes, char *file, int line)
{
void *s;
Hdr *hdr;
unsigned long *end;
int siz;
/* Round the caller's count by size of pointer then make it a multiple of sizeof pointer */
nbytes = (nbytes + (sizeof(char *) - 1)) & ~(sizeof(char *) - 1);
siz = nbytes + sizeof(Hdr) + sizeof(char *);
hdr = (Hdr *)calloc((unsigned int)siz, (unsigned int)1); /* get some memory from OS */
if ( hdr == (Hdr *)0 )
{
fprintf(stderr, "%%%s-F-FATAL, %s:%d Ran out of memory requesting %d bytes. Used %ld so far.\n",
macxx_name, file, line, siz, total_mem_used);
#if defined(LLF) || defined(MACXX)
display_mem();
#endif
fprintf(stderr, "%s", emsg);
abort();
}
hdr->size = nbytes; /* Rounded up size of region */
hdr->file = file; /* pointer to file name */
hdr->line = line; /* line number */
hdr->magic = PRE_MAGIC; /* surround user's buffer with known information */
s = (void *)(hdr + 1); /* Point to buffer to hand back to user */
end = (unsigned long *)((char *)s + nbytes); /* Get pointer to end of buffer */
*end = POST_MAGIC; /* Stuff a magic number there too */
#if defined(DEBUG_MALLOC)
if ( top == 0 )
{
top = hdr;
}
hdr->prev = bottom;
if ( bottom )
bottom->next = hdr;
bottom = hdr;
hdr->next = 0;
check_all();
#endif
#if defined(LLF) || defined(MACXX)
total_mem_used += siz;
if ( total_mem_used > peak_mem_used )
peak_mem_used = total_mem_used;
#endif
return s;
}
void *mem_calloc(int nbytes, char *file, int line)
{
void *retV = mem_alloc(nbytes, file, line);
if ( retV )
memset(retV, 0, nbytes);
return retV;
}
void* mem_realloc(void *old, int nbytes, char *file, int line)
{
void *s;
Hdr *hdr;
#if defined(DEBUG_MALLOC)
Hdr * prev,*next;
#endif
int siz;
unsigned long *end;
if ( old != 0 )
{
hdr = (Hdr *)old - 1;
siz = hdr->size;
if ( hdr->magic != PRE_MAGIC )
{
fprintf(stderr, "%%%s-F-FATAL, %s:%d realloc'd %08lX with corrupted header.\n",
macxx_name, file, line, (unsigned long)old);
abort();
}
#if defined(LLF) || defined(MACXX)
total_mem_used -= siz + sizeof(Hdr) + sizeof(long);
#endif
end = (unsigned long *)((char *)old + hdr->size);
if ( *end != POST_MAGIC )
{
fprintf(stderr, "%%%s-F-FATAL, %s:%d realloc'd %08lX with corrupted trailer.\n",
macxx_name, file, line, (unsigned long)old);
if ( hdr->line > 0 )
fprintf(stderr, " area allocated by %s:%d\n", hdr->file, hdr->line);
abort();
}
#if defined(DEBUG_MALLOC)
check_all();
next = hdr->next;
prev = hdr->prev;
#endif
*end = 0;
/* Round up user's size and make it a multiple of sizeof pointer */
nbytes = (nbytes + (sizeof(char *) - 1)) & ~(sizeof(char *) - 1);
siz = nbytes + sizeof(Hdr) + sizeof(long);
s = (void *)realloc(hdr, siz);
if ( s == NFG )
{
fprintf(stderr, "%%%s-F-FATAL, %s:%d ran out of memory realloc'ing %ld bytes to %d. Used %ld so far.\n",
macxx_name, file, line, hdr->size, siz, total_mem_used);
#if defined(LLF) || defined(MACXX)
display_mem();
#endif
fprintf(stderr, "%s", emsg);
abort();
}
hdr = (Hdr *)s;
hdr->file = file; /* Pointer to filename */
hdr->line = line; /* line number */
hdr->magic = PRE_MAGIC; /* Marker */
if ( nbytes > hdr->size )
{ /* 0 the newly alloc'd area if it got bigger */
s = (char *)(hdr + 1) + hdr->size;
memset(s, 0, nbytes - hdr->size);
}
s = (void *)(hdr + 1); /* Compute pointer to user's buffer */
end = (unsigned long *)((char *)s + nbytes); /* Compute pointer to end of buffer */
*end = POST_MAGIC; /* deposit a marker */
hdr->size = nbytes; /* record the new buffer size */
#if defined(DEBUG_MALLOC)
hdr->next = next;
hdr->prev = prev;
if ( next )
next->prev = hdr;
if ( prev )
prev->next = hdr;
if ( top == prev )
top = hdr;
#endif
#if defined(LLF) || defined(MACXX)
total_mem_used += siz;
if ( total_mem_used > peak_mem_used )
peak_mem_used = total_mem_used;
#endif
return s;
}
else
{
return mem_alloc(nbytes, file, line);
}
}