home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / general / raytrace / renaisnc / delaunay.lha / Delaunay / storage.c < prev   
C/C++ Source or Header  |  1992-01-04  |  2KB  |  76 lines

  1. /*
  2. ** Author:      storage
  3. ** Purpose:     storage allocation in globs.
  4. **
  5. ** Copyright (c), 1988 GRAIL, University of Washington
  6. **
  7. ** This module provides dynamic memory storage allocation functions
  8. ** which minimizes the cost of allocation by allocating elements in
  9. ** large "globs".  The cost of a malloc is amortized over many 
  10. ** allocations.
  11. **
  12. **  For each size allocated, a free list must be maintained.
  13. **  It showed be initialized to NULL.  All further maintenance of the
  14. **  free list is handle in this module.
  15. **
  16. ** $Revision: 1.1 $
  17. ** $Date: 88/11/21 17:08:07 $
  18. ** $Locker: jamie $
  19. ** $Log:    storage.c,v $
  20.  * Revision 1.1  88/11/21  17:08:07  jamie
  21.  * Initial revision
  22.  * 
  23. */
  24. #include <stdio.h>
  25.  
  26. extern void *malloc();
  27.  
  28.  
  29. void *New( sizeofelt, globsize, freelist )
  30. unsigned sizeofelt;
  31. unsigned globsize;
  32. void ***freelist;
  33. {
  34.   void **result;        /* Actually a void * , but... */
  35.   void **glob;
  36.   int i;
  37.  
  38.   if (sizeofelt < sizeof(void *))  sizeofelt = sizeof(void*);
  39.  
  40.   if ( (void **) 0L != (*freelist) )
  41.     {
  42.       /* Get one from the free list */
  43.       result = (*freelist);
  44.       (*freelist) = (void **) (*(*freelist));
  45.     } 
  46.   else
  47.     {
  48.       /* Allocate a glob */
  49.       glob = (void **) malloc( sizeofelt * globsize );
  50.       if ( (void **) 0L == glob) 
  51.     {
  52.       fprintf( stderr, "malloc failure!\n" );
  53.       return (void *) 0L;
  54.     }
  55.       result =  glob;
  56.       glob = (void **) ( ( (char *) glob ) + sizeofelt );
  57.       for(i=1; i<globsize; i++)
  58.     {
  59.       *glob = (void *) (*freelist);
  60.       (*freelist) = glob;
  61.       glob = (void **) ( ( (char *) glob ) + sizeofelt );
  62.     }
  63.     }
  64.   return (void *) result;
  65. }
  66.     
  67.  
  68. void Dispose( item, freelist )
  69. void **item;            /* returned from New */
  70. void ***freelist;               /* Free list used when New was allocated */
  71. {
  72.   *item = (void *) (*freelist);
  73.   (*freelist) = item;
  74. }
  75.  
  76.