forked from adesutherland/CMS-370-BREXX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmem.c
287 lines (242 loc) · 7.41 KB
/
bmem.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
282
283
284
285
286
287
/*
* $Id: bmem.c,v 1.10 2009/06/02 09:41:27 bnv Exp $
* $Log: bmem.c,v $
* Revision 1.10 2009/06/02 09:41:27 bnv
* MVS/CMS corrections
*
* Revision 1.9 2008/07/15 07:40:25 bnv
* #include changed from <> to ""
*
* Revision 1.8 2008/07/14 13:08:42 bnv
* MVS,CMS support
*
* Revision 1.7 2004/08/16 15:27:58 bnv
* Error message if the WCE version is compiled with bmem
*
* Revision 1.6 2003/10/30 13:15:29 bnv
* Create a coredump in case of error
*
* Revision 1.5 2002/06/11 12:37:38 bnv
* Added: CDECL
*
* Revision 1.4 2001/06/25 18:50:28 bnv
* Changed: Some minor changes at mem_chk and mem_list, mem_print
*
* Revision 1.3 1999/11/26 14:30:27 bnv
* Added: A dummy int, at Memory structure for 8-byte alignment on 32-bit machines
*
* Revision 1.2 1999/11/26 13:13:47 bnv
* Changed: Added MAGIC number also to the end.
* Chanted: Modified to work with 64-bit computers.
*
* Revision 1.1 1998/07/02 17:34:50 bnv
* Initial revision
*
*/
#define __BMEM_C__
#ifdef WCE
# error "bmem.c: should not be included in the CE version"
#endif
#include <ctype.h>
#include <stdio.h>
#if !defined(__CMS__) && !defined(__MVS__)
# include <malloc.h>
#endif
#include <stdlib.h>
#include <cmssys.h>
#include "os.h"
#include "bmem.h"
#include "ldefs.h"
#include "signal.h"
#define MAGIC 0xDECAFFEE
/*
* This file provides some debugging functions for memory allocation
*/
typedef struct tmemory_st {
dword magic;
char *desc;
size_t size;
struct tmemory_st *next;
struct tmemory_st *prev;
#if defined(ALIGN) && !defined(__ALPHA)
/* Some machines have problems ithe address is not at 8-bytes aligned */
int dummy;
#endif
byte data[sizeof(void *)];
} Memory;
#include "context.h"
void mem_list(void);
/* -------------- mem_malloc ---------------- */
void *
mem_malloc(size_t size, char *desc) {
Memory *mem;
Context *context = (Context *) CMSGetPG();
/* add space for the header */
#if defined(__BORLANDC__) && (defined(__HUGE__) || defined(__LARGE__))
mem = (Memory *)farmalloc(sizeof(Memory)+size);
#else
mem = (Memory *) malloc(sizeof(Memory) + size);
#endif
if (mem) {
/* Create the memory header */
mem->magic = MAGIC;
mem->desc = desc;
mem->size = size;
mem->next = NULL;
mem->prev = ((Memory *) (context->bmem_mem_head));
if (((Memory *) (context->bmem_mem_head)))
((Memory *) (context->bmem_mem_head))->next = mem;
(context->bmem_mem_head) = mem;
(context->bmem_total_mem) += size;
/* Mark also the END of data */
*(dword *) ((byte *) mem->data + mem->size) = MAGIC;
return (void *) (mem->data);
} else {
fprintf(STDERR, "Not enough memory to allocate object %s size=%d\n",
desc, size);
return NULL;
}
} /* mem_malloc */
/* -------------- mem_realloc ---------------- */
void *
mem_realloc(void *ptr, size_t size) {
Memory *mem, *other;
int head;
Context *context = (Context *) CMSGetPG();
/* find our header */
mem = (Memory *) ((char *) ptr - (sizeof(Memory) - sizeof(void *)));
/* check if the memory is valid */
if (mem->magic != MAGIC) {
fprintf(STDERR,
"mem_realloc: PREFIX Magic number doesn't match of object %p!\n",
ptr);
mem_list();
raise(SIGSEGV);
}
if (*(dword *) (mem->data + mem->size) != MAGIC) {
fprintf(STDERR,
"mem_realloc: SUFFIX Magic number doesn't match of object %p!\n",
ptr);
mem_list();
raise(SIGSEGV);
}
(context->bmem_total_mem) -= mem->size;
head = (mem == ((Memory *) (context->bmem_mem_head)));
#if defined(__BORLANDC__) && (defined(__HUGE__) || defined(__LARGE__))
mem = (Memory *)farrealloc(mem,size+sizeof(Memory));
#else
mem = (Memory *) realloc(mem, size + sizeof(Memory));
#endif
if (mem == NULL) {
fprintf(STDERR, "Not enough memory to allocate object %s size=%d\n",
mem->desc, size);
return NULL;
}
if (head) ((Memory *) (context->bmem_mem_head)) = mem;
mem->size = size;
(context->bmem_total_mem) += size;
other = mem->prev;
if (other) other->next = mem;
other = mem->next;
if (other) other->prev = mem;
/* Mark also the new END of data */
*(dword *) (mem->data + mem->size) = MAGIC;
return (void *) (mem->data);
} /* mem_realloc */
/* -------------- mem_free ---------------- */
void __CDECL
mem_free(void *ptr) {
Memory *mem, *mem_prev, *mem_next;
int head;
Context *context = (Context *) CMSGetPG();
/* find our header */
mem = (Memory *) ((char *) ptr - (sizeof(Memory) - sizeof(void *)));
if (mem->magic != MAGIC) {
fprintf(STDERR,
"mem_free: PREFIX Magic number doesn't match of object %p!\n",
ptr);
mem_list();
raise(SIGSEGV);
}
if (*(dword *) (mem->data + mem->size) != MAGIC) {
fprintf(STDERR, "mem_free: SUFFIX Magic number doesn't match!\n");
mem_list();
raise(SIGSEGV);
}
/* Remove the MAGIC number, just to catch invalid entries */
mem->magic = 0L;
mem_prev = mem->prev;
mem_next = mem->next;
(context->bmem_total_mem) -= mem->size;
head = (mem == ((Memory *) (context->bmem_mem_head)));
#if defined(__BORLANDC__) && (defined(__HUGE__) || defined(__LARGE__))
farfree(mem);
#else
free(mem);
#endif
if (mem_next) mem_next->prev = mem_prev;
if (mem_prev) mem_prev->next = mem_next;
if (head) ((Memory *) (context->bmem_mem_head)) = mem_prev;
} /* mem_free */
/* -------------- mem_print --------------- */
static void
mem_print(int count, Memory *mem) {
int i;
fputs((mem->magic == MAGIC) ? " " : "??", STDERR);
fprintf(STDERR, "%3d %5d %p %s\t\"",
count, mem->size, mem->data, mem->desc);
for (i = 0; i < 10; i++)
fprintf(STDERR, "%c",
isprint(mem->data[i]) ? mem->data[i] : '.');
fprintf(STDERR, "\" ");
for (i = 0; i < 10; i++)
fprintf(STDERR, "%02X ", mem->data[i]);
fprintf(STDERR, "\n");
} /* mem_print */
/* -------------- mem_list ---------------- */
void __CDECL
mem_list(void) {
Memory *mem;
int y, count;
Context *context = (Context *) CMSGetPG();
mem = ((Memory *) (context->bmem_mem_head));
count = 0;
fprintf(STDERR, "\nMemory list:\n");
y = 0;
while (mem) {
mem_print(count++, mem);
mem = mem->prev;
if (++y == 15) {
if (getchar() == 'q') exit(0);
y = 0;
}
}
fprintf(STDERR, "\n");
} /* mem_list */
/* --------------- mem_chk ------------------- */
int __CDECL
mem_chk(void) {
Memory *mem;
int i = 0;
Context *context = (Context *) CMSGetPG();
for (mem = ((Memory *) (context->bmem_mem_head)); mem;
mem = mem->prev, i++) {
if (mem->magic != MAGIC) {
fprintf(STDERR, "PREFIX Magic number doesn't match! ID=%d\n", i);
mem_print(i, mem);
mem_list();
}
if (*(dword *) (mem->data + mem->size) != MAGIC) {
fprintf(STDERR, "SUFFIX Magic number doesn't match! ID=%d\n", i);
mem_print(i, mem);
mem_list();
}
}
return 0;
} /* mem_chk */
/* -------------- mem_allocated ----------------- */
long __CDECL
mem_allocated(void) {
Context *context = (Context *) CMSGetPG();
return (context->bmem_total_mem);
} /* mem_allocated */