home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 0 / 0980 / ckalloc.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  824b  |  50 lines

  1. /*
  2.  * VOID *ckalloc(memory)
  3.  * unsigned memory;
  4.  *
  5.  * Allocate memory using malloc. If it fails, call a user-defined routine.
  6.  * This routine returns one of:
  7.  *
  8.  * ALLOC_FATAL (-1)    Can't free any more memory, abort.
  9.  * ALLOC_RETRY (0)    Try to allocate the memory again.
  10.  */
  11. #include <stdio.h>
  12. #include "ckalloc.h"
  13.  
  14. static int (*lowmem)() = NULL;
  15.  
  16. VOID *ckalloc(memory)
  17. unsigned memory;
  18. {
  19.     VOID *result;
  20.     VOID *malloc();
  21.  
  22.     do {
  23.         result = malloc(memory);
  24.     } while(result == NULL
  25.          && lowmem
  26.          && (*lowmem)(memory) == ALLOC_RETRY);
  27.  
  28.     if(result == NULL)
  29.         panic("Out of memory: can't malloc %u bytes.\n", memory);
  30.  
  31.     return result;
  32. }
  33.  
  34. int (*setalloc(func))()
  35. int (*func)();
  36. {
  37.     int (*old_lowmem)();
  38.  
  39.     old_lowmem = lowmem;
  40.     lowmem = func;
  41.     return old_lowmem;
  42. }
  43.  
  44. ckfree(memory)
  45. char *memory;
  46. {
  47.     if(memory)
  48.         free(memory);
  49. }
  50.