home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- #ifndef NOMALLOCCHECK
- #define MAXBLOCKS 16384
- typedef struct {
- unsigned int callerspc;
- unsigned int num;
- unsigned int mp;
- } block, *bp;
- block ht[MAXBLOCKS];
-
- #define Hash(N) (((int)((N) >> 3)) % MAXBLOCKS)
-
- #define installpanic(S) { printf( "%s\n", S); abort(); }
- #define MAGIC 0x99773311
- #define MAGIC2 0x11773399
- #endif
- unsigned int totalAllocated, totalFreed;
-
- /*ARGSUSED*/
- install_alloc(callerspc, num, mp)
- unsigned int callerspc;
- unsigned int num;
- unsigned int mp;
- {
- #ifndef NOMALLOCCHECK
- register int hv = Hash(mp);
- register bp p = &ht[hv];
- totalAllocated++;
- while (p < &ht[MAXBLOCKS] && p->mp != 0) p++;
- if (p >= &ht[MAXBLOCKS]) {
- p = ht;
- while (p < &ht[MAXBLOCKS] && p->mp != 0) p++;
- if (p >= &ht[MAXBLOCKS]) installpanic("Too many allocated blocks.");
- }
- p->callerspc = callerspc;
- p->num = num;
- p->mp = mp;
- #endif
- }
-
- /*ARGSUSED*/
- install_free(callerspc, mp)
- unsigned int callerspc, mp;
- {
- #ifndef NOMALLOCCHECK
- register int hv = Hash(mp);
- register bp p = &ht[hv];
- totalFreed++;
- while (p < &ht[MAXBLOCKS] && p->mp != mp) p++;
- if (p >= &ht[MAXBLOCKS]) {
- p = ht;
- while (p < &ht[MAXBLOCKS] && p->mp != mp) p++;
- if (p >= &ht[MAXBLOCKS]) installpanic("Free of non-allocated block.");
- }
- if (*(int *)(mp - 4) != MAGIC || *(int *)(mp + p->num) != MAGIC2) {
- printf( "Free of corrupted block (0x%x) allocated at 0x%x\n",
- p->mp, p->callerspc);
- abort();
- }
- p->callerspc = 0;
- p->num = 0;
- p->mp = 0;
- #endif
- }
-
-