home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 400-499 / ff472.lzh / ICalc / src / memory.c < prev    next >
C/C++ Source or Header  |  1991-04-17  |  1KB  |  75 lines

  1. /*
  2. *    memory routines to ease garbage collection when errors occur
  3. *    with parsing.
  4. *
  5. *    MWS, March 20, 1991.
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include "memory.h"
  10.  
  11.  
  12. static RemKey **gkey;
  13.  
  14. void *emalloc(n)        /* check return from malloc() */
  15.     unsigned n;
  16. {
  17.     extern void execerror(char *, char *);
  18.     char *p;
  19.  
  20.     if ((p = malloc(n)) == NULL)
  21.         execerror("out of memory", NULL);
  22.  
  23.     return p;
  24. }
  25.  
  26. RemKey **rem_setkey(k)        /* set key to be used for malloc's */
  27.     RemKey **k;
  28. {
  29.     RemKey **oldkey = gkey;
  30.     gkey = k;
  31.     return oldkey;
  32. }
  33.  
  34. void *rem_malloc(size)        /* allocate memory, tag to list */ 
  35.     unsigned int size;
  36. {
  37.     RemKey *k;
  38.  
  39.     k = emalloc(sizeof(RemKey));
  40.     k->ptr = emalloc(size);
  41.     k->next = *gkey;
  42.     *gkey = k;
  43.  
  44.     return k->ptr;
  45. }
  46.  
  47. void rem_freeall()        /* free all memory associated with a key */
  48. {
  49.     RemKey *k;
  50.  
  51.     while (*gkey)
  52.     {
  53.         k = (*gkey)->next;
  54.         free((*gkey)->ptr);
  55.         free(*gkey);
  56.         *gkey = k;
  57.     }
  58. }
  59.  
  60.  
  61. /* just free 'remember' info */
  62. /* not being used in current implementation */
  63. /********
  64. void rem_freelinks()    
  65. {
  66.     RemKey *k;
  67.  
  68.     while (*gkey)
  69.     {
  70.         k = (*gkey)->next;
  71.         free(*gkey);
  72.         *gkey = k;
  73.     }
  74. }
  75. *********/