home *** CD-ROM | disk | FTP | other *** search
- /* (C) Copyright 1986-1992 MetaWare Incorporated; Santa Cruz, CA 95060. */
-
- #include <stdio.h>
- #include <implement.cf>
- pragma calling_convention(_REVERSE_PARMS|_CALLEE_POPS_STACK);
- pragma off(emit_names);
- #if 0
- *********************************************************************
- This is a "cheap" version of the heap manager. It allocates space
- from a local static area. It can be used for programs that use only
- standard input and standard output, because the heap requirements
- for those files are minimal. Use of this "heap manager" will reduce
- memory requirements substantially by discarding the full-feature
- heap manager.
- This should NOT be used when full use of the heap is intended --
- the call to Free here only frees if the space freed is the most
- recently allocated area.
-
- The C standard IO library needs 512 bytes if the file is to be
- buffered. If the heap manager cannot allocate the 512 bytes the file
- remains unbuffered.
-
- The malloc and free implemented here are actually the one in the underlying
- Pascal heap management system that the C library malloc and free calls.
- Thus we are NOT replacing the C malloc and free, but are replacing the
- Pascal malloc and free. Note the Alias pragmas at the bottom that
- rename malloc and free to their "private" library names.
- *********************************************************************
- #endif
-
- #define NULL ((void *)0)
- #define __HEAP_STORAGE (256)
- #define Byte_count unsigned int
-
- static char Heap_area[__HEAP_STORAGE - 1];
- static Byte_count Next_heap_byte = 0;
-
- #define Len_size 2 /* (Sizeof(Byte_count)) */
-
- /* This is here to print out the size of an allocated region to the console. */
- /* It's used for debugging, and commented out in the production version. */
- #ifdef DEBUGGING
- static void PW(Byte_count Len) {
- void P(Byte_count I) {
- if (I > 0) P(I/10);
- putchar((I%10)+'0');
- }
- P(Len); putchar('\n');
- }
- #endif
-
- void *malloc(Byte_count Len) {
- #ifdef DEBUGGING
- PW(Len);
- #endif
- if (Next_heap_byte + Len + Len_size > __HEAP_STORAGE) return NULL;
- /* Save the length. */
- *(Byte_count *)&Heap_area[Next_heap_byte] = Len;
- Next_heap_byte += Len+Len_size;
- return &Heap_area[Next_heap_byte-Len];
- }
-
- /* Frees the most recently allocated object only. */
-
- void free(Byte_count *P) {
- Byte_count Len;
- P -= Len_size; /* Refer to length. */
- Len = *P;
- if ((Byte_count *)&Heap_area[Next_heap_byte-Len] == P)
- Next_heap_byte -= Len+Len_size;
- }
- pragma Alias(malloc,_Private_routine_prefix "malloc");
- pragma Alias(free, _Private_routine_prefix "free");
-