home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / EmMalloc / install.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  1.5 KB  |  67 lines

  1. #include <stdio.h>
  2.  
  3. #ifndef NOMALLOCCHECK
  4. #define MAXBLOCKS 16384
  5. typedef struct {
  6.     unsigned int     callerspc;
  7.     unsigned int      num;
  8.     unsigned int     mp;
  9. } block, *bp;
  10. block ht[MAXBLOCKS];
  11.  
  12. #define Hash(N) (((int)((N) >> 3)) % MAXBLOCKS)
  13.  
  14. #define installpanic(S) { printf( "%s\n", S); abort(); }
  15. #define MAGIC  0x99773311
  16. #define MAGIC2 0x11773399
  17. #endif
  18. unsigned int totalAllocated, totalFreed;
  19.  
  20. /*ARGSUSED*/
  21. install_alloc(callerspc, num, mp)
  22. unsigned int callerspc;
  23. unsigned int num;
  24. unsigned int mp;
  25. {
  26. #ifndef NOMALLOCCHECK
  27.     register int hv = Hash(mp);
  28.     register bp p = &ht[hv];
  29.     totalAllocated++;
  30.     while (p < &ht[MAXBLOCKS] && p->mp != 0) p++;
  31.     if (p >= &ht[MAXBLOCKS]) {
  32.     p = ht;
  33.     while (p < &ht[MAXBLOCKS] && p->mp != 0) p++;
  34.     if (p >= &ht[MAXBLOCKS]) installpanic("Too many allocated blocks.");
  35.     }
  36.     p->callerspc = callerspc;
  37.     p->num = num;
  38.     p->mp = mp;
  39. #endif
  40. }
  41.  
  42. /*ARGSUSED*/
  43. install_free(callerspc, mp)
  44. unsigned int callerspc, mp;
  45. {
  46. #ifndef NOMALLOCCHECK
  47.     register int hv = Hash(mp);
  48.     register bp p = &ht[hv];
  49.     totalFreed++;
  50.     while (p < &ht[MAXBLOCKS] && p->mp != mp) p++;
  51.     if (p >= &ht[MAXBLOCKS]) {
  52.     p = ht;
  53.     while (p < &ht[MAXBLOCKS] && p->mp != mp) p++;
  54.     if (p >= &ht[MAXBLOCKS]) installpanic("Free of non-allocated block.");
  55.     }
  56.     if (*(int *)(mp - 4) != MAGIC || *(int *)(mp + p->num) != MAGIC2) {
  57.       printf( "Free of corrupted block (0x%x) allocated at 0x%x\n",
  58.     p->mp, p->callerspc);
  59.       abort();
  60.     }
  61.     p->callerspc = 0;
  62.     p->num = 0;
  63.     p->mp = 0;
  64. #endif
  65. }
  66.  
  67.