⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memorymanager.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 4 页
字号:
 *  This method is responsible for formating the owner string.  This string states the file
 *  name and line number within the specified file.
 * 
 *  Return Type : char* -> A pointer to the string representing the owner string. 
 *  Arguments   : 
 *  	const char *file : The files name
 *  	int line	       : The line number within the specified file.
 */
char *formatOwnerString( const char *file, int line )
{
  static char	str[90];
	memset( str, 0, sizeof(str));
	sprintf( str, "%s(%05d)", sourceFileStripper(file), line );
	return str;
}

/*******************************************************************************************/

/**
 * sourceFileStripper():
 *  This method takes a file name and strips off all directory information.
 * 
 *  Return Type : char* -> A pointer to the actual file minus all directory information. 
 *  Arguments   : 
 *  	const char *sourceFile : The file to strip.
 */
char *sourceFileStripper( const char *sourceFile )
{
	if (!sourceFile) return NULL;
	char *ptr = strrchr( sourceFile, '\\' );
	if (ptr) return ptr + 1;
	ptr = strrchr(sourceFile, '/');
	if (ptr) return ptr + 1;
	return (char*)sourceFile;
}

/*******************************************************************************************/

/**
 * log():
 *  Dumps a specific string to the log file.  Used for error reporting during runtime.  This
 *  method accepts a variable argument lenght such as printf() for ease of reporting.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	char *s	: The string to be written to the log file.
 *  	...	    : The parameters to be placed within the string, simular to say: printf( s, ... )
 */
void log( char *s, ... )
{
	static char buffer[2048];            // Create the buffer
	va_list	list;
	va_start( list, s );
	vsprintf( buffer, s, list );
	va_end( list );

	FILE	*fp = fopen( LOGFILE, "ab" );  // Open the log file
	if (!fp) return;

	fprintf( fp, "%s\r\n", buffer );     // Write the data to the log file
	fclose( fp );                        // Close the file
}

/*******************************************************************************************/

/**
 * insertCommas():
 *  This method takes a value and inserts commas, creating a nicely formated string.  Thus
 *  the value => 23456 would be converted to the following string => 23,456.
 * 
 *  Return Type : char* -> A string representing the provided value with commas inserted. 
 *  Arguments   : 
 *  	unsigned long value	: The value to insert commas into.
 */
char *insertCommas( unsigned long value )
{
	static	char	str[30];
	for (int ii = 0; ii < 30; ++ii) str[ii] = NULL;
	sprintf(str, "%d", value);
	if (strlen(str) > 3) {
		memmove( &str[strlen(str)-3], &str[strlen(str)-4], 4 );
		str[strlen(str) - 4] = ',';
	}
	if (strlen(str) > 7) {
		memmove( &str[strlen(str)-7], &str[strlen(str)-8], 8 );
		str[strlen(str) - 8] = ',';
	}
	if (strlen(str) > 11) {
		memmove( &str[strlen(str)-11], &str[strlen(str)-12], 12 );
		str[strlen(str) - 12] = ',';
	}
	return str;
}

/*******************************************************************************************/

/**
 * memorySizeString():
 *  This method takes a memory size and creates a user friendly string that displays the 
 *  memory size in bytes, K or M. 
 * 
 *  Return Type : char* -> The final memory size string. 
 *  Arguments   : 
 *  	unsigned long size	: The size of the memory.
 *  	bool lengthenString : Whether or not to pad the string with white spaces.
 */
char *memorySizeString( unsigned long size, bool lengthenString /* = true */ )
{
	static	char	str[90];
	if (lengthenString) {
		if (size > (1024*1024))	sprintf(str, "%10s (%7.2fM)", insertCommas(size), size / (1024.0 * 1024.0));
		else if (size > 1024)		sprintf(str, "%10s (%7.2fK)", insertCommas(size), size / 1024.0);
		else				            sprintf(str, "%10s bytes     ", insertCommas(size), size);
	}
	else {
		if (size > (1024*1024))	sprintf(str, "%s (%7.2fM)", insertCommas(size), size / (1024.0 * 1024.0));
		else if (size > 1024)		sprintf(str, "%s (%7.2fK)", insertCommas(size), size / 1024.0);
		else				            sprintf(str, "%s bytes     ", insertCommas(size), size);
	}
	return str;
}

/*******************************************************************************************/
/*******************************************************************************************/
// ****** Implementation of Access Functions defined within MemoryManager.h

