Guardedalloc: Add extra logging and checks in MEM_freeN()
[blender-staging.git] / intern / guardedalloc / intern / mallocn.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Contributor(s): Brecht Van Lommel
19  *                 Campbell Barton
20  *                 Sergey Sharybin
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file guardedalloc/intern/mallocn.c
26  *  \ingroup MEM
27  *
28  * Guarded memory allocation, and boundary-write detection.
29  */
30
31 #include "MEM_guardedalloc.h"
32
33 /* to ensure strict conversions */
34 #include "../../source/blender/blenlib/BLI_strict_flags.h"
35
36 #include <assert.h>
37
38 #include "mallocn_intern.h"
39
40 size_t (*MEM_allocN_len)(const void *vmemh) = MEM_lockfree_allocN_len;
41 void (*MEM_freeN)(void *vmemh) = MEM_lockfree_freeN;
42 void *(*MEM_dupallocN)(const void *vmemh) = MEM_lockfree_dupallocN;
43 void *(*MEM_reallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_reallocN_id;
44 void *(*MEM_recallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_recallocN_id;
45 void *(*MEM_callocN)(size_t len, const char *str) = MEM_lockfree_callocN;
46 void *(*MEM_mallocN)(size_t len, const char *str) = MEM_lockfree_mallocN;
47 void *(*MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str) = MEM_lockfree_mallocN_aligned;
48 void *(*MEM_mapallocN)(size_t len, const char *str) = MEM_lockfree_mapallocN;
49 void (*MEM_printmemlist_pydict)(void) = MEM_lockfree_printmemlist_pydict;
50 void (*MEM_printmemlist)(void) = MEM_lockfree_printmemlist;
51 void (*MEM_callbackmemlist)(void (*func)(void *)) = MEM_lockfree_callbackmemlist;
52 void (*MEM_printmemlist_stats)(void) = MEM_lockfree_printmemlist_stats;
53 void (*MEM_set_error_callback)(void (*func)(const char *)) = MEM_lockfree_set_error_callback;
54 bool (*MEM_check_memory_integrity)(void) = MEM_lockfree_check_memory_integrity;
55 void (*MEM_set_lock_callback)(void (*lock)(void), void (*unlock)(void)) = MEM_lockfree_set_lock_callback;
56 void (*MEM_set_memory_debug)(void) = MEM_lockfree_set_memory_debug;
57 size_t (*MEM_get_memory_in_use)(void) = MEM_lockfree_get_memory_in_use;
58 size_t (*MEM_get_mapped_memory_in_use)(void) = MEM_lockfree_get_mapped_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
67 void *aligned_malloc(size_t size, size_t alignment)
68 {
69 #ifdef _WIN32
70         return _aligned_malloc(size, alignment);
71 #elif defined(__APPLE__)
72         /* On Mac OS X, both the heap and the stack are guaranteed 16-byte aligned so
73          * they work natively with SSE types with no further work.
74          */
75         assert(alignment == 16);
76         (void)alignment;
77         return malloc(size);
78 #elif defined(__FreeBSD__) || defined(__NetBSD__)
79         void *result;
80
81         if (posix_memalign(&result, alignment, size)) {
82                 /* non-zero means allocation error
83                  * either no allocation or bad alignment value
84                  */
85                 return NULL;
86         }
87         return result;
88 #else  /* This is for Linux. */
89         return memalign(alignment, size);
90 #endif
91 }
92
93 void aligned_free(void *ptr)
94 {
95 #ifdef _WIN32
96         _aligned_free(ptr);
97 #else
98         free(ptr);
99 #endif
100 }
101
102 void MEM_use_guarded_allocator(void)
103 {
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_mallocN = MEM_guarded_mallocN;
111         MEM_mallocN_aligned = MEM_guarded_mallocN_aligned;
112         MEM_mapallocN = MEM_guarded_mapallocN;
113         MEM_printmemlist_pydict = MEM_guarded_printmemlist_pydict;
114         MEM_printmemlist = MEM_guarded_printmemlist;
115         MEM_callbackmemlist = MEM_guarded_callbackmemlist;
116         MEM_printmemlist_stats = MEM_guarded_printmemlist_stats;
117         MEM_set_error_callback = MEM_guarded_set_error_callback;
118         MEM_check_memory_integrity = MEM_guarded_check_memory_integrity;
119         MEM_set_lock_callback = MEM_guarded_set_lock_callback;
120         MEM_set_memory_debug = MEM_guarded_set_memory_debug;
121         MEM_get_memory_in_use = MEM_guarded_get_memory_in_use;
122         MEM_get_mapped_memory_in_use = MEM_guarded_get_mapped_memory_in_use;
123         MEM_get_memory_blocks_in_use = MEM_guarded_get_memory_blocks_in_use;
124         MEM_reset_peak_memory = MEM_guarded_reset_peak_memory;
125         MEM_get_peak_memory = MEM_guarded_get_peak_memory;
126
127 #ifndef NDEBUG
128         MEM_name_ptr = MEM_guarded_name_ptr;
129 #endif
130 }