2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
16 Stores a list of addreses, their size, and file and line they came from.
17 All exposed lib functions are prefaced by vpx_ and allow the global list
19 Current supported platforms are:
20 Linux, Win32, win_ce and vx_works
21 Further support can be added by defining the platform specific mutex
22 in the memory_tracker struct as well as calls to create/destroy/lock/unlock
23 the mutex in vpx_memory_tracker_init/Destroy and memory_tracker_lock_mutex/unlock_mutex
25 #include "vpx_ports/config.h"
27 #if defined(__uClinux__)
33 #elif defined(WIN32) || defined(_WIN32_WCE)
34 # define WIN32_LEAN_AND_MEAN
37 #elif defined(VXWORKS)
43 #include <string.h> //VXWORKS doesn't have a malloc/memory.h file,
44 //this should pull in malloc,free,etc.
47 #include "include/vpx_mem_tracker.h"
49 #undef vpx_malloc //undefine any vpx_mem macros that may affect calls to
50 #undef vpx_free //memory functions in this file
55 #ifndef USE_GLOBAL_FUNCTION_POINTERS
56 # define USE_GLOBAL_FUNCTION_POINTERS 0 //use function pointers instead of compiled functions.
59 #if USE_GLOBAL_FUNCTION_POINTERS
60 static mem_track_malloc_func g_malloc
= malloc
;
61 static mem_track_calloc_func g_calloc
= calloc
;
62 static mem_track_realloc_func g_realloc
= realloc
;
63 static mem_track_free_func g_free
= free
;
64 static mem_track_memcpy_func g_memcpy
= memcpy
;
65 static mem_track_memset_func g_memset
= memset
;
66 static mem_track_memmove_func g_memmove
= memmove
;
67 # define MEM_TRACK_MALLOC g_malloc
68 # define MEM_TRACK_FREE g_free
69 # define MEM_TRACK_MEMCPY g_memcpy
70 # define MEM_TRACK_MEMSET g_memset
72 # define MEM_TRACK_MALLOC vpx_malloc
73 # define MEM_TRACK_FREE vpx_free
74 # define MEM_TRACK_MEMCPY vpx_memcpy
75 # define MEM_TRACK_MEMSET vpx_memset
76 #endif // USE_GLOBAL_FUNCTION_POINTERS
78 /* prototypes for internal library functions */
79 static void memtrack_log(const char *fmt
, ...);
80 static void memory_tracker_dump();
81 static void memory_tracker_check_integrity(char *file
, unsigned int line
);
82 static void memory_tracker_add(size_t addr
, unsigned int size
,
83 char *file
, unsigned int line
,
85 static int memory_tracker_remove(size_t addr
);
86 static struct mem_block
*memory_tracker_find(size_t addr
);
89 # define memory_tracker_lock_mutex() (!g_b_mem_tracker_inited)
90 # define memory_tracker_unlock_mutex()
92 static int memory_tracker_lock_mutex();
93 static int memory_tracker_unlock_mutex();
96 #ifndef VPX_NO_GLOBALS
99 struct mem_block
*head
,
103 unsigned int current_allocated
,
107 pthread_mutex_t mutex
;
108 #elif defined(WIN32) || defined(_WIN32_WCE)
110 #elif defined(VXWORKS)
112 #elif defined(NO_MUTEX)
114 #error "No mutex type defined for this platform!"
121 static struct memory_tracker memtrack
; //our global memory allocation list
122 static int g_b_mem_tracker_inited
= 0; //indicates whether the global list has
123 //been initialized (1:yes/0:no)
128 void (*func
)(void *userdata
, const char *fmt
, va_list args
);
130 } g_logging
= {NULL
, 0, NULL
, NULL
};
132 # include "vpx_global_handling.h"
133 #define g_b_mem_tracker_inited vpxglobalm(vpxmem,g_b_mem_tracker_inited)
134 #define g_logging vpxglobalm(vpxmem,g_logging)
135 #define memtrack vpxglobalm(vpxmem,memtrack)
136 #endif // #ifndef VPX_NO_GLOBALS
138 extern void *vpx_malloc(size_t size
);
139 extern void vpx_free(void *memblk
);
140 extern void *vpx_memcpy(void *dest
, const void *src
, size_t length
);
141 extern void *vpx_memset(void *dest
, int val
, size_t length
);
145 * Exposed library functions
150 vpx_memory_tracker_init(int padding_size, int pad_value)
151 padding_size - the size of the padding before and after each mem addr.
152 Values > 0 indicate that integrity checks can be performed
153 by inspecting these areas.
154 pad_value - the initial value within the padding area before and after
157 Initializes global memory tracker structure
158 Allocates the head of the list
160 int vpx_memory_tracker_init(int padding_size
, int pad_value
)
162 if (!g_b_mem_tracker_inited
)
164 if ((memtrack
.head
= (struct mem_block
*)
165 MEM_TRACK_MALLOC(sizeof(struct mem_block
))))
169 MEM_TRACK_MEMSET(memtrack
.head
, 0, sizeof(struct mem_block
));
171 memtrack
.tail
= memtrack
.head
;
173 memtrack
.current_allocated
= 0;
174 memtrack
.max_allocated
= 0;
176 memtrack
.padding_size
= padding_size
;
177 memtrack
.pad_value
= pad_value
;
180 ret
= pthread_mutex_init(&memtrack
.mutex
,
181 NULL
); /*mutex attributes (NULL=default)*/
182 #elif defined(WIN32) || defined(_WIN32_WCE)
183 memtrack
.mutex
= CreateMutex(NULL
, /*security attributes*/
184 FALSE
, /*we don't want initial ownership*/
185 NULL
); /*mutex name*/
186 ret
= !memtrack
.mutex
;
187 #elif defined(VXWORKS)
188 memtrack
.mutex
= sem_bcreate(SEM_Q_FIFO
, /*SEM_Q_FIFO non-priority based mutex*/
189 SEM_FULL
); /*SEM_FULL initial state is unlocked*/
190 ret
= !memtrack
.mutex
;
191 #elif defined(NO_MUTEX)
197 memtrack_log("vpx_memory_tracker_init: Error creating mutex!\n");
199 MEM_TRACK_FREE(memtrack
.head
);
200 memtrack
.head
= NULL
;
204 memtrack_log("Memory Tracker init'd, v."vpx_mem_tracker_version
" pad_size:%d pad_val:0x%x %d\n"
208 g_b_mem_tracker_inited
= 1;
213 return g_b_mem_tracker_inited
;
217 vpx_memory_tracker_destroy()
218 If our global struct was initialized zeros out all its members,
219 frees memory and destroys it's mutex
221 void vpx_memory_tracker_destroy()
223 if (!memory_tracker_lock_mutex())
225 struct mem_block
*p
= memtrack
.head
,
226 * p2
= memtrack
.head
;
228 memory_tracker_dump();
238 memtrack
.head
= NULL
;
239 memtrack
.tail
= NULL
;
241 memtrack
.current_allocated
= 0;
242 memtrack
.max_allocated
= 0;
244 if (!g_logging
.type
&& g_logging
.file
&& g_logging
.file
!= stderr
)
246 fclose(g_logging
.file
);
247 g_logging
.file
= NULL
;
250 memory_tracker_unlock_mutex();
252 g_b_mem_tracker_inited
= 0;
257 vpx_memory_tracker_add(size_t addr, unsigned int size,
258 char * file, unsigned int line)
259 addr - memory address to be added to list
261 file - the file addr was referenced from
262 line - the line in file addr was referenced from
263 Adds memory address addr, it's size, file and line it came from
264 to the global list via the thread safe internal library function
266 void vpx_memory_tracker_add(size_t addr
, unsigned int size
,
267 char *file
, unsigned int line
,
270 memory_tracker_add(addr
, size
, file
, line
, padded
);
274 vpx_memory_tracker_remove(size_t addr)
275 addr - memory address to be removed from list
276 Removes addr from the global list via the thread safe
277 internal remove function
279 Same as described for memory_tracker_remove
281 int vpx_memory_tracker_remove(size_t addr
)
283 return memory_tracker_remove(addr
);
287 vpx_memory_tracker_find(size_t addr)
288 addr - address to be found in list
290 If found, pointer to the memory block that matches addr
293 struct mem_block
*vpx_memory_tracker_find(size_t addr
)
295 struct mem_block
*p
= NULL
;
297 if (!memory_tracker_lock_mutex())
299 p
= memory_tracker_find(addr
);
300 memory_tracker_unlock_mutex();
307 vpx_memory_tracker_dump()
308 Locks the memory tracker's mutex and calls the internal
309 library function to dump the current contents of the
310 global memory allocation list
312 void vpx_memory_tracker_dump()
314 if (!memory_tracker_lock_mutex())
316 memory_tracker_dump();
317 memory_tracker_unlock_mutex();
322 vpx_memory_tracker_check_integrity(char* file, unsigned int line)
323 file - The file name where the check was placed
324 line - The line in file where the check was placed
325 Locks the memory tracker's mutex and calls the internal
326 integrity check function to inspect every address in the global
327 memory allocation list
329 void vpx_memory_tracker_check_integrity(char *file
, unsigned int line
)
331 if (!memory_tracker_lock_mutex())
333 memory_tracker_check_integrity(file
, line
);
334 memory_tracker_unlock_mutex();
339 vpx_memory_tracker_set_log_type
340 Sets the logging type for the memory tracker. Based on the value it will
341 direct its output to the appropriate place.
344 -1: if the logging type could not be set, because the value was invalid
345 or because a file could not be opened
347 int vpx_memory_tracker_set_log_type(int type
, char *option
)
358 g_logging
.file
= stderr
;
363 if ((g_logging
.file
= fopen((char *)option
, "w")))
368 #if defined(WIN32) && !defined(_WIN32_WCE)
370 g_logging
.type
= type
;
378 //output the version to the new logging destination
380 memtrack_log("Memory Tracker logging initialized, "
381 "Memory Tracker v."vpx_mem_tracker_version
"\n");
387 vpx_memory_tracker_set_log_func
388 Sets a logging function to be used by the memory tracker.
391 -1: if the logging type could not be set because logfunc was NULL
393 int vpx_memory_tracker_set_log_func(void *userdata
,
394 void(*logfunc
)(void *userdata
,
395 const char *fmt
, va_list args
))
402 g_logging
.userdata
= userdata
;
403 g_logging
.func
= logfunc
;
407 //output the version to the new logging destination
409 memtrack_log("Memory Tracker logging initialized, "
410 "Memory Tracker v."vpx_mem_tracker_version
"\n");
417 * END - Exposed library functions
424 * Internal library functions
428 static void memtrack_log(const char *fmt
, ...)
434 switch (g_logging
.type
)
439 g_logging
.func(g_logging
.userdata
, fmt
, list
);
446 vfprintf(g_logging
.file
, fmt
, list
);
447 fflush(g_logging
.file
);
451 #if defined(WIN32) && !defined(_WIN32_WCE)
455 _vsnprintf(temp
, sizeof(temp
) / sizeof(char) - 1, fmt
, list
);
456 OutputDebugString(temp
);
468 memory_tracker_dump()
469 Dumps the current contents of the global memory allocation list
471 static void memory_tracker_dump()
474 struct mem_block
*p
= (memtrack
.head
? memtrack
.head
->next
: NULL
);
476 memtrack_log("\n_currently Allocated= %d; Max allocated= %d\n",
477 memtrack
.current_allocated
, memtrack
.max_allocated
);
481 #if defined(WIN32) && !defined(_WIN32_WCE)
483 /*when using outputdebugstring, output filenames so they
484 can be clicked to be opened in visual studio*/
485 if (g_logging
.type
== 1)
486 memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file:\n"
492 memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file: %s, line: %d\n", i
,
504 memory_tracker_check_integrity(char* file, unsigned int file)
505 file - the file name where the check was placed
506 line - the line in file where the check was placed
507 If a padding_size was supplied to vpx_memory_tracker_init()
508 this function will check ea. addr in the list verifying that
509 addr-padding_size and addr+padding_size is filled with pad_value
511 static void memory_tracker_check_integrity(char *file
, unsigned int line
)
513 if (memtrack
.padding_size
)
517 unsigned char *p_show_me
,
519 unsigned int tempme
= memtrack
.pad_value
,
522 unsigned char *x_bounds
;
523 struct mem_block
*p
= memtrack
.head
->next
;
527 //x_bounds = (unsigned char*)p->addr;
528 //back up VPX_BYTE_ALIGNMENT
529 //x_bounds -= memtrack.padding_size;
531 if (p
->padded
) // can the bounds be checked?
533 /*yes, move to the address that was actually allocated
535 x_bounds
= (unsigned char *)(((size_t *)p
->addr
)[-1]);
537 for (i
= 0; i
< memtrack
.padding_size
; i
+= sizeof(unsigned int))
539 p_show_me
= (x_bounds
+ i
);
540 p_show_me2
= (unsigned char *)(p
->addr
+ p
->size
+ i
);
542 MEM_TRACK_MEMCPY(&dead1
, p_show_me
, sizeof(unsigned int));
543 MEM_TRACK_MEMCPY(&dead2
, p_show_me2
, sizeof(unsigned int));
545 if ((dead1
!= tempme
) || (dead2
!= tempme
))
547 memtrack_log("\n[vpx_mem integrity check failed]:\n"
548 " index[%d,%d] {%s:%d} addr=0x%x, size=%d,"
549 " file: %s, line: %d c0:0x%x c1:0x%x\n",
550 index
, i
, file
, line
, p
->addr
, p
->size
, p
->file
,
551 p
->line
, dead1
, dead2
);
563 memory_tracker_add(size_t addr, unsigned int size,
564 char * file, unsigned int line)
565 Adds an address (addr), it's size, file and line number to our list.
566 Adjusts the total bytes allocated and max bytes allocated if necessary.
567 If memory cannot be allocated the list will be destroyed.
569 void memory_tracker_add(size_t addr
, unsigned int size
,
570 char *file
, unsigned int line
,
573 if (!memory_tracker_lock_mutex())
577 p
= MEM_TRACK_MALLOC(sizeof(struct mem_block
));
581 p
->prev
= memtrack
.tail
;
592 memtrack
.current_allocated
+= size
;
594 if (memtrack
.current_allocated
> memtrack
.max_allocated
)
595 memtrack
.max_allocated
= memtrack
.current_allocated
;
597 //memtrack_log("memory_tracker_add: added addr=0x%.8x\n", addr);
599 memory_tracker_unlock_mutex();
603 memtrack_log("memory_tracker_add: error allocating memory!\n");
604 memory_tracker_unlock_mutex();
605 vpx_memory_tracker_destroy();
611 memory_tracker_remove(size_t addr)
612 Removes an address and its corresponding size (if they exist)
613 from the memory tracker list and adjusts the current number
617 -1: if the mutex could not be locked
618 -2: if the addr was not found in the list
620 int memory_tracker_remove(size_t addr
)
624 if (!memory_tracker_lock_mutex())
628 if ((p
= memory_tracker_find(addr
)))
630 memtrack
.current_allocated
-= p
->size
;
632 p
->prev
->next
= p
->next
;
635 p
->next
->prev
= p
->prev
;
637 memtrack
.tail
= p
->prev
;
645 memtrack_log("memory_tracker_remove(): addr not found in list,"
651 memory_tracker_unlock_mutex();
658 memory_tracker_find(size_t addr)
659 Finds an address in our addrs list
660 NOTE: the mutex MUST be locked in the other internal
661 functions before calling this one. This avoids
662 the need for repeated locking and unlocking as in Remove
663 Returns: pointer to the mem block if found, NULL otherwise
665 static struct mem_block
*memory_tracker_find(size_t addr
)
667 struct mem_block
*p
= NULL
;
671 p
= memtrack
.head
->next
;
673 while (p
&& (p
->addr
!= addr
))
681 #if !defined(NO_MUTEX)
683 memory_tracker_lock_mutex()
684 Locks the memory tracker mutex with a platform specific call
687 <0: Failure, either the mutex was not initialized
688 or the call to lock the mutex failed
690 static int memory_tracker_lock_mutex()
694 if (g_b_mem_tracker_inited
)
698 ret
= pthread_mutex_lock(&memtrack
.mutex
);
699 #elif defined(WIN32) || defined(_WIN32_WCE)
700 ret
= WaitForSingleObject(memtrack
.mutex
, INFINITE
);
701 #elif defined(VXWORKS)
702 ret
= sem_take(memtrack
.mutex
, WAIT_FOREVER
);
707 memtrack_log("memory_tracker_lock_mutex: mutex lock failed\n");
715 memory_tracker_unlock_mutex()
716 Unlocks the memory tracker mutex with a platform specific call
719 <0: Failure, either the mutex was not initialized
720 or the call to unlock the mutex failed
722 static int memory_tracker_unlock_mutex()
726 if (g_b_mem_tracker_inited
)
730 ret
= pthread_mutex_unlock(&memtrack
.mutex
);
731 #elif defined(WIN32) || defined(_WIN32_WCE)
732 ret
= !ReleaseMutex(memtrack
.mutex
);
733 #elif defined(VXWORKS)
734 ret
= sem_give(memtrack
.mutex
);
739 memtrack_log("memory_tracker_unlock_mutex: mutex unlock failed\n");
748 vpx_memory_tracker_set_functions
750 Sets the function pointers for the standard library functions.
754 -1: if the use global function pointers is not set.
756 int vpx_memory_tracker_set_functions(mem_track_malloc_func g_malloc_l
757 , mem_track_calloc_func g_calloc_l
758 , mem_track_realloc_func g_realloc_l
759 , mem_track_free_func g_free_l
760 , mem_track_memcpy_func g_memcpy_l
761 , mem_track_memset_func g_memset_l
762 , mem_track_memmove_func g_memmove_l
)
764 #if USE_GLOBAL_FUNCTION_POINTERS
767 g_malloc
= g_malloc_l
;
770 g_calloc
= g_calloc_l
;
773 g_realloc
= g_realloc_l
;
779 g_memcpy
= g_memcpy_l
;
782 g_memset
= g_memset_l
;
785 g_memmove
= g_memmove_l
;