home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / general / raytrace / renaisnc / delaunay.lha / Delaunay / .‾storage.c < prev    next >
C/C++ Source or Header  |  1992-01-04  |  2KB  |  74 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. #include <malloc.h>
  26.  
  27. void *New( sizeofelt, globsize, freelist )
  28. unsigned sizeofelt;
  29. unsigned globsize;
  30. void ***freelist;
  31. {
  32.   void **result;        /* Actually a void * , but... */
  33.   void **glob;
  34.   int i;
  35.  
  36.   if (sizeofelt < sizeof(void *))  sizeofelt = sizeof(void*);
  37.  
  38.   if ( (void **) 0L != (*freelist) )
  39.     {
  40.       /* Get one from the free list */
  41.       result = (*freelist);
  42.       (*freelist) = (void **) (*(*freelist));
  43.     } 
  44.   else
  45.     {
  46.       /* Allocate a glob */
  47.       glob = (void **) malloc( sizeofelt * globsize );
  48.       if ( (void **) 0L == glob) 
  49.     {
  50.       fprintf( stderr, "malloc failure!\n" );
  51.       return (void *) 0L;
  52.     }
  53.       result =  glob;
  54.       glob = (void **) ( ( (char *) glob ) + sizeofelt );
  55.       for(i=1; i<globsize; i++)
  56.     {
  57.       *glob = (void *) (*freelist);
  58.       (*freelist) = glob;
  59.       glob = (void **) ( ( (char *) glob ) + sizeofelt );
  60.     }
  61.     }
  62.   return (void *) result;
  63. }
  64.     
  65.  
  66. void Dispose( item, freelist )
  67. void **item;            /* returned from New */
  68. void ***freelist;               /* Free list used when New was allocated */
  69. {
  70.   *item = (void *) (*freelist);
  71.   (*freelist) = item;
  72. }
  73.  
  74.