/* 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(); } } }
/* vpx_memory_tracker_init(int padding_size, int pad_value) padding_size - the size of the padding before and after each mem addr. Values > 0 indicate that integrity checks can be performed by inspecting these areas. pad_value - the initial value within the padding area before and after each mem addr. Initializes global memory tracker structure Allocates the head of the list */ int vpx_memory_tracker_init(int padding_size, int pad_value) { if (!g_b_mem_tracker_inited) { if (memtrack.head = (struct mem_block *)MEM_TRACK_MALLOC(sizeof(struct mem_block))) { int ret; MEM_TRACK_MEMSET(memtrack.head, 0, sizeof(struct mem_block)); memtrack.tail = memtrack.head; memtrack.current_allocated = 0; memtrack.max_allocated = 0; memtrack.padding_size = padding_size; memtrack.pad_value = pad_value; #if defined(LINUX) || defined(__uClinux__) ret = pthread_mutex_init(&memtrack.mutex, NULL); /*mutex attributes (NULL=default)*/ #elif defined(WIN32) || defined(_WIN32_WCE) memtrack.mutex = create_mutex(NULL, /*security attributes*/ FALSE, /*we don't want initial ownership*/ NULL); /*mutex name*/ ret = !memtrack.mutex; #elif defined(VXWORKS) memtrack.mutex = sem_bcreate(SEM_Q_FIFO, /*SEM_Q_FIFO non-priority based mutex*/ SEM_FULL); /*SEM_FULL initial state is unlocked*/ ret = !memtrack.mutex; #elif defined(NDS_NITRO) os_init_mutex(&memtrack.mutex); ret = 0; #elif defined(NO_MUTEX) ret = 0; #endif if (ret) { memtrack_log("vpx_memory_tracker_init: Error creating mutex!\n"); MEM_TRACK_FREE(memtrack.head); memtrack.head = NULL; } else { memtrack_log("Memory Tracker init'd, v."vpx_mem_tracker_version" pad_size:%d pad_val:0x%x %d\n" , padding_size , pad_value , pad_value); g_b_mem_tracker_inited = 1; } } } return g_b_mem_tracker_inited; }
int vpx_memory_tracker_init(int padding_size, int pad_value) { if (!g_b_mem_tracker_inited) { if ((memtrack.head = (struct mem_block *) MEM_TRACK_MALLOC(sizeof(struct mem_block)))) { int ret; MEM_TRACK_MEMSET(memtrack.head, 0, sizeof(struct mem_block)); memtrack.tail = memtrack.head; memtrack.current_allocated = 0; memtrack.max_allocated = 0; memtrack.padding_size = padding_size; memtrack.pad_value = pad_value; #if HAVE_PTHREAD_H ret = pthread_mutex_init(&memtrack.mutex, NULL); #elif defined(WIN32) || defined(_WIN32_WCE) memtrack.mutex = CreateMutex(NULL, FALSE, NULL); ret = !memtrack.mutex; #elif defined(VXWORKS) memtrack.mutex = sem_bcreate(SEM_Q_FIFO, SEM_FULL); ret = !memtrack.mutex; #elif defined(NO_MUTEX) ret = 0; #endif if (ret) { memtrack_log("vpx_memory_tracker_init: Error creating mutex!\n"); MEM_TRACK_FREE(memtrack.head); memtrack.head = NULL; } else { memtrack_log("Memory Tracker init'd, v."vpx_mem_tracker_version" pad_size:%d pad_val:0x%x %d\n" , padding_size , pad_value , pad_value); g_b_mem_tracker_inited = 1; } } } return g_b_mem_tracker_inited; }