Blender  V2.93
mallocn_intern.h
Go to the documentation of this file.
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  * The Original Code is Copyright (C) 2013 by Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #ifndef __MALLOCN_INTERN_H__
25 #define __MALLOCN_INTERN_H__
26 
27 #ifdef __GNUC__
28 # define UNUSED(x) UNUSED_##x __attribute__((__unused__))
29 #else
30 # define UNUSED(x) UNUSED_##x
31 #endif
32 
33 #undef HAVE_MALLOC_STATS
34 #define USE_MALLOC_USABLE_SIZE /* internal, when we have malloc_usable_size() */
35 
36 #if defined(__linux__) || (defined(__FreeBSD_kernel__) && !defined(__FreeBSD__)) || \
37  defined(__GLIBC__)
38 # include <malloc.h>
39 # define HAVE_MALLOC_STATS
40 #elif defined(__FreeBSD__)
41 # include <malloc_np.h>
42 #elif defined(__APPLE__)
43 # include <malloc/malloc.h>
44 # define malloc_usable_size malloc_size
45 #elif defined(WIN32)
46 # include <malloc.h>
47 # define malloc_usable_size _msize
48 #elif defined(__HAIKU__)
49 # include <malloc.h>
50 size_t malloc_usable_size(void *ptr);
51 #else
52 # pragma message "We don't know how to use malloc_usable_size on your platform"
53 # undef USE_MALLOC_USABLE_SIZE
54 #endif
55 
56 /* Blame Microsoft for LLP64 and no inttypes.h, quick workaround needed: */
57 #if defined(WIN64)
58 # define SIZET_FORMAT "%I64u"
59 # define SIZET_ARG(a) ((unsigned long long)(a))
60 #else
61 # define SIZET_FORMAT "%lu"
62 # define SIZET_ARG(a) ((unsigned long)(a))
63 #endif
64 
65 #define SIZET_ALIGN_4(len) ((len + 3) & ~(size_t)3)
66 
67 #ifdef __GNUC__
68 # define LIKELY(x) __builtin_expect(!!(x), 1)
69 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
70 #else
71 # define LIKELY(x) (x)
72 # define UNLIKELY(x) (x)
73 #endif
74 
75 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
76 // Needed for memalign on Linux and _aligned_alloc on Windows.
77 
78 # include <malloc.h>
79 #else
80 // Apple's malloc is 16-byte aligned, and does not have malloc.h, so include
81 // stdilb instead.
82 # include <stdlib.h>
83 #endif
84 
85 /* visual studio 2012 does not define inline for C */
86 #ifdef _MSC_VER
87 # define MEM_INLINE static __inline
88 #else
89 # define MEM_INLINE static inline
90 #endif
91 
92 #define IS_POW2(a) (((a) & ((a)-1)) == 0)
93 
94 /* Extra padding which needs to be applied on MemHead to make it aligned. */
95 #define MEMHEAD_ALIGN_PADDING(alignment) \
96  ((size_t)alignment - (sizeof(MemHeadAligned) % (size_t)alignment))
97 
98 /* Real pointer returned by the malloc or aligned_alloc. */
99 #define MEMHEAD_REAL_PTR(memh) ((char *)memh - MEMHEAD_ALIGN_PADDING(memh->alignment))
100 
101 #include "mallocn_inline.h"
102 
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
107 #define ALIGNED_MALLOC_MINIMUM_ALIGNMENT sizeof(void *)
108 
109 void *aligned_malloc(size_t size, size_t alignment);
110 void aligned_free(void *ptr);
111 
112 extern bool leak_detector_has_run;
114 
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);
119 void *MEM_lockfree_reallocN_id(void *vmemh,
120  size_t len,
122  ATTR_ALLOC_SIZE(2);
123 void *MEM_lockfree_recallocN_id(void *vmemh,
124  size_t len,
126  ATTR_ALLOC_SIZE(2);
130  size_t size,
132  ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
136  size_t size,
138  ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
139 void *MEM_lockfree_mallocN_aligned(size_t len,
140  size_t alignment,
144 void MEM_lockfree_printmemlist(void);
145 void MEM_lockfree_callbackmemlist(void (*func)(void *));
147 void MEM_lockfree_set_error_callback(void (*func)(const char *));
150 size_t MEM_lockfree_get_memory_in_use(void);
151 unsigned int MEM_lockfree_get_memory_blocks_in_use(void);
154 #ifndef NDEBUG
155 const char *MEM_lockfree_name_ptr(void *vmemh);
156 #endif
157 
158 /* Prototypes for fully guarded allocator functions */
159 size_t MEM_guarded_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
160 void MEM_guarded_freeN(void *vmemh);
162 void *MEM_guarded_reallocN_id(void *vmemh,
163  size_t len,
165  ATTR_ALLOC_SIZE(2);
166 void *MEM_guarded_recallocN_id(void *vmemh,
167  size_t len,
169  ATTR_ALLOC_SIZE(2);
173  size_t size,
175  ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
179  size_t size,
181  ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
182 void *MEM_guarded_mallocN_aligned(size_t len,
183  size_t alignment,
187 void MEM_guarded_printmemlist(void);
188 void MEM_guarded_callbackmemlist(void (*func)(void *));
190 void MEM_guarded_set_error_callback(void (*func)(const char *));
193 size_t MEM_guarded_get_memory_in_use(void);
194 unsigned int MEM_guarded_get_memory_blocks_in_use(void);
197 #ifndef NDEBUG
198 const char *MEM_guarded_name_ptr(void *vmemh);
199 #endif
200 
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 #endif /* __MALLOCN_INTERN_H__ */
#define ATTR_MALLOC
#define ATTR_ALLOC_SIZE(...)
void BLI_kdtree_nd_() int BLI_kdtree_nd_() int BLI_kdtree_nd_() int BLI_kdtree_nd_() ATTR_WARN_UNUSED_RESULT
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define str(s)
const char * MEM_guarded_name_ptr(void *vmemh)
unsigned int MEM_guarded_get_memory_blocks_in_use(void)
void * MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
void * MEM_guarded_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void * MEM_lockfree_mallocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void aligned_free(void *ptr)
Definition: mallocn.c:90
size_t MEM_guarded_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT
void * MEM_lockfree_reallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void MEM_lockfree_reset_peak_memory(void)
void * aligned_malloc(size_t size, size_t alignment)
Definition: mallocn.c:68
size_t MEM_guarded_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT
void * MEM_lockfree_recallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void MEM_lockfree_set_error_callback(void(*func)(const char *))
void * MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void MEM_guarded_set_error_callback(void(*func)(const char *))
void MEM_lockfree_printmemlist_stats(void)
void MEM_lockfree_set_memory_debug(void)
void * MEM_guarded_callocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void * MEM_guarded_calloc_arrayN(size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void MEM_guarded_printmemlist(void)
void MEM_guarded_printmemlist_pydict(void)
void * MEM_lockfree_callocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void * MEM_guarded_malloc_arrayN(size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void MEM_guarded_callbackmemlist(void(*func)(void *))
bool leak_detector_has_run
bool MEM_guarded_consistency_check(void)
bool MEM_lockfree_consistency_check(void)
void * MEM_lockfree_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
const char * MEM_lockfree_name_ptr(void *vmemh)
void MEM_guarded_set_memory_debug(void)
void ATTR_NONNULL(3)
Iterator New.
void MEM_guarded_printmemlist_stats(void)
void * MEM_lockfree_malloc_arrayN(size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void MEM_lockfree_printmemlist_pydict(void)
char free_after_leak_detection_message[]
void * MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
void MEM_lockfree_printmemlist(void)
void MEM_lockfree_freeN(void *vmemh)
size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT
void MEM_lockfree_callbackmemlist(void(*func)(void *))
void * MEM_lockfree_calloc_arrayN(size_t len, size_t size, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void MEM_guarded_reset_peak_memory(void)
void * MEM_guarded_recallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
size_t MEM_lockfree_get_memory_in_use(void)
size_t MEM_guarded_get_memory_in_use(void)
void * MEM_guarded_mallocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned int MEM_lockfree_get_memory_blocks_in_use(void)
size_t MEM_lockfree_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT
void MEM_guarded_freeN(void *vmemh)
uint len
PointerRNA * ptr
Definition: wm_files.c:3157