📄 extrautilmemory.c
字号:
/**CFile**************************************************************** FileName [extraUtilMemory.c] PackageName [extra] Synopsis [Memory managers.] Author [Alan Mishchenko] Affiliation [UC Berkeley] Date [Ver. 1.0. Started - February 1, 2003.] Revision [$Id: extraUtilMemory.c,v 1.2 2003/05/27 23:14:41 alanmi Exp $]***********************************************************************/#include "extra.h"#include "util.h"#include "string.h"/*---------------------------------------------------------------------------*//* Constant declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Stucture declarations *//*---------------------------------------------------------------------------*/struct mm_fixed_{ // information about individual entries int nEntrySize; // the size of one entry int nEntriesAlloc; // the total number of entries allocated int nEntriesUsed; // the number of entries in use char * pEntriesFree; // the linked list of free entries // this is where the memory is stored int nChunkSize; // the size of one chunk int nChunksAlloc; // the maximum number of memory chunks int nChunks; // the current number of memory chunks char ** pChunks; // the allocated memory // statistics int nMemoryUsed; // memory used in the allocated entries int nMemoryAlloc; // memory allocated};struct mm_flex_{ // information about individual entries int nEntriesUsed; // the number of entries allocated char * pCurrent; // the current pointer to free memory char * pEnd; // the first entry outside the free memory // this is where the memory is stored int nChunkSize; // the size of one chunk int nChunksAlloc; // the maximum number of memory chunks int nChunks; // the current number of memory chunks char ** pChunks; // the allocated memory // statistics int nMemoryUsed; // memory used in the allocated entries int nMemoryAlloc; // memory allocated};/*---------------------------------------------------------------------------*//* Type declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Variable declarations *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Macro declarations *//*---------------------------------------------------------------------------*//**AutomaticStart*************************************************************//*---------------------------------------------------------------------------*//* Static function prototypes *//*---------------------------------------------------------------------------*//**AutomaticEnd***************************************************************//*---------------------------------------------------------------------------*//* Definition of exported functions *//*---------------------------------------------------------------------------*//**Function************************************************************* Synopsis [] Description [Can only work with entry size at least 4 byte long.] SideEffects [] SeeAlso []***********************************************************************/mm_fixed * memManFixedStart( int nEntrySize, int nChunkSize, int nChunksAlloc ){ mm_fixed * p; p = ALLOC( mm_fixed, 1 ); memset( p, 0, sizeof(mm_fixed) ); p->nEntrySize = nEntrySize; p->nEntriesAlloc = 0; p->nEntriesUsed = 0; p->pEntriesFree = NULL; p->nChunkSize = nChunkSize; p->nChunksAlloc = nChunksAlloc; p->nChunks = 0; p->pChunks = ALLOC( char *, p->nChunksAlloc ); p->nMemoryUsed = 0; p->nMemoryAlloc = 0; return p;}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/void memManFixedStop( mm_fixed * p, int fVerbose ){ int i; if ( p == NULL ) return; if ( fVerbose ) { printf( "Fixed memory manager: Chunk size = %d. Chunks used = %d.\n", p->nChunkSize, p->nChunks ); printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n", p->nEntriesUsed, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc ); } for ( i = 0; i < p->nChunks; i++ ) free( p->pChunks[i] ); free( p->pChunks ); free( p );}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/char * memManFixedEntryFetch( mm_fixed * p ){ char * pTemp; int i; // check if there are still free entries if ( p->nEntriesUsed == p->nEntriesAlloc ) { // need to allocate more entries assert( p->pEntriesFree == NULL ); if ( p->nChunks == p->nChunksAlloc ) { /* // memory manager has run out of memory printf( "Memory manager already allocated %d entries and cannot allocate more.\n", p->nEntriesAlloc ); printf( "Entry size = %d bytes. Chunk size = %d. Maximum chunks = %d.\n", p->nEntrySize, p->nChunkSize, p->nChunksAlloc ); exit(1); */ p->nChunksAlloc *= 2; p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); } p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize ); p->nMemoryAlloc += p->nEntrySize * p->nChunkSize; // transform these entries into a linked list pTemp = p->pEntriesFree; for ( i = 1; i < p->nChunkSize; i++ ) { *((char **)pTemp) = pTemp + p->nEntrySize; pTemp += p->nEntrySize; } // set the last link *((char **)pTemp) = NULL; // add the chunk to the chunk storage p->pChunks[ p->nChunks++ ] = p->pEntriesFree; // add to the number of entries allocated p->nEntriesAlloc += p->nChunkSize; } // incrememt the counter of used entries p->nEntriesUsed++; // return the first entry in the free entry list pTemp = p->pEntriesFree; p->pEntriesFree = *((char **)pTemp); return pTemp;}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/void memManFixedEntryRecycle( mm_fixed * p, char * pEntry ){ // decrement the counter of used entries p->nEntriesUsed--; // add the entry to the linked list of free entries *((char **)pEntry) = p->pEntriesFree; p->pEntriesFree = pEntry;}/**Function************************************************************* Synopsis [] Description [Relocates all the memory except the first chunk.] SideEffects [] SeeAlso []***********************************************************************/void memManFixedRestart( mm_fixed * p ){ int i; char * pTemp; // delocate all chunks except the first one for ( i = 1; i < p->nChunks; i++ ) free( p->pChunks[i] ); p->nChunks = 1; // transform these entries into a linked list pTemp = p->pChunks[0]; for ( i = 1; i < p->nChunkSize; i++ ) { *((char **)pTemp) = pTemp + p->nEntrySize; pTemp += p->nEntrySize; } // set the last link *((char **)pTemp) = NULL; // set the free entry list p->pEntriesFree = p->pChunks[0]; // set the correct statistics p->nMemoryAlloc = p->nEntrySize * p->nChunkSize; p->nMemoryUsed = 0; p->nEntriesAlloc = p->nChunkSize; p->nEntriesUsed = 0;}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/int memManFixedReadMemUsage( mm_fixed * p ){ return p->nMemoryAlloc;}/**Function************************************************************* Synopsis [] Description [Can only work with entry size at least 4 byte long.] SideEffects [] SeeAlso []***********************************************************************/mm_flex * memManFlexStart( int nChunkSize, int nChunksAlloc ){ mm_flex * p; p = ALLOC( mm_flex, 1 ); memset( p, 0, sizeof(mm_flex) ); p->nEntriesUsed = 0; p->pCurrent = NULL; p->pEnd = NULL; p->nChunkSize = nChunkSize; p->nChunksAlloc = nChunksAlloc; p->nChunks = 0; p->pChunks = ALLOC( char *, p->nChunksAlloc ); p->nMemoryUsed = 0; p->nMemoryAlloc = 0; return p;}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/void memManFlexStop( mm_flex * p, int fVerbose ){ int i; if ( p == NULL ) return; if ( fVerbose ) { printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n", p->nChunkSize, p->nChunks ); printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n", p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc ); } for ( i = 0; i < p->nChunks; i++ ) free( p->pChunks[i] ); free( p->pChunks ); free( p );}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/char * memManFlexEntryFetch( mm_flex * p, int nBytes ){ char * pTemp; // check if there are still free entries if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd ) { // need to allocate more entries if ( p->nChunks == p->nChunksAlloc ) { p->nChunksAlloc *= 2; p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc ); } if ( nBytes > p->nChunkSize ) { // resize the chunk size if more memory is requested than it can give // (ideally, this should never happen) p->nChunkSize = 3 * nBytes; } p->pCurrent = ALLOC( char, p->nChunkSize ); p->pEnd = p->pCurrent + p->nChunkSize; p->nMemoryAlloc += p->nChunkSize; // add the chunk to the chunk storage p->pChunks[ p->nChunks++ ] = p->pCurrent; } assert( p->pCurrent + nBytes <= p->pEnd ); // increment the counter of used entries p->nEntriesUsed++; // keep track of the memory used p->nMemoryUsed += nBytes; // return the next entry pTemp = p->pCurrent; p->pCurrent += nBytes; return pTemp;}/**Function************************************************************* Synopsis [] Description [] SideEffects [] SeeAlso []***********************************************************************/int memManFlexReadMemUsage( mm_flex * p ){ return p->nMemoryAlloc;}/*---------------------------------------------------------------------------*//* Definition of internal functions *//*---------------------------------------------------------------------------*//*---------------------------------------------------------------------------*//* Definition of static functions *//*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -