/* ------------------------------------------------------------------------------ | MEM | | Author : Terry Leach | Revision: 8/22/01 Initial Creation | Revision: 10/04/01 Add huge memory support | Revision: 4/16/02 Make use of the error handling object | | Purpose : This is a memory encapsulation providing for standard checking and error message reporting surrounding malloc, realloc, halloc etc. calls. */ #include // debugging #include #include #include #include "ERROR.hpp" char *MEM_MsgOut( char *pszFail ); #define ERRMSG_LEN 256 #define ADD_LEN 24 typedef unsigned int UINT; static ERR err; static char _szMemErr[ ERRMSG_LEN ]; // .... ....1.... ....2.... ....3 static char *pszMsg = " failed mem allocation\0"; // ------------------------------------------------- void MEM_Show( char *pszMark ) { /* UINT unAvl; UINT unContig; // ------------------ unAvl = _memavl(); unContig = _memmax(); printf( "MEM_Show: Avl %u Contig %u @ %s\n" ,unAvl ,unContig ,pszMark ); */ printf( "MEM_Show: %s\n" ,pszMark ); } void *MEM_New( unsigned int unSz ,char *pszFail ) { void *pv; // --------------- pv = malloc( unSz ); if( pv == NULL ) err.Abort( MEM_MsgOut( pszFail ) ); memset( pv ,0 ,unSz ); return pv; } void *MEM_Grow( void *pvCurMem ,unsigned int unOldSz ,int nSzToAdd ,char *pszFail ) { void *pv; // ------------------- pv = realloc( pvCurMem ,unOldSz+nSzToAdd ); // Guarantees old contents to be unchanged even if pv!=pvCurMem // Have to assume pvCurMem is freed if move takes place if( pv == NULL ) { err.Warn( MEM_MsgOut( pszFail ) ); return NULL; } if( !unOldSz ) memset( pv ,0 ,nSzToAdd ); return pv; } char *MEM_MsgOut( char *pszFail ) { int nMsgEnd; // ----------------- strcpy( _szMemErr ,pszFail ); nMsgEnd = strlen( _szMemErr ); strncpy( &_szMemErr[nMsgEnd] ,pszMsg ,__min( ADD_LEN ,ERRMSG_LEN - nMsgEnd ) ); return _szMemErr; } void MEM_Del( void *pv ) { free( pv ); }