1 /*
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.
6 *
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.
11 *
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.
15 */
16
17 /** \file
18 * \ingroup MEM
19 *
20 * Guarded memory allocation, and boundary-write detection.
21 */
22
23 #include "MEM_guardedalloc.h"
24
25 /* to ensure strict conversions */
26 #include "../../source/blender/blenlib/BLI_strict_flags.h"
27
28 #include <assert.h>
29
30 #include "mallocn_intern.h"
31
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";
37 #endif
38
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,
49 size_t alignment,
50 const char *str) = MEM_lockfree_mallocN_aligned;
51 void (*MEM_printmemlist_pydict)(void) = MEM_lockfree_printmemlist_pydict;
52 void (*MEM_printmemlist)(void) = MEM_lockfree_printmemlist;
53 void (*MEM_callbackmemlist)(void (*func)(void *)) = MEM_lockfree_callbackmemlist;
54 void (*MEM_printmemlist_stats)(void) = MEM_lockfree_printmemlist_stats;
55 void (*MEM_set_error_callback)(void (*func)(const char *)) = MEM_lockfree_set_error_callback;
56 bool (*MEM_consistency_check)(void) = MEM_lockfree_consistency_check;
57 void (*MEM_set_memory_debug)(void) = MEM_lockfree_set_memory_debug;
58 size_t (*MEM_get_memory_in_use)(void) = MEM_lockfree_get_memory_in_use;
59 unsigned int (*MEM_get_memory_blocks_in_use)(void) = MEM_lockfree_get_memory_blocks_in_use;
60 void (*MEM_reset_peak_memory)(void) = MEM_lockfree_reset_peak_memory;
61 size_t (*MEM_get_peak_memory)(void) = MEM_lockfree_get_peak_memory;
62
63 #ifndef NDEBUG
64 const char *(*MEM_name_ptr)(void *vmemh) = MEM_lockfree_name_ptr;
65 #endif
66
aligned_malloc(size_t size,size_t alignment)67 void *aligned_malloc(size_t size, size_t alignment)
68 {
69 /* posix_memalign requires alignment to be a multiple of sizeof(void *). */
70 assert(alignment >= ALIGNED_MALLOC_MINIMUM_ALIGNMENT);
71
72 #ifdef _WIN32
73 return _aligned_malloc(size, alignment);
74 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
75 defined(__DragonFly__)
76 void *result;
77
78 if (posix_memalign(&result, alignment, size)) {
79 /* non-zero means allocation error
80 * either no allocation or bad alignment value
81 */
82 return NULL;
83 }
84 return result;
85 #else /* This is for Linux. */
86 return memalign(alignment, size);
87 #endif
88 }
89
aligned_free(void * ptr)90 void aligned_free(void *ptr)
91 {
92 #ifdef _WIN32
93 _aligned_free(ptr);
94 #else
95 free(ptr);
96 #endif
97 }
98
MEM_use_guarded_allocator(void)99 void MEM_use_guarded_allocator(void)
100 {
101 MEM_allocN_len = MEM_guarded_allocN_len;
102 MEM_freeN = MEM_guarded_freeN;
103 MEM_dupallocN = MEM_guarded_dupallocN;
104 MEM_reallocN_id = MEM_guarded_reallocN_id;
105 MEM_recallocN_id = MEM_guarded_recallocN_id;
106 MEM_callocN = MEM_guarded_callocN;
107 MEM_calloc_arrayN = MEM_guarded_calloc_arrayN;
108 MEM_mallocN = MEM_guarded_mallocN;
109 MEM_malloc_arrayN = MEM_guarded_malloc_arrayN;
110 MEM_mallocN_aligned = MEM_guarded_mallocN_aligned;
111 MEM_printmemlist_pydict = MEM_guarded_printmemlist_pydict;
112 MEM_printmemlist = MEM_guarded_printmemlist;
113 MEM_callbackmemlist = MEM_guarded_callbackmemlist;
114 MEM_printmemlist_stats = MEM_guarded_printmemlist_stats;
115 MEM_set_error_callback = MEM_guarded_set_error_callback;
116 MEM_consistency_check = MEM_guarded_consistency_check;
117 MEM_set_memory_debug = MEM_guarded_set_memory_debug;
118 MEM_get_memory_in_use = MEM_guarded_get_memory_in_use;
119 MEM_get_memory_blocks_in_use = MEM_guarded_get_memory_blocks_in_use;
120 MEM_reset_peak_memory = MEM_guarded_reset_peak_memory;
121 MEM_get_peak_memory = MEM_guarded_get_peak_memory;
122
123 #ifndef NDEBUG
124 MEM_name_ptr = MEM_guarded_name_ptr;
125 #endif
126 }
127