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
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))
78 /** Returns the length of the allocated memory segment pointed at
79 * by vmemh. If the pointer was not previously allocated by this
80 * module, the result is undefined.*/
81 size_t MEM_allocN_len(void *vmemh) WARN_UNUSED;
84 * Release memory previously allocatred by this module.
86 short MEM_freeN(void *vmemh);
90 * Return zero if memory is not in allocated list
92 short MEM_testN(void *vmemh);
95 * Duplicates a block of memory, and returns a pointer to the
96 * newly allocated block. */
97 void *MEM_dupallocN(void *vmemh) WARN_UNUSED;
100 * Reallocates a block of memory, and returns pointer to the newly
101 * allocated block, the old one is freed. this is not as optimized
102 * as a system realloc but just makes a new allocation and copies
103 * over from existing memory. */
104 void *MEM_reallocN(void *vmemh, size_t len) WARN_UNUSED;
107 * Allocate a block of memory of size len, with tag name str. The
108 * memory is cleared. The name must be static, because only a
109 * pointer to it is stored ! */
110 void *MEM_callocN(size_t len, const char * str) WARN_UNUSED;
112 /** Allocate a block of memory of size len, with tag name str. The
113 * name must be a static, because only a pointer to it is stored !
115 void *MEM_mallocN(size_t len, const char * str) WARN_UNUSED;
117 /** Same as callocN, clears memory and uses mmap (disk cached) if supported.
118 Can be free'd with MEM_freeN as usual.
120 void *MEM_mapallocN(size_t len, const char * str) WARN_UNUSED;
122 /** Print a list of the names and sizes of all allocated memory
123 * blocks. as a python dict for easy investigation */
124 void MEM_printmemlist_pydict(void);
126 /** Print a list of the names and sizes of all allocated memory
128 void MEM_printmemlist(void);
130 /** calls the function on all allocated memory blocks. */
131 void MEM_callbackmemlist(void (*func)(void*));
133 /** Print statistics about memory usage */
134 void MEM_printmemlist_stats(void);
136 /** Set the callback function for error output. */
137 void MEM_set_error_callback(void (*func)(const char *));
140 * Are the start/end block markers still correct ?
142 * @retval 0 for correct memory, 1 for corrupted memory. */
143 int MEM_check_memory_integrity(void);
145 /** Set thread locking functions for safe memory allocation from multiple
146 threads, pass NULL pointers to disable thread locking again. */
147 void MEM_set_lock_callback(void (*lock)(void), void (*unlock)(void));
149 /** Attempt to enforce OSX (or other OS's) to have malloc and stack nonzero */
150 void MEM_set_memory_debug(void);
152 /** Memory usage stats
153 * - MEM_get_memory_in_use is all memory
154 * - MEM_get_mapped_memory_in_use is a subset of all memory */
155 uintptr_t MEM_get_memory_in_use(void);
156 /** Get mapped memory usage. */
157 uintptr_t MEM_get_mapped_memory_in_use(void);
158 /** Get amount of memory blocks in use. */
159 int MEM_get_memory_blocks_in_use(void);
161 /** Reset the peak memory statistic to zero. */
162 void MEM_reset_peak_memory(void);
164 /** Get the peak memory usage in bytes, including mmap allocations. */
165 uintptr_t MEM_get_peak_memory(void) WARN_UNUSED;
168 const char *MEM_name_ptr(void *vmemh);