home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume26
/
maint
/
part02
/
pool.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-13
|
13KB
|
405 lines
/******************************************************************************
*******************************************************************************
Site: Western Michigan University Academic Computer Center
System: Directory/File System Maintenance
Program: maint
Version=01 Level=00 01/24/92 Leonard J. Peirce
Purpose: Routines for handling memory pools.
Arguments: See individual routines.
External variables: See individual routines.
External functions:
Defined: free_pool, get_pool_mem, init_pool, new_pool, put_pool,
reset_pool
Called: None
Files accessed: None
Return codes: See individual routines
Compiling instructions: See Makefile
Linking instructions: See Makefile
Other information: Copyright (C) 1992, Leonard J. Peirce
********************************************************************************
*******************************************************************************/
/******************************************************************************/
/* */
/* # I N C L U D E F I L E S */
/* */
/******************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include "maint.h"
#include <string.h>
/******************************************************************************/
/* */
/* # D E F I N E S */
/* */
/******************************************************************************/
/******************************************************************************/
/* */
/* S T R U C T U R E S , U N I O N S , T Y P E D E F S */
/* */
/******************************************************************************/
/******************************************************************************/
/* */
/* E X T E R N A L D E F I N I T I O N S & D E C L A R A T I O N S */
/* */
/******************************************************************************/
char *get_pool_mem();
POOL_DEF *new_pool();
void init_pool(),
put_pool(),
free_pool(),
reset_pool();
/******************************************************************************/
/* */
/* S T A T I C D E F I N I T I O N S & D E C L A R A T I O N S */
/* */
/******************************************************************************/
/*******************************************************************************
********************************************************************************
Function: init_pool
Purpose: Allocate the first memory pool for the current directory,
initializing the pool structure node and initialize all of
the other structures to point to null pools. The remaining
pool node structures are linked together in a linked list.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
none
Termination Codes:
Code Reason
---- ------
CANT_ALLOC cannot allocate memory
********************************************************************************
*******************************************************************************/
void init_pool(pool,pool_length)
/******* FORMAL PARAMETERS *******/
POOL_DEF **pool; /* array of pool node structures */
size_t pool_length; /* length of pool to allocate */
{ /*** init_pool ***/
/******** LOCAL VARIABLES ********/
register POOL_DEF *tptr; /* temporary pool node structure ptr */
/* allocate memory for the pool node structure first */
tptr = (POOL_DEF *) malloc(sizeof(POOL_DEF));
if(tptr == NULL)
{
puts("*ERROR* Cannot allocate memory in init_pool #1");
exit(CANT_ALLOCATE);
}
/* allocate memory for the first pool and initialize all of the other
* accounting stuff associated with it
*/
tptr->first = (char *) malloc((u_int) pool_length);
if(tptr == NULL)
{
puts("*ERROR* Cannot allocate memory in init_pool #2");
exit(CANT_ALLOCATE);
}
/* set up the other stuff for the first memory pool */
tptr->ptr = tptr->first; /* set pointer to available memory */
tptr->remaining = pool_length; /* all bytes in pool available */
tptr->next_pool = NULL; /* terminate the linked list.... */
tptr->length = pool_length; /* save original length of pool */
*pool = tptr; /* start everything.... */
return;
} /*** init_pool ***/
/*******************************************************************************
********************************************************************************
Function: reset_pool
Purpose: Reset the pool pointers so that the memory allocated for them
can be used again without reallocating the memory.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void reset_pool(pool)
/******* FORMAL PARAMETERS *******/
register POOL_DEF *pool; /* memory pool node structures */
{ /*** reset_pool ***/
while(pool != NULL)
{
pool->ptr = pool->first; /* reset available byte pointer */
pool->remaining = pool->length; /* reset number of bytes available */
pool = pool->next_pool; /* go to next pool node structure */
}
return;
} /*** reset_pool ***/
/*******************************************************************************
********************************************************************************
Function: new_pool
Purpose: allocate memory for a pool node structure and the actual
memory for the pool; also link the pool to the pool list.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
POOL_DEF *new_pool(pool,pool_length)
/******* FORMAL PARAMETERS *******/
register POOL_DEF *pool; /* pointer to current memory pool */
size_t pool_length; /* length of pool to allocate */
{ /*** new_pool ***/
/******** LOCAL VARIABLES ********/
POOL_DEF *tptr; /* temporary pool pointer */
/* first see if there is a pool already out there; if there is,
* just return so that we don't have to reuse the memory
*/
if(pool->next_pool != NULL) /* is there a pool out there? */
return(pool->next_pool); /* yes, just return a pointer to it */
/* at this point we have conceded that a new memory pool is needed; allocate
* the structure to hold the new pool's accounting information
*/
init_pool(&tptr,pool_length);
pool->next_pool = tptr; /* link to current pool */
pool = pool->next_pool; /* move to new pool structure */
return(tptr); /* return pointer to new pool struct. */
} /*** new_pool ***/
/*******************************************************************************
********************************************************************************
Function: put_pool
Purpose: Copy a string to a memory pool. If the current pool does not
have enough memory to accomodate the string, allocate a new
pool first and then copy the string to the new pool.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void put_pool(dest,pool,str,length,pool_length)
/******* FORMAL PARAMETERS *******/
char **dest; /* where to store pointer to str */
POOL_DEF **pool; /* current memory pool structure */
char *str; /* string to be stored in memory pool */
size_t length; /* length of string to be stored */
size_t pool_length; /* length of pool if we need another */
{ /*** put_pool ***/
/******** LOCAL VARIABLES ********/
register POOL_DEF *tptr; /* temporary pool structure pointer */
if((*pool)->remaining < (length + 1)) /* has the pool run dry....? */
{
*pool = new_pool(*pool,pool_length); /* yes, get another pool */
}
tptr = *pool; /* set up temporary pool struct. ptr. */
strcpy(tptr->ptr,str); /* copy the string to the pool */
*dest = tptr->ptr; /* set the pointer to the string */
tptr->ptr = tptr->ptr + length + 1; /* update available memory pointer */
tptr->remaining = tptr->remaining - (length + 1);
return;
} /*** put_pool ***/
/*******************************************************************************
********************************************************************************
Function: get_pool_mem
Purpose: Return a pointer to a memory slot that will satisfy the request.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
retptr pointer to memory slot
********************************************************************************
*******************************************************************************/
char *get_pool_mem(pool,pool_length,length)
/******* FORMAL PARAMETERS *******/
register POOL_DEF **pool; /* current memory pool */
size_t pool_length, /* length of pool to request */
length; /* length of memory slot needed */
{ /*** get_pool_mem ***/
/******** LOCAL VARIABLES ********/
register char *retptr; /* pointer to return */
register POOL_DEF *tptr; /* used to speed things up a bit.... */
/* is there enough room in the current memory pool? If not, allocate
* another one and get the memory from there
*/
if((*pool)->remaining < (length + 1))
*pool = new_pool(*pool,pool_length);
tptr = *pool; /* make a pointer */
/* we have the memory, whether it is in a new pool or the one we had on
* entry; update the pool structure to reflect the memory that we need
* to take from it
*/
retptr = tptr->ptr; /* save pointer to memory slot */
tptr->ptr = tptr->ptr + length + 1L;
tptr->remaining = tptr->remaining - (length + 1);
return(retptr); /* return pointer to slot */
} /*** get_pool_mem ***/
/*******************************************************************************
********************************************************************************
Function: free_pool
Purpose: Free the memory of all of the memory pools for a directory,
including the node structures for all of the nodes. This
routine will be called when exiting a directory.
Global variables:
Name Examine/Modify/Use/Read/Write
---- -----------------------------
none
Return Codes:
Code Reason
---- ------
none
********************************************************************************
*******************************************************************************/
void free_pool(pool)
/******* FORMAL PARAMETERS *******/
register POOL_DEF *pool; /* first pool in pool node list */
{ /*** free_pool ***/
/******** LOCAL VARIABLES ********/
register POOL_DEF *tpool; /* temporary pool node pointer */
while(pool != NULL)
{
/* free the memory for the pool first */
tpool = pool->next_pool; /* save pointer to next pool node */
free(pool->first); /* free the memory */
free((char *) pool); /* free memory for pool node struct. */
pool = tpool; /* move to next pool in list */
}
return;
} /*** free_pool ***/