3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
30 * Copyright (C) 2001 NaN Technologies B.V.
31 * Guarded memory (de)allocation
34 * @mainpage MEM - c-style guarded memory allocation
36 * @section about About the MEM module
38 * MEM provides guarded malloc/calloc calls. All memory is enclosed by
39 * pads, to detect out-of-bound writes. All blocks are placed in a
40 * linked list, so they remain reachable at all times. There is no
41 * back-up in case the linked-list related data is lost.
43 * @section issues Known issues with MEM
45 * There are currently no known issues with MEM. Note that there is a
46 * second intern/ module with MEM_ prefix, for use in c++.
48 * @section dependencies Dependencies
59 #include "stdio.h" /* needed for FILE* */
60 #include "BLO_sys_types.h" /* needed for uintptr_t */
66 /** Returns the lenght of the allocated memory segment pointed at
67 * by vmemh. If the pointer was not previously allocated by this
68 * module, the result is undefined.*/
69 int MEM_allocN_len(void *vmemh);
72 * Release memory previously allocatred by this module.
74 short MEM_freeN(void *vmemh);
78 * Return zero if memory is not in allocated list
80 short MEM_testN(void *vmemh);
83 * Duplicates a block of memory, and returns a pointer to the
84 * newly allocated block. */
85 void *MEM_dupallocN(void *vmemh);
88 * Reallocates a block of memory, and returns pointer to the newly
89 * allocated block, the old one is freed. this is not as optimized
90 * as a system realloc but just makes a new allocation and copies
91 * over from existing memory. */
92 void *MEM_reallocN(void *vmemh, unsigned int len);
95 * Allocate a block of memory of size len, with tag name str. The
96 * memory is cleared. The name must be static, because only a
97 * pointer to it is stored ! */
98 void *MEM_callocN(unsigned int len, const char * str);
100 /** Allocate a block of memory of size len, with tag name str. The
101 * name must be a static, because only a pointer to it is stored !
103 void *MEM_mallocN(unsigned int len, const char * str);
105 /** Same as callocN, clears memory and uses mmap (disk cached) if supported.
106 Can be free'd with MEM_freeN as usual.
108 void *MEM_mapallocN(unsigned int len, const char * str);
110 /** Print a list of the names and sizes of all allocated memory
111 * blocks. as a python dict for easy investigation */
112 void MEM_printmemlist_pydict(void);
114 /** Print a list of the names and sizes of all allocated memory
116 void MEM_printmemlist(void);
118 /** calls the function on all allocated memory blocks. */
119 void MEM_callbackmemlist(void (*func)(void*));
121 /** Print statistics about memory usage */
122 void MEM_printmemlist_stats(void);
124 /** Set the callback function for error output. */
125 void MEM_set_error_callback(void (*func)(char *));
128 * Are the start/end block markers still correct ?
130 * @retval 0 for correct memory, 1 for corrupted memory. */
131 int MEM_check_memory_integrity(void);
133 /** Set thread locking functions for safe memory allocation from multiple
134 threads, pass NULL pointers to disable thread locking again. */
135 void MEM_set_lock_callback(void (*lock)(void), void (*unlock)(void));
137 /** Attempt to enforce OSX (or other OS's) to have malloc and stack nonzero */
138 void MEM_set_memory_debug(void);
140 /* Memory usage stats
141 * - MEM_get_memory_in_use is all memory
142 * - MEM_get_mapped_memory_in_use is a subset of all memory */
143 uintptr_t MEM_get_memory_in_use(void);
144 uintptr_t MEM_get_mapped_memory_in_use(void);
145 int MEM_get_memory_blocks_in_use(void);