2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Guarded memory allocation, and boundary-write detection.
23 #include "MEM_guardedalloc.h"
25 /* to ensure strict conversions */
26 #include "../../source/blender/blenlib/BLI_strict_flags.h"
30 #include "mallocn_intern.h"
32 #ifdef WITH_JEMALLOC_CONF
33 /* If jemalloc is used, it reads this global variable and enables background
34 * threads to purge dirty pages. Otherwise we release memory too slowly or not
35 * at all if the thread that did the allocation stays inactive. */
36 const char *malloc_conf = "background_thread:true,dirty_decay_ms:4000";
39 size_t (*MEM_allocN_len)(const void *vmemh) = MEM_lockfree_allocN_len;
40 void (*MEM_freeN)(void *vmemh) = MEM_lockfree_freeN;
41 void *(*MEM_dupallocN)(const void *vmemh) = MEM_lockfree_dupallocN;
42 void *(*MEM_reallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_reallocN_id;
43 void *(*MEM_recallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_recallocN_id;
44 void *(*MEM_callocN)(size_t len, const char *str) = MEM_lockfree_callocN;
45 void *(*MEM_calloc_arrayN)(size_t len, size_t size, const char *str) = MEM_lockfree_calloc_arrayN;
46 void *(*MEM_mallocN)(size_t len, const char *str) = MEM_lockfree_mallocN;
47 void *(*MEM_malloc_arrayN)(size_t len, size_t size, const char *str) = MEM_lockfree_malloc_arrayN;
48 void *(*MEM_mallocN_aligned)(size_t len,
50 const char *str) = MEM_lockfree_mallocN_aligned;
51 void *(*MEM_mapallocN)(size_t len, const char *str) = MEM_lockfree_mapallocN;
52 void (*MEM_printmemlist_pydict)(void) = MEM_lockfree_printmemlist_pydict;
53 void (*MEM_printmemlist)(void) = MEM_lockfree_printmemlist;
54 void (*MEM_callbackmemlist)(void (*func)(void *)) = MEM_lockfree_callbackmemlist;
55 void (*MEM_printmemlist_stats)(void) = MEM_lockfree_printmemlist_stats;
56 void (*MEM_set_error_callback)(void (*func)(const char *)) = MEM_lockfree_set_error_callback;
57 bool (*MEM_consistency_check)(void) = MEM_lockfree_consistency_check;
58 void (*MEM_set_lock_callback)(void (*lock)(void),
59 void (*unlock)(void)) = MEM_lockfree_set_lock_callback;
60 void (*MEM_set_memory_debug)(void) = MEM_lockfree_set_memory_debug;
61 size_t (*MEM_get_memory_in_use)(void) = MEM_lockfree_get_memory_in_use;
62 size_t (*MEM_get_mapped_memory_in_use)(void) = MEM_lockfree_get_mapped_memory_in_use;
63 unsigned int (*MEM_get_memory_blocks_in_use)(void) = MEM_lockfree_get_memory_blocks_in_use;
64 void (*MEM_reset_peak_memory)(void) = MEM_lockfree_reset_peak_memory;
65 size_t (*MEM_get_peak_memory)(void) = MEM_lockfree_get_peak_memory;
68 const char *(*MEM_name_ptr)(void *vmemh) = MEM_lockfree_name_ptr;
71 void *aligned_malloc(size_t size, size_t alignment)
73 /* posix_memalign requires alignment to be a multiple of sizeof(void *). */
74 assert(alignment >= ALIGNED_MALLOC_MINIMUM_ALIGNMENT);
77 return _aligned_malloc(size, alignment);
78 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
81 if (posix_memalign(&result, alignment, size)) {
82 /* non-zero means allocation error
83 * either no allocation or bad alignment value
88 #else /* This is for Linux. */
89 return memalign(alignment, size);
93 void aligned_free(void *ptr)
102 void MEM_use_guarded_allocator(void)
104 MEM_allocN_len = MEM_guarded_allocN_len;
105 MEM_freeN = MEM_guarded_freeN;
106 MEM_dupallocN = MEM_guarded_dupallocN;
107 MEM_reallocN_id = MEM_guarded_reallocN_id;
108 MEM_recallocN_id = MEM_guarded_recallocN_id;
109 MEM_callocN = MEM_guarded_callocN;
110 MEM_calloc_arrayN = MEM_guarded_calloc_arrayN;
111 MEM_mallocN = MEM_guarded_mallocN;
112 MEM_malloc_arrayN = MEM_guarded_malloc_arrayN;
113 MEM_mallocN_aligned = MEM_guarded_mallocN_aligned;
114 MEM_mapallocN = MEM_guarded_mapallocN;
115 MEM_printmemlist_pydict = MEM_guarded_printmemlist_pydict;
116 MEM_printmemlist = MEM_guarded_printmemlist;
117 MEM_callbackmemlist = MEM_guarded_callbackmemlist;
118 MEM_printmemlist_stats = MEM_guarded_printmemlist_stats;
119 MEM_set_error_callback = MEM_guarded_set_error_callback;
120 MEM_consistency_check = MEM_guarded_consistency_check;
121 MEM_set_lock_callback = MEM_guarded_set_lock_callback;
122 MEM_set_memory_debug = MEM_guarded_set_memory_debug;
123 MEM_get_memory_in_use = MEM_guarded_get_memory_in_use;
124 MEM_get_mapped_memory_in_use = MEM_guarded_get_mapped_memory_in_use;
125 MEM_get_memory_blocks_in_use = MEM_guarded_get_memory_blocks_in_use;
126 MEM_reset_peak_memory = MEM_guarded_reset_peak_memory;
127 MEM_get_peak_memory = MEM_guarded_get_peak_memory;
130 MEM_name_ptr = MEM_guarded_name_ptr;