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.
16 * The Original Code is Copyright (C) 2013 by Blender Foundation.
17 * All rights reserved.
24 #ifndef __MALLOCN_INTERN_H__
25 #define __MALLOCN_INTERN_H__
29 # include "mmap_win.h"
31 # include <sys/mman.h>
35 # define UNUSED(x) UNUSED_##x __attribute__((__unused__))
37 # define UNUSED(x) UNUSED_##x
40 #undef HAVE_MALLOC_STATS
41 #define USE_MALLOC_USABLE_SIZE /* internal, when we have malloc_usable_size() */
43 #if defined(__linux__) || (defined(__FreeBSD_kernel__) && !defined(__FreeBSD__)) || \
46 # define HAVE_MALLOC_STATS
47 #elif defined(__FreeBSD__)
48 # include <malloc_np.h>
49 #elif defined(__APPLE__)
50 # include <malloc/malloc.h>
51 # define malloc_usable_size malloc_size
54 # define malloc_usable_size _msize
55 #elif defined(__HAIKU__)
57 size_t malloc_usable_size(void *ptr);
59 # pragma message "We don't know how to use malloc_usable_size on your platform"
60 # undef USE_MALLOC_USABLE_SIZE
63 /* Blame Microsoft for LLP64 and no inttypes.h, quick workaround needed: */
65 # define SIZET_FORMAT "%I64u"
66 # define SIZET_ARG(a) ((unsigned long long)(a))
68 # define SIZET_FORMAT "%lu"
69 # define SIZET_ARG(a) ((unsigned long)(a))
72 #define SIZET_ALIGN_4(len) ((len + 3) & ~(size_t)3)
75 # define LIKELY(x) __builtin_expect(!!(x), 1)
76 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
78 # define LIKELY(x) (x)
79 # define UNLIKELY(x) (x)
82 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
83 // Needed for memalign on Linux and _aligned_alloc on Windows.
87 // Apple's malloc is 16-byte aligned, and does not have malloc.h, so include
92 /* visual studio 2012 does not define inline for C */
94 # define MEM_INLINE static __inline
96 # define MEM_INLINE static inline
99 #define IS_POW2(a) (((a) & ((a)-1)) == 0)
101 /* Extra padding which needs to be applied on MemHead to make it aligned. */
102 #define MEMHEAD_ALIGN_PADDING(alignment) \
103 ((size_t)alignment - (sizeof(MemHeadAligned) % (size_t)alignment))
105 /* Real pointer returned by the malloc or aligned_alloc. */
106 #define MEMHEAD_REAL_PTR(memh) ((char *)memh - MEMHEAD_ALIGN_PADDING(memh->alignment))
108 #include "mallocn_inline.h"
110 #define ALIGNED_MALLOC_MINIMUM_ALIGNMENT sizeof(void *)
112 void *aligned_malloc(size_t size, size_t alignment);
113 void aligned_free(void *ptr);
115 /* Prototypes for counted allocator functions */
116 size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
117 void MEM_lockfree_freeN(void *vmemh);
118 void *MEM_lockfree_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
119 void *MEM_lockfree_reallocN_id(void *vmemh,
121 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
123 void *MEM_lockfree_recallocN_id(void *vmemh,
125 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
127 void *MEM_lockfree_callocN(size_t len, const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
128 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
129 void *MEM_lockfree_calloc_arrayN(size_t len,
131 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
132 ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
133 void *MEM_lockfree_mallocN(size_t len, const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
134 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
135 void *MEM_lockfree_malloc_arrayN(size_t len,
137 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
138 ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
139 void *MEM_lockfree_mallocN_aligned(size_t len,
141 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
142 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3);
143 void *MEM_lockfree_mapallocN(size_t len,
144 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
145 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
146 void MEM_lockfree_printmemlist_pydict(void);
147 void MEM_lockfree_printmemlist(void);
148 void MEM_lockfree_callbackmemlist(void (*func)(void *));
149 void MEM_lockfree_printmemlist_stats(void);
150 void MEM_lockfree_set_error_callback(void (*func)(const char *));
151 bool MEM_lockfree_consistency_check(void);
152 void MEM_lockfree_set_lock_callback(void (*lock)(void), void (*unlock)(void));
153 void MEM_lockfree_set_memory_debug(void);
154 size_t MEM_lockfree_get_memory_in_use(void);
155 size_t MEM_lockfree_get_mapped_memory_in_use(void);
156 unsigned int MEM_lockfree_get_memory_blocks_in_use(void);
157 void MEM_lockfree_reset_peak_memory(void);
158 size_t MEM_lockfree_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT;
160 const char *MEM_lockfree_name_ptr(void *vmemh);
163 /* Prototypes for fully guarded allocator functions */
164 size_t MEM_guarded_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
165 void MEM_guarded_freeN(void *vmemh);
166 void *MEM_guarded_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
167 void *MEM_guarded_reallocN_id(void *vmemh,
169 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
171 void *MEM_guarded_recallocN_id(void *vmemh,
173 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
175 void *MEM_guarded_callocN(size_t len, const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
176 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
177 void *MEM_guarded_calloc_arrayN(size_t len,
179 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
180 ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
181 void *MEM_guarded_mallocN(size_t len, const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
182 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
183 void *MEM_guarded_malloc_arrayN(size_t len,
185 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
186 ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
187 void *MEM_guarded_mallocN_aligned(size_t len,
189 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
190 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3);
191 void *MEM_guarded_mapallocN(size_t len,
192 const char *UNUSED(str)) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
193 ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
194 void MEM_guarded_printmemlist_pydict(void);
195 void MEM_guarded_printmemlist(void);
196 void MEM_guarded_callbackmemlist(void (*func)(void *));
197 void MEM_guarded_printmemlist_stats(void);
198 void MEM_guarded_set_error_callback(void (*func)(const char *));
199 bool MEM_guarded_consistency_check(void);
200 void MEM_guarded_set_lock_callback(void (*lock)(void), void (*unlock)(void));
201 void MEM_guarded_set_memory_debug(void);
202 size_t MEM_guarded_get_memory_in_use(void);
203 size_t MEM_guarded_get_mapped_memory_in_use(void);
204 unsigned int MEM_guarded_get_memory_blocks_in_use(void);
205 void MEM_guarded_reset_peak_memory(void);
206 size_t MEM_guarded_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT;
208 const char *MEM_guarded_name_ptr(void *vmemh);
211 #endif /* __MALLOCN_INTERN_H__ */