home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c220 / 4.ddi / LIB / SRC / C_HEAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-16  |  2.5 KB  |  72 lines

  1. #include <stdio.h>
  2. #include <implement.cf>
  3. pragma calling_convention(_REVERSE_PARMS|_CALLEE_POPS_STACK);
  4. pragma off(emit_names);
  5. #if 0
  6. *********************************************************************
  7. This is a "cheap" version of the heap manager.  It allocates space
  8. from a local static area.  It can be used for programs that use only
  9. standard input and standard output, because the heap requirements
  10. for those files are minimal.  Use of this "heap manager" will reduce
  11. memory requirements substantially by discarding the full-feature
  12. heap manager.
  13. This should NOT be used when full use of the heap is intended --
  14. the call to Free here only frees if the space freed is the most
  15. recently allocated area.
  16.  
  17. The C standard IO library needs 512 bytes if the file is to be
  18. buffered.  If the heap manager cannot allocate the 512 bytes the file
  19. remains unbuffered.
  20.  
  21. The malloc and free implemented here are actually the one in the underlying
  22. Pascal heap management system that the C library malloc and free calls.
  23. Thus we are NOT replacing the C malloc and free, but are replacing the
  24. Pascal malloc and free.  Note the Alias pragmas at the bottom that
  25. rename malloc and free to their "private" library names.
  26. *********************************************************************
  27. #endif
  28.  
  29. #define NULL ((void *)0)
  30. #define __HEAP_STORAGE (256)
  31. #define Byte_count unsigned int
  32.  
  33. static char Heap_area[__HEAP_STORAGE - 1];
  34. static Byte_count  Next_heap_byte = 0;
  35.  
  36. #define Len_size 2 /* (Sizeof(Byte_count)) */
  37.  
  38. /* This is here to print out the size of an allocated region to the console. */
  39. /* It's used for debugging, and commented out in the production version.     */
  40. #ifdef DEBUGGING
  41. static void PW(Byte_count Len) {
  42.    void P(Byte_count I) {
  43.       if (I > 0)  P(I/10);
  44.       putchar((I%10)+'0');
  45.       }
  46.    P(Len); putchar('\n');
  47.    }
  48. #endif
  49.  
  50. void *malloc(Byte_count Len) {
  51. #ifdef DEBUGGING
  52.    PW(Len);
  53. #endif
  54.    if (Next_heap_byte + Len + Len_size > __HEAP_STORAGE) return NULL;
  55.    /* Save the length.    */
  56.    *(Byte_count *)&Heap_area[Next_heap_byte] = Len;
  57.    Next_heap_byte += Len+Len_size;
  58.    return &Heap_area[Next_heap_byte-Len];
  59.    }
  60.  
  61. /* Frees the most recently allocated object only.             */
  62.  
  63. void free(Byte_count *P) {
  64.    Byte_count Len;
  65.    P -= Len_size;     /* Refer to length.      */
  66.    Len = *P;
  67.    if ((Byte_count *)&Heap_area[Next_heap_byte-Len] == P)
  68.       Next_heap_byte -= Len+Len_size;
  69.    }
  70. pragma Alias(malloc,_Private_routine_prefix "malloc");
  71. pragma Alias(free,  _Private_routine_prefix "free");
  72.