vpx_mem.h revision b08e2e23eec181e9951df33cd704ac294c5407b6
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_VPX_MEM_H_
13#define VPX_MEM_VPX_MEM_H_
14
15#include "vpx_config.h"
16#if defined(__uClinux__)
17# include <lddk.h>
18#endif
19
20/* vpx_mem version info */
21#define vpx_mem_version "2.2.1.5"
22
23#define VPX_MEM_VERSION_CHIEF 2
24#define VPX_MEM_VERSION_MAJOR 2
25#define VPX_MEM_VERSION_MINOR 1
26#define VPX_MEM_VERSION_PATCH 5
27/* end - vpx_mem version info */
28
29#ifndef VPX_TRACK_MEM_USAGE
30# define VPX_TRACK_MEM_USAGE       0  /* enable memory tracking/integrity checks */
31#endif
32#ifndef VPX_CHECK_MEM_FUNCTIONS
33# define VPX_CHECK_MEM_FUNCTIONS   0  /* enable basic safety checks in _memcpy,
34_memset, and _memmove */
35#endif
36#ifndef REPLACE_BUILTIN_FUNCTIONS
37# define REPLACE_BUILTIN_FUNCTIONS 0  /* replace builtin functions with their
38vpx_ equivalents */
39#endif
40
41#include <stdlib.h>
42#include <stddef.h>
43
44#if defined(__cplusplus)
45extern "C" {
46#endif
47
48  /*
49      vpx_mem_get_version()
50      provided for runtime version checking. Returns an unsigned int of the form
51      CHIEF | MAJOR | MINOR | PATCH, where the chief version number is the high
52      order byte.
53  */
54  unsigned int vpx_mem_get_version(void);
55
56  /*
57      vpx_mem_set_heap_size(size_t size)
58        size - size in bytes for the memory manager to allocate for its heap
59      Sets the memory manager's initial heap size
60      Return:
61        0: on success
62        -1: if memory manager calls have not been included in the vpx_mem lib
63        -2: if the memory manager has been compiled to use static memory
64        -3: if the memory manager has already allocated its heap
65  */
66  int vpx_mem_set_heap_size(size_t size);
67
68  void *vpx_memalign(size_t align, size_t size);
69  void *vpx_malloc(size_t size);
70  void *vpx_calloc(size_t num, size_t size);
71  void *vpx_realloc(void *memblk, size_t size);
72  void vpx_free(void *memblk);
73
74  void *vpx_memcpy(void *dest, const void *src, size_t length);
75  void *vpx_memset(void *dest, int val, size_t length);
76  void *vpx_memmove(void *dest, const void *src, size_t count);
77
78  /* special memory functions */
79  void *vpx_mem_alloc(int id, size_t size, size_t align);
80  void vpx_mem_free(int id, void *mem, size_t size);
81
82  /* Wrappers to standard library functions. */
83  typedef void *(* g_malloc_func)(size_t);
84  typedef void *(* g_calloc_func)(size_t, size_t);
85  typedef void *(* g_realloc_func)(void *, size_t);
86  typedef void (* g_free_func)(void *);
87  typedef void *(* g_memcpy_func)(void *, const void *, size_t);
88  typedef void *(* g_memset_func)(void *, int, size_t);
89  typedef void *(* g_memmove_func)(void *, const void *, size_t);
90
91  int vpx_mem_set_functions(g_malloc_func g_malloc_l
92, g_calloc_func g_calloc_l
93, g_realloc_func g_realloc_l
94, g_free_func g_free_l
95, g_memcpy_func g_memcpy_l
96, g_memset_func g_memset_l
97, g_memmove_func g_memmove_l);
98  int vpx_mem_unset_functions(void);
99
100
101  /* some defines for backward compatibility */
102#define DMEM_GENERAL 0
103
104// (*)<
105
106#if REPLACE_BUILTIN_FUNCTIONS
107# ifndef __VPX_MEM_C__
108#  define memalign vpx_memalign
109#  define malloc   vpx_malloc
110#  define calloc   vpx_calloc
111#  define realloc  vpx_realloc
112#  define free     vpx_free
113#  define memcpy   vpx_memcpy
114#  define memmove  vpx_memmove
115#  define memset   vpx_memset
116# endif
117#endif
118
119#if CONFIG_MEM_TRACKER
120#include <stdarg.h>
121  /*from vpx_mem/vpx_mem_tracker.c*/
122  extern void vpx_memory_tracker_dump();
123  extern void vpx_memory_tracker_check_integrity(char *file, unsigned int line);
124  extern int vpx_memory_tracker_set_log_type(int type, char *option);
125  extern int vpx_memory_tracker_set_log_func(void *userdata,
126                                             void(*logfunc)(void *userdata,
127                                                            const char *fmt, va_list args));
128# ifndef __VPX_MEM_C__
129#  define vpx_memalign(align, size) xvpx_memalign((align), (size), __FILE__, __LINE__)
130#  define vpx_malloc(size)          xvpx_malloc((size), __FILE__, __LINE__)
131#  define vpx_calloc(num, size)     xvpx_calloc(num, size, __FILE__, __LINE__)
132#  define vpx_realloc(addr, size)   xvpx_realloc(addr, size, __FILE__, __LINE__)
133#  define vpx_free(addr)            xvpx_free(addr, __FILE__, __LINE__)
134#  define vpx_memory_tracker_check_integrity() vpx_memory_tracker_check_integrity(__FILE__, __LINE__)
135#  define vpx_mem_alloc(id,size,align) xvpx_mem_alloc(id, size, align, __FILE__, __LINE__)
136#  define vpx_mem_free(id,mem,size) xvpx_mem_free(id, mem, size, __FILE__, __LINE__)
137# endif
138
139  void *xvpx_memalign(size_t align, size_t size, char *file, int line);
140  void *xvpx_malloc(size_t size, char *file, int line);
141  void *xvpx_calloc(size_t num, size_t size, char *file, int line);
142  void *xvpx_realloc(void *memblk, size_t size, char *file, int line);
143  void xvpx_free(void *memblk, char *file, int line);
144  void *xvpx_mem_alloc(int id, size_t size, size_t align, char *file, int line);
145  void xvpx_mem_free(int id, void *mem, size_t size, char *file, int line);
146
147#else
148# ifndef __VPX_MEM_C__
149#  define vpx_memory_tracker_dump()
150#  define vpx_memory_tracker_check_integrity()
151#  define vpx_memory_tracker_set_log_type(t,o) 0
152#  define vpx_memory_tracker_set_log_func(u,f) 0
153# endif
154#endif
155
156#if !VPX_CHECK_MEM_FUNCTIONS
157# ifndef __VPX_MEM_C__
158#  include <string.h>
159#  define vpx_memcpy  memcpy
160#  define vpx_memset  memset
161#  define vpx_memmove memmove
162# endif
163#endif
164
165#ifdef VPX_MEM_PLTFRM
166# include VPX_MEM_PLTFRM
167#endif
168
169#if defined(__cplusplus)
170}
171#endif
172
173#endif  // VPX_MEM_VPX_MEM_H_
174