/* ALMLIB.C for CC8, by Mark Miller 5/27/91. */#include "STDIO.H"#define MEMSTART 1536#define MEMSIZE 256/* num of bytes for alloc info. */#define INFOLEN 2#define FREE_SPC 1#define USED_SPC 2/*  Does housekeeping on memory space.  If there are consecutive memory  spaces that are freed, it will consolidate these spaces into one free space.*/update()$(  /* ptr to alloc info.; ptr to first free space found */  char *loc, *tempaddr;  /* size of a space; accum for size of consecutive spaces */  int size, templen;  /* flag for first non-free space found (after free space(s) found) */  int done;  loc = MEMSTART;  templen = 0;  /* search through memory space until end, for free space to consolidate */  while(loc < MEMSTART+MEMSIZE)  $(    if(*loc == FREE_SPC)    $(      /* if space is free, search for consecutive free spaces */      size = *(loc+1);      /* hold first free-space pos */      tempaddr = loc;      /* get size of this space */      templen = size;      /* get to next info. bytes */      loc += size+INFOLEN;      done = 0;      while((loc < MEMSTART+MEMSIZE) && !done)      $(        /* if next space is free, add its free space to accum */        if(*loc == FREE_SPC)        $(          size = *(loc+1);          templen += size+INFOLEN;        $) /* if */        else          /* otherwise, done with present search */          done = 1;        /* go to next info. bytes */        loc += size+INFOLEN;      $) /* while */      /* if more than one free space was found, record this free space */      /* at first free-space pos */      if(templen != size)        *(tempaddr+1) = templen;      templen = 0;    $) /* if */    else      /* if space is not free, go to next info. bytes (next space) */      loc += size+INFOLEN;  $) /* while */$) /* update *//*  Finds a free space for something of specified size (data type does not  matter).  If no free space can be found with specified size, malloc  will return NULL.  Currently malloc only works in the 256 byte space of Page 6 (location  1536 decimal).*/char *malloc(size)  int size;$(  /* ptr to info. bytes; ptr to allocated space */  char *loc, *bloc;  /* amt of space free/used */  int space;  /* flag for when free space found and allocated; flag for when update() */  /* is called */  int done, updated;  loc = MEMSTART;  /* if memory area is completely unused, put in info. bytes, set to */  /* free space */  if(*loc == 0)  $(    *loc = FREE_SPC;    *(loc+1) = MEMSIZE-2;  $) /* if */  bloc = NULL;  updated = 0;  done = 0;  while(!done)  $(    /* search for free space of specified size (or more) */    while((loc < MEMSTART+MEMSIZE) && !done)    $(      space = *(loc+1);      /* if space is free, allocate it and return ptr to it.  Also put */      /* modified info. bytes after allocated space */      if((*loc == FREE_SPC) && ((space == size) || (space >= size+INFOLEN)))      $(        *loc = USED_SPC;        *(loc+1) = size;        if(space >= size+INFOLEN)        $(          *(loc+INFOLEN+size) = FREE_SPC;          *(loc+INFOLEN+1+size) = space-size-INFOLEN;        $) /* if */        bloc = loc+INFOLEN;        done = 1;      $) /* if */      else        /* otherwise, go to next info. bytes */        loc += space+INFOLEN;    $) /* while */    if(!updated && !done)    $(      update();      updated = 1;      loc = MEMSTART;    $)    else      done = 1;  $) /* while */  return bloc;$) /* malloc *//*  Deallocates space pointed to by addr.*/free(addr)  char *addr;$(  int size;  if((addr >= MEMSTART) && (addr < MEMSTART+MEMSIZE))    *(addr-2) = FREE_SPC;$) /* free *//*  Allocation Memory Reset:  Sets all bytes in memory space to 0, just in case any data is there.*/almreset()$(  clear(MEMSTART-1, MEMSIZE+1);$)/*  Allocation Memory Dump:  This function is provided for program debugging purposes.  It displays, in  decimal form, what is in the memory area used for dynamic memory  allocation by malloc().*/almdump()$(  char *addr;  printf("Allocation memory dump from location\n");  printf("%d to %d, in bytes:\n", MEMSTART, MEMSTART + MEMSIZE);  for(addr = MEMSTART; addr <= MEMSTART+MEMSIZE; addr++)    printf("%d ", *addr);  printf("\nPress RETURN to continue");  getchar();$) /* almdump */