home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / rolodex / part01 / toolsdir / mem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  2.2 KB  |  94 lines

  1. /**************************************************************************/
  2. /**************************************************************************/
  3.  
  4.  
  5.                   /***** Block Memory Allocator *****/
  6.  
  7.  
  8. /**************************************************************************/
  9. /**************************************************************************/
  10.  
  11. /* Author: JP Massar */
  12.  
  13. #include <stdio.h>
  14.  
  15. #include "sys5.h"
  16.  
  17. #ifdef BSD42
  18. #include <strings.h>
  19. #endif
  20.  
  21. #define NO_MORE_MEMORY -1
  22.  
  23. static int bytes_left;                  /* space left in current block */
  24. static char *ptr_next_byte;             /* next free byte */
  25. static char *ptr_space;                 /* current block */
  26. static unsigned chunk_size;                  /* size of block last allocated */
  27.  
  28. extern char *malloc();
  29.  
  30.  
  31. unsigned allocate_memory_chunk(space) 
  32. unsigned space;
  33.  
  34. /* malloc up a new block of memory.  Set our static variables */
  35. /* returns NO_MORE_MEMORY if can't allocate block. */
  36.  
  37. #ifdef MSDOS
  38.   if ((char *)NULL == (ptr_space = malloc(space))) {
  39. #else
  40.   if (0 == (ptr_space = malloc(space))) {
  41. #endif
  42.         fprintf(stderr,"fatal error, no more memory\n");
  43.         return(NO_MORE_MEMORY);
  44.   }
  45.   ptr_next_byte = ptr_space;
  46.   bytes_left = space;
  47.   chunk_size = space;
  48.   return(0);
  49. }
  50.  
  51.  
  52. char * get_memory_chunk (size) unsigned size;
  53.  
  54. /* allocate a small segment out of our large block of memory.  If we */
  55. /* run out allocate another block.  Adjust our static variables. */
  56. /* returns 0 if no more memory. */
  57.  
  58. { char *rval;
  59.         
  60.   if (size > chunk_size) {
  61.         fprintf(stderr,"attempt to allocate too large a chunk\n");
  62.         return(0);
  63.   }
  64.         
  65.   if (size > bytes_left) {
  66.         if (NO_MORE_MEMORY == allocate_memory_chunk(chunk_size)) {
  67.                 return(0);
  68.         }
  69.         return(get_memory_chunk(size));
  70.   }
  71.  
  72.   rval = ptr_next_byte;
  73.   ptr_next_byte += size;
  74.   bytes_left -= size;
  75.   return(rval);
  76.   
  77. }
  78.  
  79.  
  80. char * store_string (str,len) char *str; unsigned len;
  81.  
  82. /* put copy of a string into storage in a memory block.  Return a pointer to */
  83. /* the copied string.  Returns 0 if out of memory. */
  84.  
  85. { char *ptr_space;
  86.  
  87.   if (0 == (ptr_space = get_memory_chunk(len+1))) {
  88.         return(0);
  89.   }
  90.   strcpy(ptr_space,str);
  91.   return(ptr_space);
  92. }
  93.