home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume9 / elm2 / part01 / src / pmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-08  |  1.2 KB  |  50 lines

  1. /**            pmalloc.c        **/
  2.  
  3. /** This routine contains a cheap permanent version of the malloc call to 
  4.     speed up the initial allocation of the weedout headers and the uuname 
  5.     data.  
  6.  
  7.       This routine is originally from Jim Davis of HP Labs, with some 
  8.     mods by me.
  9. **/
  10.  
  11. #include <stdio.h>
  12. #include "defs.h"
  13.  
  14. /*VARARGS0*/
  15.  
  16. char *pmalloc(size)
  17. int size; 
  18. {
  19.     /** return the address of a specified block **/
  20.  
  21.     static char *our_block = NULL;
  22.     static int   free_mem  = 0;
  23.  
  24.     char   *return_value, *malloc();
  25.  
  26.     /** if bigger than our threshold, just do the real thing! **/
  27.  
  28.     if (size > PMALLOC_THRESHOLD) 
  29.        return(malloc(size));
  30.  
  31.     /** if bigger than available space, get more, tossing what's left **/
  32.  
  33.     if (size > free_mem) {
  34.       if ((our_block = malloc(PMALLOC_BUFFER_SIZE)) == NULL) {
  35.         fprintf(stderr, "\n\r\n\rCouldn't malloc %d bytes!!\n\r\n\r",
  36.             PMALLOC_BUFFER_SIZE);
  37.         leave();    
  38.           }
  39.       our_block += 4;  /* just for safety, don't give back true address */
  40.       free_mem = PMALLOC_BUFFER_SIZE-4;
  41.     }
  42.     
  43.     return_value  = our_block;    /* get the memory */
  44.     size = ((size+3)/4)*4;        /* Go to quad byte boundary */
  45.     our_block += size;        /* use it up      */
  46.     free_mem  -= size;        /*  and decrement */
  47.  
  48.     return( (char *) return_value);
  49. }
  50.