A header file to allocate some memory (2006)
This commit is contained in:
parent
771ba38721
commit
c7d8df0fb9
38
demos/alloc_perm.h
Normal file
38
demos/alloc_perm.h
Normal file
@ -0,0 +1,38 @@
|
||||
#define MAX_PERM_BLOCK 131072
|
||||
|
||||
/*
|
||||
* Allocate some permanent memory.
|
||||
* Permanent memory is never freed,
|
||||
* pointers into it may be copied safely.
|
||||
*/
|
||||
void *alloc_perm( int sMem )
|
||||
{
|
||||
static char *pMemPerm;
|
||||
static int iMemPerm;
|
||||
void *pMem;
|
||||
|
||||
while ( sMem % sizeof(long) != 0 )
|
||||
sMem++;
|
||||
if ( sMem > MAX_PERM_BLOCK )
|
||||
{
|
||||
printf("\n\rAlloc_perm: %d too large.", sMem );
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
if ( pMemPerm == NULL || iMemPerm + sMem > MAX_PERM_BLOCK )
|
||||
{
|
||||
iMemPerm = 0;
|
||||
if ( ( pMemPerm = calloc( 1, MAX_PERM_BLOCK ) ) == NULL )
|
||||
{
|
||||
perror( "Alloc_perm" );
|
||||
exit( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
pMem = pMemPerm + iMemPerm;
|
||||
iMemPerm += sMem;
|
||||
// nAllocPerm += 1;
|
||||
// sAllocPerm += sMem;
|
||||
return pMem;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user