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

  1. /*
  2.  * What we want to do is keep track of who calls calloc, malloc, and free,
  3.  * and what size blocks they request.
  4.  * Turn off all this expensive checking by defining NOMALLOCCHECK.
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. char *__malloc();
  10.  
  11. #define MAGIC 0x99773311
  12. #define MAGIC2 0x11773399
  13. /* define MINALLOC 0 */ /* MINALLOC is the minimum number of bytes
  14.             that may be allocated.
  15.             Some dumb Yellow Page routine on june.cs.washington.edu
  16.             allocates 0 bytes for some unknown reason
  17.             so I had to disable MINALLOC.  Eric Jul, Nov. 1987 */
  18.  
  19. int vMallocLimit = 2000000;
  20.  
  21. /*VARARGS*/
  22. char *malloc(num)
  23. int num;
  24. {
  25.     register char *callerspc;        /* r11 on vax, a5 on sun */
  26.     register char *mp;
  27.     
  28. #ifdef NOMALLOCCHECK
  29.     return(__malloc(num));
  30. #else NOMALLOCCHECK
  31.  
  32. #ifdef vax
  33.     asm("    movl 16(fp), r11");
  34. #endif
  35. #ifdef sun
  36.     asm("    movl a6@(4), a5");
  37. #endif
  38.  
  39. #ifdef MINALLOC
  40.     if (num > vMallocLimit || num < MINALLOC) {
  41.     printf( "malloc(%d) arg too small or above limit of %d\n",
  42.         num, vMallocLimit);
  43.     printf( "(vMallocLimit is tunable.)\n");
  44.     abort();
  45.     }
  46. #else MINALLOC
  47.     if (num > vMallocLimit) {
  48.     printf( "malloc request %d bytes, limit is %d bytes\n",
  49.         num, vMallocLimit);
  50.     printf( "(vMallocLimit is tunable.)\n");
  51.     abort();
  52.     }
  53. #endif MINALLOC
  54.     mp = __malloc (num + 2*sizeof(int));
  55. #ifdef lint
  56.     callerspc = mp;
  57. #endif
  58.     install_alloc(callerspc, num, mp+sizeof(int));
  59.     *(int *)mp = MAGIC;
  60.     *(int *)(mp+num+sizeof(int)) = MAGIC2;
  61.     return(mp+sizeof(int));
  62. #endif NOMALLOCCHECK
  63.  
  64. }
  65.  
  66. #define CHARPERINT (sizeof(int)/sizeof(char))
  67. #define NULL 0
  68.  
  69. char *calloc(num, size)
  70. unsigned int num, size;
  71. {
  72.     register char *callerspc;        /* r11 on vax, a5 on sun */
  73.     register char *mp;            /* r10 */
  74.     register int length;        /* r 9 */
  75.     register char *fp;            /* r 8 */
  76.  
  77. #ifdef NOMALLOCCHECK
  78.     fp = __malloc(length);
  79. #ifdef vax
  80.     while (length > 32768) {
  81.       asm("    movc5    $0, (r0), $0, $32768, (r8)");
  82.       length -= 32768;
  83.       fp += 32768;
  84.     }
  85.     asm("    movc5    $0, (r0), $0, r9, (r8)");
  86. #endif
  87. #ifdef sun
  88.     bzero(fp, length);
  89. #endif
  90.     return (fp);
  91. #else NOMALLOCCHECK
  92.  
  93. #ifdef vax
  94.     asm("    movl 16(fp), r11");
  95. #endif
  96. #ifdef sun
  97.     asm("    movl a6@(4), a5");
  98. #endif
  99.     length = num;
  100.     length *= size;
  101.     mp = __malloc(length+8);
  102. #ifdef lint
  103.     callerspc = mp;
  104. #endif lint
  105.     install_alloc(callerspc, length, mp+4);
  106.     if (mp == NULL) return(NULL);
  107.     fp = mp + sizeof(int);
  108. #ifdef lint
  109.     fp = fp;
  110. #endif
  111. #ifdef vax
  112.     while (length > 32768) {
  113.       asm("    movc5    $0, (r0), $0, $32768, (r8)");
  114.       length -= 32768;
  115.       fp += 32768;
  116.     }
  117.     asm("    movc5    $0, (r0), $0, r9, (r8)");
  118. #endif
  119. #ifdef sun
  120.     bzero(fp, length);
  121. #endif
  122.     *(int *)mp = MAGIC;
  123.     *(int *)(mp+length+4) = MAGIC2;
  124.     return(mp+4);
  125. #endif NOMALLOCCHECK
  126. }
  127.  
  128. void free(mp)
  129. char *mp;
  130. {
  131.     register char *callerspc;        /* r11 on vax, a5 on sun */
  132. #ifdef NOMALLOCCHECK
  133.     __free(mp);
  134.     return;
  135. #else NOMALLOCCHECK
  136.  
  137. #ifdef vax
  138.     asm("    movl 16(fp), r11");
  139. #endif
  140. #ifdef sun
  141.     asm("    movl a6@(4), a5");
  142. #endif
  143. #ifdef lint
  144.     callerspc = mp;
  145. #endif lint
  146.     install_free(callerspc, (unsigned int) mp);
  147.     __free(mp - 4);
  148. #endif NOMALLOCCHECK
  149. }
  150.  
  151. char *cfree()
  152. {
  153.   abort();
  154. }
  155.  
  156. char *realloc()
  157. {
  158.   abort();
  159. }
  160.