-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxm.c
373 lines (325 loc) · 10.9 KB
/
xm.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*-----------------------------------------------------------------------
xmem.c -- Extended Dynamic Memory Control Module.
Dr. Dobb's Journal, #154 August 1989
Modified by A.Boshkin, October 1990 (named idlxmem.c)
Modified by P.Dubner, September 1991 (named xm.c)
Included to InfoScope Turbo C Tool Box, September, 1991
(modified & named xm.c + xm.h)
Rebuild completely by P.Dubner, March 1992
---------------------------------------------------------------------- */
/* ************************ INCLUDE FILE ***************************** */
#include <mem.h>
#include <stdio.h>
#include <process.h>
#include "xm.h"
/* ************************ GLOBAL VARIABLES ************************** */
/* ˆá¯®«ì§®¢ ¨¥ xm_Trace:
(NO_HEAPCTL) => ¯à®áâë¥ ¢ë§®¢ë malloc & free
(FREE_CTL) => ®âá«¥¦¨¢ ¨¥ á ¨á¯®«ì§®¢ ¨¥¬ hash-â ¡«¨æë
(FULL_CTL) => â® ¦¥ + ª®âà®«ì § ¨§¬¥¥¨ï¬¨ ¢ à ¥¥ ®á¢®¡®¦¤¥ëå ¡«®ª å
*/
unsigned int xm_Trace = FULL_CTL; /* “¯à ¢«¥¨¥ ã஢¥¬ âà áá¨à®¢ª¨ */
unsigned long xm_userMemory = 0L; /* ޡ饥 ª®«-¢® ¯ ¬ïâ¨, ¢ë¤¥«¥®© */
/* ¯®«ì§®¢ ⥫î */
unsigned long xm_totalMemory = 0L; /* ‘㬬 ஥ ª®«-¢® § ï⮩ ¯ ¬ï⨠*/
/* (+¯ ¬ïâì ᮡáâ¢¥ë¥ ã¦¤ë) */
unsigned long xm_userAlloc = 0L; /* ’¥ªã饥 ª®«¨ç¥á⢮ § ¯à®á®¢ ¯ ¬ï⨠*/
unsigned long xm_totalAlloc = 0L; /* ޡ饥 ª®«¨ç¥á⢮ § ¯à®á®¢ ¯ ¬ï⨠*/
unsigned int xm_last_error_code;
void HeapWng (unsigned int code, void *ptr);
void (*xm_heapWng) (unsigned int code, void *ptr) = HeapWng;
char *xm_wngText[] = XM_WNGTEXT;
/* ************************ VARIABLES ************************** */
/* ’ ¡«¨æ ¤«ï ®âá«¥¦¨¢ ¨ï § ¯à®á®¢ ¯ ¬ï⨠*/
#define hashsize 47 /* §¬¥à hash-â ¡«¨æë */
#define bucketsize 10 /* Š®«-¢® í«¥¬¥â®¢ hash bucket */
/* amount of extra allocation for overhead*/
#define OVHSIZE 2
/* fill character for overhead gap*/
#define FILLCHAR '\xFF'
typedef struct alloc_entry
{ /* Allocated entry information */
size_t size; /* size of the allocated area */
char *ptr; /* pointer to the allocated area */
long alloc_no; /* allocation number of the area */
int chksum; /* checksum of a freed area or 0 */
int freed; /* 1 if freed, 0 otherwise */
}
ALLOCATION;
typedef struct bucket
{
struct bucket *next; /* Pointer to next bucket when filled */
int entries; /* number of used entries */
ALLOCATION *alloc; /* allocated entry array */
}
BUCKET;
#define NOT_FREED 256
#define HASH_INDEX(p) (int)((unsigned long)p % hashsize)
BUCKET **ptrHash = NULL; /* dynamic pointer hash table */
unsigned long hashVolume = 0L;
void HeapWng (unsigned int code, void *ptr)
{
xm_last_error_code = code;
fprintf (stderr, "\nWARNING: %s, addr: %p", xm_wngText[code], ptr);
}
/* =======================================================================
Compute checksum of a given area
*/
int checksum (char *p, size_t size)
{
int i;
unsigned char res = 0;
for (i = 0; i < size; i++)
res ^= *p++;
return (int)res;
}
/* =======================================================================
‡ ¯®¬¨âì 㪠§ â¥«ì ¢ hash-â ¡«¨æ¥
*/
int storePtr (void *p, /* pointer to be stored */
size_t b) /* size of area */
{
BUCKET *bp, *bq; /* bucket pointers */
int bno; /* bucket/entry number */
if (!ptrHash)
{ /* 㦠¯ ¬ïâì ¯®¤ ®á®¢ãî hash-â ¡«¨æã? */
if ((ptrHash =
(BUCKET **) calloc (hashsize, sizeof (BUCKET *))) == NULL)
return 0;
hashVolume = 0;
xm_totalMemory = hashsize * sizeof (BUCKET *);
xm_totalAlloc++;
}
bno = HASH_INDEX (p); /* ‚ëç¨á«¨¬ ¢å®¤ ¢ hash-â ¡«¨æã */
/* ©¤¥¬ ¯¥à¢ë© ¥ ¯®«®áâìî § ¯®«¥ë© bucket */
for (bq = bp = ptrHash[bno]; bp && bp->entries == bucketsize; bp = bp->next)
bq = bp;
/* ®á«¥ ¢ë室 ¨§ 横« ¬®¦¥â ®ª § âìáï, çâ®: */
/* (a) á í⨬ ¢å®¤®¬ ¥é¥ ¥ à ¡®â «¨ - */
/* ¢ í⮬ á«ãç ¥ bp == NULL ¨ bq == NULL; */
/* (b) á í⨬ ¢å®¤®¬ à ¡®â «¨ ¨ § ¯®«¨«¨ ¢á¥ bucket'ë - */
/* ¢ í⮬ á«ãç ¥ bp == NULL, ® bq != NULL. */
/* (c) ©¤¥ ¥¯®«®áâìî § ¯®«¥ë© bucket - */
/* ¢ í⮬ á«ãç ¥ bp != NULL ¨ bq != NULL. */
if (bp == NULL)
{ /* á«ãç © (a) ¨«¨ (b)? */
if ((bp = (BUCKET *) malloc (sizeof (BUCKET))) == NULL)
return 0;
hashVolume++;
xm_totalMemory += sizeof (BUCKET);
xm_totalAlloc++;
bp->alloc = (ALLOCATION *) calloc (bucketsize, sizeof (ALLOCATION));
if (bp->alloc == NULL)
return 0;
xm_totalMemory += bucketsize * sizeof (ALLOCATION);
xm_totalAlloc++;
bp->next = NULL;
bp->entries = 0;
if (bq == NULL) /* á¨âã æ¨ï (a): ®¢ë© bucket - ¢ £®«®¢ã ᯨ᪠*/
ptrHash[bno] = bp;
else /* á¨âã æ¨ï (b): ®¢ë© bucket - ¢ ª®¥æ ᯨ᪠*/
bq->next = bp;
}
/* ’¥¯¥àì § ¯®¬¨¬ 㪠§ â¥«ì ¨ ¯à®ç¥¥ */
bno = bp->entries++;
bp->alloc[bno].ptr = p;
bp->alloc[bno].size = b;
bp->alloc[bno].freed = NOT_FREED;
bp->alloc[bno].chksum = 0;
bp->alloc[bno].alloc_no = xm_totalAlloc;
return 1;
} /*storePtr */
int checkGap (char *p, int size)
{
int gap;
for (gap = size; gap < size + OVHSIZE; ++gap)
if (p[gap] != FILLCHAR)
return 0;
return 1;
} /*checkGap */
void freeMem (void *p, int size)
{
free (p);
xm_totalMemory -= size;
xm_totalAlloc--;
}
/**/
/* =======================================================================
“¤ «¥¨¥ ªã᪠¯ ¬ïâ¨, ¤à¥á㥬®£® 㪠§ ⥫¥¬ 'p'
¨§ hash â ¡«¨æë
======================================================================*/
int deletePtr (char *p)
{
BUCKET *bp, *bq; /* “ª § ⥫¨ ¤«ï bucket'®¢ */
int bno, i; /* ®¬¥à bucket' ¨ entry */
size_t size; /* §¬¥à ®á¢®¡®¦¤ ¥¬®© ¯ ¬ï⨠*/
if (!ptrHash)
return 0; /* “à ! ¥ç¥£® ¤¥« âì! */
bno = HASH_INDEX (p); /* ‚ëç¨á«¨¬ ¢å®¤ ¢ hash-â ¡«¨æã */
/* ன¤¥¬áï ¯® bucket' ¬ ¢ ¯®¨áª å 㪠§ ⥫ï 'p' */
for (bq = NULL, bp = ptrHash[bno]; bp; bp = bp->next)
{
for (i = 0; i < bp->entries; i++)
if (bp->alloc[i].ptr == p)
{ /* ’®, ç⮠㦮? */
size = bp->alloc[i].size;
/* ०¤¥ ¢á¥£® ¯à®¢¥à¨¬, çâ® gap-®¡« áâì … ¨§¬¥¨« áì, */
/* â.¥. ¯à®¢¥à¨¬, çâ® ¥ ¯¨á «¨ ‡€ ¢ë¤¥«¥ãî ¯ ¬ïâì. */
if (!checkGap (p, size))
xm_heapWng (OVERWRITE_WNG, p);
switch (xm_Trace)
{
case FULL_CTL:
if (bp->alloc[i].freed == NOT_FREED)
{
/* Š®â஫ì ï á㬬 : ¯®â®¬ ¡ã¤¥¬ ¯à®¢¥àïâì, */
/* ¥ ¯¨á «¨ «¨ ¢ ¡«®ª Ž‘‹… ®á¢®¡®¦¤¥¨ï */
bp->alloc[i].chksum = checksum (bp->alloc[i].ptr, size);
bp->alloc[i].freed = !NOT_FREED;
}
else
xm_heapWng (FREEING_FREE_WNG, p);
break;
case FREE_CTL:
freeMem (p, size + OVHSIZE);
if (--bp->entries == 0)
{
if (bq) /* ®¯à ¢¨¬ ᯨ᮪ bucket'®¢ */
bq->next = bp->next;
else
ptrHash[bno] = bp->next;
freeMem (bp->alloc, bucketsize * sizeof (ALLOCATION));
freeMem (bp, sizeof (BUCKET));
hashVolume--;
} /*if(--bp->entries == 0) */
else if (i < bp->entries)
bp->alloc[i] = bp->alloc[bp->entries];
break;
default: /* ¤¥îáì, ˆŠŽƒ„€ ¥ ¯® ¤®¡¨âáï */
fprintf (stderr, "\n\n\nŽè¨¡ª ¯à¨ à ¡®â¥ ¬®¤ã«ï XM !!!\n\n");
abort ();
}
return size; /* ®à¬ «ì®¥ § ¢¥à襨¥ à ¡®âë */
} /* if ... == p */
bq = bp;
} /* for bq = ... */
if (bp == NULL)
xm_heapWng (FREEING_BAD_WNG, p);
return 0;
} /*deletePtr */
/* =======================================================================
‚뤥«¨âì 'b' ¡ ©â®¢ ¯ ¬ïâ¨
*/
void *xm_Xmalloc (size_t b)
{
char *mptr;
if (xm_Trace != NO_HEAPCTL)
{
/* „®¡ ¢¨¬ ªãá®ç¥ª ¤«ï ¤ «ì¥©è¥£® ª®â஫ï */
if ((mptr = malloc (b + OVHSIZE)) != NULL)
{
memset (mptr + b, FILLCHAR, OVHSIZE); /* ‡ ¯®«¨¬ ¤®¡ ¢ªã */
if (!storePtr (mptr, b))
{ /* 'mptr' - ¢ ptrHash */
free (mptr);
return NULL;
}
xm_userAlloc++;
xm_totalAlloc++;
xm_userMemory += b;
xm_totalMemory += b + OVHSIZE;
}
}
else
mptr = malloc (b);
return mptr;
} /*xm_Xmalloc */
/* =======================================================================
Allocate and clear i*s bytes of memory
*/
void *xm_Xcalloc (unsigned int i, /* 㦮¥ ª®«¨ç¥á⢮ ¡«®ª®¢ */
unsigned int s) /* §¬¥à ¡«®ª ¢ ¡ ©â å */
{
register unsigned int amt;
register char *mptr;
if ((mptr = xm_Xmalloc (amt = i * s)) != NULL)
memset (mptr, '\0', amt); /* clear requested space */
return mptr;
} /*xm_Xcalloc */
/* =======================================================================
Free allocated memory
*/
void xm_Xfree (void *p) /* 'p' points to block to be freed */
{
if (p == NULL)
xm_heapWng (FREEING_NULL_WNG, p);
else if (xm_Trace != NO_HEAPCTL)
{
int size = deletePtr ((char *)p);
if (size)
{
xm_userMemory -= size; /* ‘ª®à४â¨à㥬 áç¥â稪¨ */
xm_userAlloc--;
}
}
else
free (p);
} /*xm_Xfree */
/* =======================================================================
Check to ensure all blocks have been freed;
free all the memory used if freeing is 1.
*/
int xm_XMem (int freeing, unsigned long start)
{
register int bno, i; /* bucket/entry number */
register BUCKET *bp, *bq; /* bucket pointers */
int l;
xm_last_error_code = 0;
if (ptrHash == NULL || xm_Trace == NO_HEAPCTL)
return 1; /* “à ! ¥ç¥£® ¤¥« âì! */
for (bq = NULL, bno = 0; bno < hashsize; ++bno)
{
for (bp = ptrHash[bno]; bp; bp = bp ? bp->next : NULL)
for (l = bp->entries, i = 0; i < l; i++)
if (bp->alloc[i].alloc_no > start)
{
if (!checkGap (bp->alloc[i].ptr, bp->alloc[i].size))
xm_heapWng (OVERWRITE_WNG, bp->alloc[i].ptr);
if (bp->alloc[i].freed == NOT_FREED)
xm_heapWng (UNFREED_WNG, bp->alloc[i].ptr);
if (freeing)
{
freeMem (bp->alloc[i].ptr, bp->alloc[i].size + OVHSIZE);
if (bp->alloc[i].freed == NOT_FREED)
{
xm_userAlloc--;
xm_userMemory -= bp->alloc[i].size;
}
l--;
if (--bp->entries == 0)
{
if (bq) /* ®¯à ¢¨¬ ᯨ᮪ bucket'®¢ */
bq->next = bp->next;
else
ptrHash[bno] = bp->next;
freeMem (bp->alloc, bucketsize * sizeof (ALLOCATION));
freeMem (bp, sizeof (BUCKET));
hashVolume--;
bp = NULL;
} /*if(--bp->entries == 0) */
else if (i < bp->entries)
bp->alloc[i] = bp->alloc[bp->entries];
break;
}
}
bq = bp;
}
if (hashVolume == 0)
{
free (ptrHash);
ptrHash = NULL;
xm_totalAlloc--;
xm_totalMemory -= hashsize * sizeof (BUCKET *);
}
return xm_last_error_code == 0;
} /*xm_XMem */