/**
 * dumpLogReport():
 *  Dump the log report to the file, this is the same method that is automatically called 
 *  upon the programs termination to report all statistical information.
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void dumpLogReport( void )
{
	if (s_manager) s_manager->dumpLogReport();
}

/*******************************************************************************************/

/**
 * dumpMemoryAllocations():
 *  Report all allocated memory to the log file.
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void dumpMemoryAllocations( void )
{
	if (s_manager) s_manager->dumpMemoryAllocations();
}

/*******************************************************************************************/

/**
 * setLogFile():
 *  Allows for the log file to be changed from the default.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	char *file : The name of the new log file.
 */
void setLogFile( char *file )
{
	if (file) strcpy( LOGFILE, file );
}

/*******************************************************************************************/

/**
 * setExhaustiveTesting():
 *  This method allows for exhaustive testing.  It has the same functionality as the following
 *  function calls => setLogAlways( true ); setPaddingSize( 1024 ); 
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	bool test : Whether or not to turn exhaustive testing on or off.
 */
void setExhaustiveTesting( bool test /* = true */ )
{
	if (!s_manager) return;

	if (test) {
		setPaddingSize( 1024 );
		setLogAlways();
	}
	else {
		setPaddingSize();
		setLogAlways( false );
	}
}

/*******************************************************************************************/

/**
 * setLogAlways():
 *  Sets the flag for exhaustive information logging.  All information is sent to the log file.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	bool log 	: Whether or not to log all information.
 */
void setLogAlways( bool log /* = true */ )
{
	if (s_manager) s_manager->m_logAlways = log;
} 

/*******************************************************************************************/

/**
 * setPaddingSize():
 *  Sets the padding size for memory bounds checks.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	int size 	: The new padding size.
 */
void setPaddingSize( int size /* = 4 */ )
{
	if (s_manager && size > 0) s_manager->m_paddingSize = size;
}

/*******************************************************************************************/

/**
 * cleanLogFile():
 *  Cleans out the log file by deleting it.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	bool clean : Whether or not to clean the log file.
 */
void cleanLogFile( bool clean /* = true */ )
{
	if (s_manager) s_manager->m_cleanLogFileOnFirstRun = true;
}

/*******************************************************************************************/

/**
 * breakOnAllocation():
 *  Allows you to set a break point on the n-th allocation.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	int allocationCount	: The allocation count to break on.
 */
void breakOnAllocation( int allocationCount )
{
	if (s_manager && allocationCount > 0) s_manager->m_breakOnAllocationCount = allocationCount;
}

/*******************************************************************************************/

/**
 * breakOnDeallocation():
 *  Sets a flag that will set a break point when the specified memory is deallocated.
 * 
 *  Return Type : void 
 *  Arguments   : 
 *  	void *address	: The address to break on when it is deallocated.
 */
void breakOnDeallocation( void *address )
{
	if (!s_manager || !address) return;

	MemoryNode *node = s_manager->getMemoryNode( address );
	node->options |= BREAK_ON_DEALLOC;
}

/*******************************************************************************************/

/**
 * breakOnReallocation():
 *  Sets a flag that will set a break point when the specified memory is reallocated by 
 *  using the realloc() method.
 *
 *  Return Type : void 
 *  Arguments   : 
 *  	void *address	: The address to break on when it is reallocated.
 */
void breakOnReallocation( void *address )
{
	if (!s_manager || !address) return;

	MemoryNode *node = s_manager->getMemoryNode( address );
	node->options |= BREAK_ON_REALLOC;
}

/*******************************************************************************************/

/**
 * setOwner():
 *  This method is only called by the delete macro defined within the MemoryManager.h header.
 *  It is responsible for logging the file and line number for tracking information.  For
 *  an explanation for the stack implementation refer to the MemoryManager class definition.
 *
 *  Return Type : void 
 *  Arguments   : 
 *  	const char *file : The file requesting the deallocation.
 *  	int line	       : The line number within the file.
 */
void setOwner( const char *file, int line )
{
	if (s_manager) {
		StackNode *n = (StackNode*)malloc( sizeof(StackNode) );
		n->fileName = file;
		n->lineNumber = line;
		s_manager->m_topStack.push( n );
	}
}

#endif  /* ACTIVATE_MEMORY_MANAGER */

// ***** End of MemoryManager.cpp
/*******************************************************************************************/
/*******************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -