home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Resource Library: Graphics
/
graphics-16000.iso
/
general
/
raytrace
/
renaisnc
/
delaunay.lha
/
Delaunay
/
.‾storage.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-04
|
2KB
|
74 lines
/*
** Author: storage
** Purpose: storage allocation in globs.
**
** Copyright (c), 1988 GRAIL, University of Washington
**
** This module provides dynamic memory storage allocation functions
** which minimizes the cost of allocation by allocating elements in
** large "globs". The cost of a malloc is amortized over many
** allocations.
**
** For each size allocated, a free list must be maintained.
** It showed be initialized to NULL. All further maintenance of the
** free list is handle in this module.
**
** $Revision: 1.1 $
** $Date: 88/11/21 17:08:07 $
** $Locker: jamie $
** $Log: storage.c,v $
* Revision 1.1 88/11/21 17:08:07 jamie
* Initial revision
*
*/
#include <stdio.h>
#include <malloc.h>
void *New( sizeofelt, globsize, freelist )
unsigned sizeofelt;
unsigned globsize;
void ***freelist;
{
void **result; /* Actually a void * , but... */
void **glob;
int i;
if (sizeofelt < sizeof(void *)) sizeofelt = sizeof(void*);
if ( (void **) 0L != (*freelist) )
{
/* Get one from the free list */
result = (*freelist);
(*freelist) = (void **) (*(*freelist));
}
else
{
/* Allocate a glob */
glob = (void **) malloc( sizeofelt * globsize );
if ( (void **) 0L == glob)
{
fprintf( stderr, "malloc failure!\n" );
return (void *) 0L;
}
result = glob;
glob = (void **) ( ( (char *) glob ) + sizeofelt );
for(i=1; i<globsize; i++)
{
*glob = (void *) (*freelist);
(*freelist) = glob;
glob = (void **) ( ( (char *) glob ) + sizeofelt );
}
}
return (void *) result;
}
void Dispose( item, freelist )
void **item; /* returned from New */
void ***freelist; /* Free list used when New was allocated */
{
*item = (void *) (*freelist);
(*freelist) = item;
}