1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 12 #ifndef VPX_MEM_INCLUDE_VPX_MEM_TRACKER_H_ 13 #define VPX_MEM_INCLUDE_VPX_MEM_TRACKER_H_ 14 15 /* vpx_mem_tracker version info */ 16 #define vpx_mem_tracker_version "2.5.1.1" 17 18 #define VPX_MEM_TRACKER_VERSION_CHIEF 2 19 #define VPX_MEM_TRACKER_VERSION_MAJOR 5 20 #define VPX_MEM_TRACKER_VERSION_MINOR 1 21 #define VPX_MEM_TRACKER_VERSION_PATCH 1 22 /* END - vpx_mem_tracker version info */ 23 24 #include <stdarg.h> 25 26 struct mem_block { 27 size_t addr; 28 unsigned int size, 29 line; 30 char *file; 31 struct mem_block *prev, 32 * next; 33 34 int padded; // This mem_block has padding for integrity checks. 35 // As of right now, this should only be 0 if 36 // using vpx_mem_alloc to allocate cache memory. 37 // 2005-01-11 tjf 38 }; 39 40 #if defined(__cplusplus) 41 extern "C" { 42 #endif 43 44 /* 45 vpx_memory_tracker_init(int padding_size, int pad_value) 46 padding_size - the size of the padding before and after each mem addr. 47 Values > 0 indicate that integrity checks can be performed 48 by inspecting these areas. 49 pad_value - the initial value within the padding area before and after 50 each mem addr. 51 52 Initializes the memory tracker interface. Should be called before any 53 other calls to the memory tracker. 54 */ 55 int vpx_memory_tracker_init(int padding_size, int pad_value); 56 57 /* 58 vpx_memory_tracker_destroy() 59 Deinitializes the memory tracker interface 60 */ 61 void vpx_memory_tracker_destroy(); 62 63 /* 64 vpx_memory_tracker_add(size_t addr, unsigned int size, 65 char * file, unsigned int line) 66 addr - memory address to be added to list 67 size - size of addr 68 file - the file addr was referenced from 69 line - the line in file addr was referenced from 70 Adds memory address addr, it's size, file and line it came from 71 to the memory tracker allocation table 72 */ 73 void vpx_memory_tracker_add(size_t addr, unsigned int size, 74 char *file, unsigned int line, 75 int padded); 76 77 /* 78 vpx_memory_tracker_add(size_t addr, unsigned int size, char * file, unsigned int line) 79 addr - memory address to be added to be removed 80 padded - if 0, disables bounds checking on this memory block even if bounds 81 checking is enabled. (for example, when allocating cache memory, we still want 82 to check for memory leaks, but we do not waste cache space for bounds check padding) 83 Removes the specified address from the memory tracker's allocation 84 table 85 Return: 86 0: on success 87 -1: if memory allocation table's mutex could not be locked 88 -2: if the addr was not found in the list 89 */ 90 int vpx_memory_tracker_remove(size_t addr); 91 92 /* 93 vpx_memory_tracker_find(unsigned int addr) 94 addr - address to be found in the memory tracker's 95 allocation table 96 Return: 97 If found, pointer to the memory block that matches addr 98 NULL otherwise 99 */ 100 struct mem_block *vpx_memory_tracker_find(size_t addr); 101 102 /* 103 vpx_memory_tracker_dump() 104 Dumps the current contents of the memory 105 tracker allocation table 106 */ 107 void vpx_memory_tracker_dump(); 108 109 /* 110 vpx_memory_tracker_check_integrity() 111 If a padding_size was provided to vpx_memory_tracker_init() 112 This function will verify that the region before and after each 113 memory address contains the specified pad_value. Should the check 114 fail, the filename and line of the check will be printed out. 115 */ 116 void vpx_memory_tracker_check_integrity(char *file, unsigned int line); 117 118 /* 119 vpx_memory_tracker_set_log_type 120 type - value representing the logging type to use 121 option - type specific option. This will be interpreted differently 122 based on the type. 123 Sets the logging type for the memory tracker. 124 Values currently supported: 125 0: if option is NULL, log to stderr, otherwise interpret option as a 126 filename and attempt to open it. 127 1: Use output_debug_string (WIN32 only), option ignored 128 Return: 129 0: on success 130 -1: if the logging type could not be set, because the value was invalid 131 or because a file could not be opened 132 */ 133 int vpx_memory_tracker_set_log_type(int type, char *option); 134 135 /* 136 vpx_memory_tracker_set_log_func 137 userdata - ptr to be passed to the supplied logfunc, can be NULL 138 logfunc - the logging function to be used to output data from 139 vpx_memory_track_dump/check_integrity 140 Sets a logging function to be used by the memory tracker. 141 Return: 142 0: on success 143 -1: if the logging type could not be set because logfunc was NULL 144 */ 145 int vpx_memory_tracker_set_log_func(void *userdata, 146 void(*logfunc)(void *userdata, 147 const char *fmt, va_list args)); 148 149 /* Wrappers to standard library functions. */ 150 typedef void *(* mem_track_malloc_func)(size_t); 151 typedef void *(* mem_track_calloc_func)(size_t, size_t); 152 typedef void *(* mem_track_realloc_func)(void *, size_t); 153 typedef void (* mem_track_free_func)(void *); 154 typedef void *(* mem_track_memcpy_func)(void *, const void *, size_t); 155 typedef void *(* mem_track_memset_func)(void *, int, size_t); 156 typedef void *(* mem_track_memmove_func)(void *, const void *, size_t); 157 158 /* 159 vpx_memory_tracker_set_functions 160 161 Sets the function pointers for the standard library functions. 162 163 Return: 164 0: on success 165 -1: if the use global function pointers is not set. 166 */ 167 int vpx_memory_tracker_set_functions(mem_track_malloc_func g_malloc_l 168 , mem_track_calloc_func g_calloc_l 169 , mem_track_realloc_func g_realloc_l 170 , mem_track_free_func g_free_l 171 , mem_track_memcpy_func g_memcpy_l 172 , mem_track_memset_func g_memset_l 173 , mem_track_memmove_func g_memmove_l); 174 175 #if defined(__cplusplus) 176 } 177 #endif 178 179 #endif // VPX_MEM_INCLUDE_VPX_MEM_TRACKER_H_ 180