/* memory_tracker_add(size_t addr, unsigned int size, char * file, unsigned int line) Adds an address (addr), it's size, file and line number to our list. Adjusts the total bytes allocated and max bytes allocated if necessary. If memory cannot be allocated the list will be destroyed. */ void memory_tracker_add(size_t addr, unsigned int size, char *file, unsigned int line, int padded) { if (!memory_tracker_lock_mutex()) { struct mem_block *p; p = MEM_TRACK_MALLOC(sizeof(struct mem_block)); if (p) { p->prev = memtrack.tail; p->prev->next = p; p->addr = addr; p->size = size; p->line = line; p->file = file; p->padded = padded; p->next = NULL; memtrack.tail = p; memtrack.current_allocated += size; if (memtrack.current_allocated > memtrack.max_allocated) memtrack.max_allocated = memtrack.current_allocated; // memtrack_log("memory_tracker_add: added addr=0x%.8x\n", addr); memory_tracker_unlock_mutex(); } else { memtrack_log("memory_tracker_add: error allocating memory!\n"); memory_tracker_unlock_mutex(); vpx_memory_tracker_destroy(); } } }
/* memory_tracker_remove(size_t addr) Removes an address and its corresponding size (if they exist) from the memory tracker list and adjusts the current number of bytes allocated. Return: 0: on success -1: if the mutex could not be locked -2: if the addr was not found in the list */ int memory_tracker_remove(size_t addr) { int ret = -1; if (!memory_tracker_lock_mutex()) { struct mem_block *p; if ((p = memory_tracker_find(addr))) { memtrack.current_allocated -= p->size; p->prev->next = p->next; if (p->next) p->next->prev = p->prev; else memtrack.tail = p->prev; ret = 0; MEM_TRACK_FREE(p); } else { if (addr) memtrack_log("memory_tracker_remove(): addr not found in list," " 0x%.8x\n", addr); ret = -2; } memory_tracker_unlock_mutex(); } return ret; }
/* vpx_memory_tracker_destroy() If our global struct was initialized zeros out all its members, frees memory and destroys it's mutex */ void vpx_memory_tracker_destroy() { if (!memory_tracker_lock_mutex()) { struct mem_block *p = memtrack.head, * p2 = memtrack.head; memory_tracker_dump(); while (p) { p2 = p; p = p->next; MEM_TRACK_FREE(p2); } memtrack.head = NULL; memtrack.tail = NULL; memtrack.len = 0; memtrack.current_allocated = 0; memtrack.max_allocated = 0; if (!g_logging.type && g_logging.file && g_logging.file != stderr) { fclose(g_logging.file); g_logging.file = NULL; } memory_tracker_unlock_mutex(); g_b_mem_tracker_inited = 0; } }
/* vpx_memory_tracker_check_integrity(char* file, unsigned int line) file - The file name where the check was placed line - The line in file where the check was placed Locks the memory tracker's mutex and calls the internal integrity check function to inspect every address in the global memory allocation list */ void vpx_memory_tracker_check_integrity(char *file, unsigned int line) { if (!memory_tracker_lock_mutex()) { memory_tracker_check_integrity(file, line); memory_tracker_unlock_mutex(); } }
/* vpx_memory_tracker_dump() Locks the memory tracker's mutex and calls the internal library function to dump the current contents of the global memory allocation list */ void vpx_memory_tracker_dump() { if (!memory_tracker_lock_mutex()) { memory_tracker_dump(); memory_tracker_unlock_mutex(); } }
/* vpx_memory_tracker_find(size_t addr) addr - address to be found in list Return: If found, pointer to the memory block that matches addr NULL otherwise */ struct mem_block *vpx_memory_tracker_find(size_t addr) { struct mem_block *p = NULL; if (!memory_tracker_lock_mutex()) { p = memory_tracker_find(addr); memory_tracker_unlock_mutex(); } return p; }
/* vpx_memory_tracker_destroy() If our global struct was initialized zeros out all its members, frees memory and destroys it's mutex */ void vpx_memory_tracker_destroy() { if (!memory_tracker_lock_mutex()) { struct mem_block *p = memtrack.head, * p2 = memtrack.head; memory_tracker_dump(); while (p) { p2 = p; p = p->next; MEM_TRACK_FREE(p2); } memtrack.head = NULL; memtrack.tail = NULL; memtrack.len = 0; memtrack.current_allocated = 0; memtrack.max_allocated = 0; if ((g_logging.type == 0) && (g_logging.file != 0)) //&& (g_logging.file != stderr) ) { #if !defined(NDS_NITRO) fclose(g_logging.file); #endif g_logging.file = NULL; } memory_tracker_unlock_mutex(); g_b_mem_tracker_inited = 0; } }