home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / Q2 < prev    next >
Encoding:
Text File  |  1992-12-09  |  2.6 KB  |  74 lines

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