2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file MEM_guardedalloc.h
31 * \author Copyright (C) 2001 NaN Technologies B.V.
32 * \brief Read \ref MEMPage
36 * \page MEMPage Guarded memory(de)allocation
38 * \section aboutmem c-style guarded memory allocation
40 * \subsection memabout About the MEM module
42 * MEM provides guarded malloc/calloc calls. All memory is enclosed by
43 * pads, to detect out-of-bound writes. All blocks are placed in a
44 * linked list, so they remain reachable at all times. There is no
45 * back-up in case the linked-list related data is lost.
47 * \subsection memissues Known issues with MEM
49 * There are currently no known issues with MEM. Note that there is a
50 * second intern/ module with MEM_ prefix, for use in c++.
52 * \subsection memdependencies Dependencies
56 * \subsection memdocs API Documentation
57 * See \ref MEM_guardedalloc.h
60 #ifndef __MEM_GUARDEDALLOC_H__
61 #define __MEM_GUARDEDALLOC_H__
63 #include <stdio.h> /* needed for FILE* */
64 #include "MEM_sys_types.h" /* needed for uintptr_t */
68 # define WARN_UNUSED __attribute__((warn_unused_result))
76 # define ALLOC_SIZE(arg_pos) __attribute__((alloc_size(arg_pos)))
78 # define ALLOC_SIZE(arg_pos)
86 /** Returns the length of the allocated memory segment pointed at
87 * by vmemh. If the pointer was not previously allocated by this
88 * module, the result is undefined.*/
89 size_t MEM_allocN_len(void *vmemh) WARN_UNUSED;
92 * Release memory previously allocatred by this module.
94 short MEM_freeN(void *vmemh);
98 * Return zero if memory is not in allocated list
100 short MEM_testN(void *vmemh);
103 * Duplicates a block of memory, and returns a pointer to the
104 * newly allocated block. */
105 void *MEM_dupallocN(void *vmemh) WARN_UNUSED;
108 * Reallocates a block of memory, and returns pointer to the newly
109 * allocated block, the old one is freed. this is not as optimized
110 * as a system realloc but just makes a new allocation and copies
111 * over from existing memory. */
112 void *MEM_reallocN(void *vmemh, size_t len) WARN_UNUSED ALLOC_SIZE(2);
115 * Allocate a block of memory of size len, with tag name str. The
116 * memory is cleared. The name must be static, because only a
117 * pointer to it is stored ! */
118 void *MEM_callocN(size_t len, const char * str) WARN_UNUSED ALLOC_SIZE(1);
120 /** Allocate a block of memory of size len, with tag name str. The
121 * name must be a static, because only a pointer to it is stored !
123 void *MEM_mallocN(size_t len, const char * str) WARN_UNUSED ALLOC_SIZE(1);
125 /** Same as callocN, clears memory and uses mmap (disk cached) if supported.
126 * Can be free'd with MEM_freeN as usual.
128 void *MEM_mapallocN(size_t len, const char * str) WARN_UNUSED ALLOC_SIZE(1);
130 /** Print a list of the names and sizes of all allocated memory
131 * blocks. as a python dict for easy investigation */
132 void MEM_printmemlist_pydict(void);
134 /** Print a list of the names and sizes of all allocated memory
136 void MEM_printmemlist(void);
138 /** calls the function on all allocated memory blocks. */
139 void MEM_callbackmemlist(void (*func)(void*));
141 /** Print statistics about memory usage */
142 void MEM_printmemlist_stats(void);
144 /** Set the callback function for error output. */
145 void MEM_set_error_callback(void (*func)(const char *));
148 * Are the start/end block markers still correct ?
150 * @retval 0 for correct memory, 1 for corrupted memory. */
151 int MEM_check_memory_integrity(void);
153 /** Set thread locking functions for safe memory allocation from multiple
154 * threads, pass NULL pointers to disable thread locking again. */
155 void MEM_set_lock_callback(void (*lock)(void), void (*unlock)(void));
157 /** Attempt to enforce OSX (or other OS's) to have malloc and stack nonzero */
158 void MEM_set_memory_debug(void);
160 /** Memory usage stats
161 * - MEM_get_memory_in_use is all memory
162 * - MEM_get_mapped_memory_in_use is a subset of all memory */
163 uintptr_t MEM_get_memory_in_use(void);
164 /** Get mapped memory usage. */
165 uintptr_t MEM_get_mapped_memory_in_use(void);
166 /** Get amount of memory blocks in use. */
167 int MEM_get_memory_blocks_in_use(void);
169 /** Reset the peak memory statistic to zero. */
170 void MEM_reset_peak_memory(void);
172 /** Get the peak memory usage in bytes, including mmap allocations. */
173 uintptr_t MEM_get_peak_memory(void) WARN_UNUSED;
176 const char *MEM_name_ptr(void *vmemh);
180 /* alloc funcs for C++ only */
181 #define MEM_CXX_CLASS_ALLOC_FUNCS(_id) \
183 void *operator new(size_t num_bytes) { \
184 return MEM_mallocN(num_bytes, _id); \
186 void operator delete(void *mem) { \
190 void *operator new[](size_t num_bytes) { \
191 return MEM_mallocN(num_bytes, _id "[]"); \
193 void operator delete[](void *mem) { \