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