summaryrefslogtreecommitdiffstats
path: root/libvpx/vpx_mem/include/vpx_mem_tracker.h
blob: 3be0d2ddb0e511838565a5215825a5a035d85f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


#ifndef __VPX_MEM_TRACKER_H__
#define __VPX_MEM_TRACKER_H__

/* vpx_mem_tracker version info */
#define vpx_mem_tracker_version "2.5.1.1"

#define VPX_MEM_TRACKER_VERSION_CHIEF 2
#define VPX_MEM_TRACKER_VERSION_MAJOR 5
#define VPX_MEM_TRACKER_VERSION_MINOR 1
#define VPX_MEM_TRACKER_VERSION_PATCH 1
/* END - vpx_mem_tracker version info */

#include <stdarg.h>

struct mem_block {
  size_t addr;
  unsigned int size,
           line;
  char *file;
  struct mem_block *prev,
      * next;

  int padded; // This mem_block has padding for integrity checks.
  // As of right now, this should only be 0 if
  // using vpx_mem_alloc to allocate cache memory.
  // 2005-01-11 tjf
};

#if defined(__cplusplus)
extern "C" {
#endif

  /*
      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 the memory tracker interface. Should be called before any
      other calls to the memory tracker.
  */
  int vpx_memory_tracker_init(int padding_size, int pad_value);

  /*
      vpx_memory_tracker_destroy()
      Deinitializes the memory tracker interface
  */
  void vpx_memory_tracker_destroy();

  /*
      vpx_memory_tracker_add(size_t addr, unsigned int size,
                           char * file, unsigned int line)
        addr - memory address to be added to list
        size - size of addr
        file - the file addr was referenced from
        line - the line in file addr was referenced from
      Adds memory address addr, it's size, file and line it came from
      to the memory tracker allocation table
  */
  void vpx_memory_tracker_add(size_t addr, unsigned int size,
                              char *file, unsigned int line,
                              int padded);

  /*
      vpx_memory_tracker_add(size_t addr, unsigned int size, char * file, unsigned int line)
        addr - memory address to be added to be removed
        padded - if 0, disables bounds checking on this memory block even if bounds
        checking is enabled. (for example, when allocating cache memory, we still want
        to check for memory leaks, but we do not waste cache space for bounds check padding)
      Removes the specified address from the memory tracker's allocation
      table
      Return:
        0: on success
        -1: if memory allocation table's mutex could not be locked
        -2: if the addr was not found in the list
  */
  int vpx_memory_tracker_remove(size_t addr);

  /*
      vpx_memory_tracker_find(unsigned int addr)
        addr - address to be found in the memory tracker's
               allocation table
      Return:
          If found, pointer to the memory block that matches addr
          NULL otherwise
  */
  struct mem_block *vpx_memory_tracker_find(size_t addr);

  /*
      vpx_memory_tracker_dump()
      Dumps the current contents of the memory
      tracker allocation table
  */
  void vpx_memory_tracker_dump();

  /*
      vpx_memory_tracker_check_integrity()
      If a padding_size was provided to vpx_memory_tracker_init()
      This function will verify that the region before and after each
      memory address contains the specified pad_value. Should the check
      fail, the filename and line of the check will be printed out.
  */
  void vpx_memory_tracker_check_integrity(char *file, unsigned int line);

  /*
      vpx_memory_tracker_set_log_type
        type - value representing the logging type to use
        option - type specific option. This will be interpreted differently
                 based on the type.
      Sets the logging type for the memory tracker.
      Values currently supported:
        0: if option is NULL, log to stderr, otherwise interpret option as a
           filename and attempt to open it.
        1: Use output_debug_string (WIN32 only), option ignored
      Return:
        0: on success
        -1: if the logging type could not be set, because the value was invalid
            or because a file could not be opened
  */
  int vpx_memory_tracker_set_log_type(int type, char *option);

  /*
      vpx_memory_tracker_set_log_func
        userdata - ptr to be passed to the supplied logfunc, can be NULL
        logfunc - the logging function to be used to output data from
                  vpx_memory_track_dump/check_integrity
      Sets a logging function to be used by the memory tracker.
      Return:
        0: on success
        -1: if the logging type could not be set because logfunc was NULL
  */
  int vpx_memory_tracker_set_log_func(void *userdata,
                                      void(*logfunc)(void *userdata,
                                                     const char *fmt, va_list args));

  /* Wrappers to standard library functions. */
  typedef void *(* mem_track_malloc_func)(size_t);
  typedef void *(* mem_track_calloc_func)(size_t, size_t);
  typedef void *(* mem_track_realloc_func)(void *, size_t);
  typedef void (* mem_track_free_func)(void *);
  typedef void *(* mem_track_memcpy_func)(void *, const void *, size_t);
  typedef void *(* mem_track_memset_func)(void *, int, size_t);
  typedef void *(* mem_track_memmove_func)(void *, const void *, size_t);

  /*
      vpx_memory_tracker_set_functions

      Sets the function pointers for the standard library functions.

      Return:
        0: on success
        -1: if the use global function pointers is not set.
  */
  int vpx_memory_tracker_set_functions(mem_track_malloc_func g_malloc_l
, mem_track_calloc_func g_calloc_l
, mem_track_realloc_func g_realloc_l
, mem_track_free_func g_free_l
, mem_track_memcpy_func g_memcpy_l
, mem_track_memset_func g_memset_l
, mem_track_memmove_func g_memmove_l);

#if defined(__cplusplus)
}
#endif

#endif // __VPX_MEM_TRACKER_H__