home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / apl / aq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-23  |  2.1 KB  |  106 lines

  1. /*    This is pretty crude.  Originally, the Purdue code did all
  2.  *    of the work, using sbrk() to get more memory from the system.
  3.  *    I just use malloc().  Someday it might be worthwhile to put
  4.  *    in the gc_ garbage collecting version and not worry about
  5.  *    releasing memory.  I've probably broken some of the i-beam
  6.  *    functions with this -- MEC
  7.  */
  8.  
  9. struct memblock {
  10.     char *block;
  11.     unsigned nbytes;
  12.     struct memblock *next;
  13. };
  14.  
  15. static struct memblock *firstblock = 0;
  16. extern char *malloc();
  17. extern int aftrace;
  18.  
  19.  
  20. alloc(nbytes)
  21. unsigned nbytes;
  22. {
  23.     struct memblock *newblock, *block;
  24.     unsigned count, sum;
  25.  
  26.     if (nbytes <= 0) return 0;
  27.     newblock = (struct memblock *)malloc(sizeof(struct memblock));
  28.     if (newblock == 0) goto failed;
  29.     newblock->nbytes = nbytes;
  30.     newblock->block = malloc(nbytes);
  31.     if (newblock->block == 0) goto failed;
  32.     if (aftrace) {
  33.         printf("[alloc: %d bytes at %d]\n", nbytes, newblock);
  34.     }
  35.     newblock->next = firstblock;
  36.     firstblock = newblock;
  37.     return (int)newblock->block;
  38. failed:
  39.     printf("unable to obtain requested memory\n");
  40.     printf("%d bytes were requested\n", nbytes);
  41.     count = 0;
  42.     sum = 0;
  43.     for (block=firstblock; block; block=block->next) {
  44.         count++;
  45.         sum += block->nbytes;
  46.     }
  47.     printf("%d bytes currently in %d blocks\n", sum, count);
  48.     abort();
  49. }
  50.  
  51.  
  52. aplfree(ap)
  53. char *ap;
  54. {
  55.     struct memblock *item, *last;
  56.  
  57.     if (ap == 0) return 0;
  58.     last = 0;
  59.     for (item=firstblock; item; item=item->next) {
  60.         if (item->block == ap) {
  61.             if (aftrace) {
  62.                 printf("[dealloc: %d bytes at %d\n", item->nbytes, item->block);
  63.             }
  64.             if (last) last->next = item->next;
  65.             else firstblock = item->next;
  66.             free(item->block);
  67.             free(item);
  68.             return;
  69.         }
  70.         last = item;
  71.     }
  72.     printf("aplfree bad block address %d\n", ap);
  73. }
  74.  
  75.  
  76. afreset()
  77. {
  78.     struct memblock *item;
  79.  
  80.     for (item=firstblock; item; item=item->next) {
  81.         free(item->block);
  82.         free(item);
  83.     }
  84.     firstblock = 0;
  85. }
  86.  
  87.  
  88. memoryCheck()
  89. {
  90.     struct memblock *item;
  91.     int count;
  92.     long sum;
  93.  
  94.     if (firstblock == 0) {
  95.         printf("no dynamic memory\n");
  96.         return;
  97.     }
  98.     count = 0;
  99.     sum = 0;
  100.     for (item=firstblock; item; item=item->next) {
  101.         count++;
  102.         sum += item->nbytes;
  103.     }
  104.     printf("%d bytes in %d blocks\n", sum, count);
  105. }
  106.