📄 memory.c
字号:
#include "config.h"#include "debug.h"#include "simple_list.h"typedef struct __mem_allocator smemAllocator_t;struct __mem_allocator { sElement_t header;#ifdef DEBUG int lineno; int validator; char pFileName[64]; int index;#endif /* DEBUG */};int __alloc_index = 0;dll_t* pdllmem = NULL;void* v2_malloc( size_t size#ifdef DEBUG , char* file, int lineno#endif /* DEBUG */ ){ unsigned int totalLength = 0; smemAllocator_t* pTemp; totalLength = sizeof( smemAllocator_t ) + size; pTemp = (smemAllocator_t*)malloc( totalLength ); if( pTemp == NULL ) return NULL; pTemp->header.iKey = size;#ifdef DEBUG printi("mem", "Allocate %d bytes in file %s at line %d index %d (%p:%p)\n", size, file, lineno, __alloc_index, pTemp, &pTemp[1] ); pTemp->lineno = lineno; pTemp->validator = 0xF0F0F0F0; strncpy( pTemp->pFileName, file, 64 ); pTemp->index = __alloc_index++;#endif /* DEBUG */ __dll_add_head( pdllmem, (sElement_t*)pTemp ); return (void*)&pTemp[1];}static inline void* __v2_calloc( size_t number, size_t size#ifdef DEBUG , char* file, int lineno#endif /* DEBUG */ ){ unsigned int totalLength = 0; smemAllocator_t* pTemp; totalLength = sizeof( smemAllocator_t ) + number * size; pTemp = (smemAllocator_t*)calloc( 1, totalLength ); pTemp->header.iKey = size;#ifdef DEBUG printi("mem", "CAllocate %d bytes in file %s at line %d (%p:%p)\n", size, file, lineno, pTemp, &pTemp[1] ); pTemp->lineno = lineno; pTemp->validator = 0xF0F0F0F0; strncpy( pTemp->pFileName, file, 64 );#endif /* DEBUG */ return pTemp;}void* v2_calloc( size_t number, size_t size#ifdef DEBUG , char* file, int lineno#endif /* DEBUG */ ){ smemAllocator_t* pTemp; pTemp = __v2_calloc( number, size#if defined(DEBUG) , file, lineno#endif /* DEBUG */ ); __dll_add_head( pdllmem, (sElement_t*)pTemp ); return (void*)&pTemp[1];}void* v2_realloc( void* ptr, size_t size#ifdef DEBUG , char* file, int lineno#endif /* DEBUG */ ){ unsigned int totalLength = 0; smemAllocator_t* pTemp = (smemAllocator_t*)((char*)ptr - sizeof(smemAllocator_t)); totalLength = sizeof( smemAllocator_t ) + size; pTemp = (smemAllocator_t*)realloc( pTemp, totalLength ); pTemp->header.iKey = size;#ifdef DEBUG printi("mem", "Reallocate %d bytes in file %s at line %d (%p:%p)\n", size, file, lineno, pTemp, &pTemp[1] ); pTemp->lineno = lineno; pTemp->validator = 0xF0F0F0F0; strncpy( pTemp->pFileName, file, 64 );#endif /* DEBUG */ __dll_add_head( pdllmem, (sElement_t*)pTemp ); return (void*)&pTemp[1];}void v2_free( void* ptr#ifdef DEBUG , char* file, int lineno#endif /* DEBUG */ ){ smemAllocator_t* pTemp = (smemAllocator_t*)((char*)ptr - sizeof(smemAllocator_t)); printi("mem", "Deallocate %d bytes in file %s at line %d allocate in file %s at line %d index %d (%p:%p)\n", pTemp->header.iKey, file, lineno, pTemp->pFileName, pTemp->lineno, pTemp->index, pTemp, ptr );#ifdef DEBUG ASSERT( pTemp->validator == 0xF0F0F0F0 ); pTemp->validator = 0;#endif /* remove it from the allocated memory */ if( __dll_remove_elem( pdllmem, (sElement_t*)pTemp ) == NULL ) { printw( "Cant find reference to allocated memory %p\n", pTemp ); } /* And now really free the memory */ free( pTemp );}#if defined(DEBUG)static void dump_memory_element( sElement_t* pElem ){ smemAllocator_t* pMem = (smemAllocator_t*)pElem; printw( "Allocate %d bytes in file %s at line %d\n", pMem->header.iKey, pMem->pFileName, pMem->lineno );}#endif /* DEBUG */void display_memory_usage( void ){#if defined(DEBUG) if( dll_is_empty(pdllmem) ) return; printw( ">> BEGIN MEMORY USAGE and TRACE\n" ); __dll_dump( pdllmem, dump_memory_element ); printw( ">> END MEMORY USAGE and TRACE\n" );#endif /* DEBUG */}void __attribute__((constructor)) __init_memory(void){ if( pdllmem != NULL ) return; printi("mem", "Memory sub-system initialized\n" ); /* handle circular calls between memory system and linked lists */#if defined(DEBUG) pdllmem = (dll_t*)__v2_calloc( 1, sizeof(dll_t), __FILE__, __LINE__ );#else pdllmem = (dll_t*)__v2_calloc( 1, sizeof(dll_t) );#endif /* DEBUG */ pdllmem->head = NULL; INIT_RW_LOCK( pdllmem->lock );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -