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 * Contributor(s): Brecht Van Lommel
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file guardedalloc/intern/mallocn.c
28 * Guarded memory allocation, and boundary-write detection.
31 #include "MEM_guardedalloc.h"
33 /* to ensure strict conversions */
34 #include "../../source/blender/blenlib/BLI_strict_flags.h"
38 #include "mallocn_intern.h"
40 #ifdef WITH_JEMALLOC_CONF
41 /* If jemalloc is used, it reads this global variable and enables background
42 * threads to purge dirty pages. Otherwise we release memory too slowly or not
43 * at all if the thread that did the allocation stays inactive. */
44 const char *malloc_conf = "background_thread:true,dirty_decay_ms:4000";
47 size_t (*MEM_allocN_len)(const void *vmemh) = MEM_lockfree_allocN_len;
48 void (*MEM_freeN)(void *vmemh) = MEM_lockfree_freeN;
49 void *(*MEM_dupallocN)(const void *vmemh) = MEM_lockfree_dupallocN;
50 void *(*MEM_reallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_reallocN_id;
51 void *(*MEM_recallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_recallocN_id;
52 void *(*MEM_callocN)(size_t len, const char *str) = MEM_lockfree_callocN;
53 void *(*MEM_calloc_arrayN)(size_t len, size_t size, const char *str) = MEM_lockfree_calloc_arrayN;
54 void *(*MEM_mallocN)(size_t len, const char *str) = MEM_lockfree_mallocN;
55 void *(*MEM_malloc_arrayN)(size_t len, size_t size, const char *str) = MEM_lockfree_malloc_arrayN;
56 void *(*MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str) = MEM_lockfree_mallocN_aligned;
57 void *(*MEM_mapallocN)(size_t len, const char *str) = MEM_lockfree_mapallocN;
58 void (*MEM_printmemlist_pydict)(void) = MEM_lockfree_printmemlist_pydict;
59 void (*MEM_printmemlist)(void) = MEM_lockfree_printmemlist;
60 void (*MEM_callbackmemlist)(void (*func)(void *)) = MEM_lockfree_callbackmemlist;
61 void (*MEM_printmemlist_stats)(void) = MEM_lockfree_printmemlist_stats;
62 void (*MEM_set_error_callback)(void (*func)(const char *)) = MEM_lockfree_set_error_callback;
63 bool (*MEM_consistency_check)(void) = MEM_lockfree_consistency_check;
64 void (*MEM_set_lock_callback)(void (*lock)(void), void (*unlock)(void)) = MEM_lockfree_set_lock_callback;
65 void (*MEM_set_memory_debug)(void) = MEM_lockfree_set_memory_debug;
66 size_t (*MEM_get_memory_in_use)(void) = MEM_lockfree_get_memory_in_use;
67 size_t (*MEM_get_mapped_memory_in_use)(void) = MEM_lockfree_get_mapped_memory_in_use;
68 unsigned int (*MEM_get_memory_blocks_in_use)(void) = MEM_lockfree_get_memory_blocks_in_use;
69 void (*MEM_reset_peak_memory)(void) = MEM_lockfree_reset_peak_memory;
70 size_t (*MEM_get_peak_memory)(void) = MEM_lockfree_get_peak_memory;
73 const char *(*MEM_name_ptr)(void *vmemh) = MEM_lockfree_name_ptr;
76 void *aligned_malloc(size_t size, size_t alignment)
79 return _aligned_malloc(size, alignment);
80 #elif defined(__APPLE__)
81 /* On Mac OS X, both the heap and the stack are guaranteed 16-byte aligned so
82 * they work natively with SSE types with no further work.
84 assert(alignment == 16);
87 #elif defined(__FreeBSD__) || defined(__NetBSD__)
90 if (posix_memalign(&result, alignment, size)) {
91 /* non-zero means allocation error
92 * either no allocation or bad alignment value
97 #else /* This is for Linux. */
98 return memalign(alignment, size);
102 void aligned_free(void *ptr)
111 void MEM_use_guarded_allocator(void)
113 MEM_allocN_len = MEM_guarded_allocN_len;
114 MEM_freeN = MEM_guarded_freeN;
115 MEM_dupallocN = MEM_guarded_dupallocN;
116 MEM_reallocN_id = MEM_guarded_reallocN_id;
117 MEM_recallocN_id = MEM_guarded_recallocN_id;
118 MEM_callocN = MEM_guarded_callocN;
119 MEM_calloc_arrayN = MEM_guarded_calloc_arrayN;
120 MEM_mallocN = MEM_guarded_mallocN;
121 MEM_malloc_arrayN = MEM_guarded_malloc_arrayN;
122 MEM_mallocN_aligned = MEM_guarded_mallocN_aligned;
123 MEM_mapallocN = MEM_guarded_mapallocN;
124 MEM_printmemlist_pydict = MEM_guarded_printmemlist_pydict;
125 MEM_printmemlist = MEM_guarded_printmemlist;
126 MEM_callbackmemlist = MEM_guarded_callbackmemlist;
127 MEM_printmemlist_stats = MEM_guarded_printmemlist_stats;
128 MEM_set_error_callback = MEM_guarded_set_error_callback;
129 MEM_consistency_check = MEM_guarded_consistency_check;
130 MEM_set_lock_callback = MEM_guarded_set_lock_callback;
131 MEM_set_memory_debug = MEM_guarded_set_memory_debug;
132 MEM_get_memory_in_use = MEM_guarded_get_memory_in_use;
133 MEM_get_mapped_memory_in_use = MEM_guarded_get_mapped_memory_in_use;
134 MEM_get_memory_blocks_in_use = MEM_guarded_get_memory_blocks_in_use;
135 MEM_reset_peak_memory = MEM_guarded_reset_peak_memory;
136 MEM_get_peak_memory = MEM_guarded_get_peak_memory;
139 MEM_name_ptr = MEM_guarded_name_ptr;