Blender V5.3
mallocn_intern.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#pragma once
5
9
10#ifdef __GNUC__
11# define UNUSED(x) UNUSED_##x __attribute__((__unused__))
12#elif defined(_MSC_VER)
13/* NOTE: This suppresses the warning for the line, not the attribute. */
14# define UNUSED(x) UNUSED_##x __pragma(warning(suppress : 4100))
15#else
16# define UNUSED(x) UNUSED_##x
17#endif
18
19#undef HAVE_MALLOC_STATS
20#define USE_MALLOC_USABLE_SIZE /* internal, when we have malloc_usable_size() */
21
22#if defined(HAVE_MALLOC_STATS_H)
23# include <malloc.h>
24# define HAVE_MALLOC_STATS
25#elif defined(__FreeBSD__)
26# include <malloc_np.h>
27#elif defined(__NetBSD__) || defined(__OpenBSD__)
28# undef USE_MALLOC_USABLE_SIZE
29#elif defined(__APPLE__)
30# include <malloc/malloc.h>
31# define malloc_usable_size malloc_size
32#elif defined(WIN32)
33# include <malloc.h>
34# define malloc_usable_size _msize
35#elif defined(__HAIKU__)
36# include <malloc.h>
37size_t malloc_usable_size(void *ptr);
38#else
39# pragma message "We don't know how to use malloc_usable_size on your platform"
40# undef USE_MALLOC_USABLE_SIZE
41#endif
42
43#define SIZET_FORMAT "%zu"
44#define SIZET_ARG(a) (size_t(a))
45
46#define SIZET_ALIGN_4(len) ((len + 3) & ~size_t(3))
47
48/* NOTE: prefer C++ `[[likely]]` / `[[unlikely]]` attribute.
49 * Use the defines in contexts where attributes aren't supported (ternary operators for e.g.). */
50#ifdef __GNUC__
51# define LIKELY(x) __builtin_expect(!!(x), 1)
52# define UNLIKELY(x) __builtin_expect(!!(x), 0)
53#else
54# define LIKELY(x) (x)
55# define UNLIKELY(x) (x)
56#endif
57
58#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
59/* Needed for `memalign` on Linux and `_aligned_alloc` on Windows. */
60
61# include <malloc.h>
62#else
63/* Apple's malloc is 16-byte aligned, and does not have `malloc.h`, so include `stdilb` instead. */
64# include <stdlib.h>
65#endif
66
67/* visual studio 2012 does not define inline for C */
68#ifdef _MSC_VER
69# define MEM_INLINE static __inline
70#else
71# define MEM_INLINE static inline
72#endif
73
74#define IS_POW2(a) (((a) & ((a) - 1)) == 0)
75
76/* Extra padding which needs to be applied on MemHead to make it aligned. */
77#define MEMHEAD_ALIGN_PADDING(alignment) \
78 (size_t(alignment) - (sizeof(MemHeadAligned) % size_t(alignment)))
79
80/* Real pointer returned by the `malloc` or `aligned_alloc`. */
81#define MEMHEAD_REAL_PTR(memh) ((char *)memh - MEMHEAD_ALIGN_PADDING(memh->alignment))
82
83#include "mallocn_inline.hh"
84
85#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT sizeof(void *)
86
87void *aligned_malloc(size_t size, size_t alignment);
88void aligned_free(void *ptr);
89
90extern bool leak_detector_has_run;
92
93void memory_usage_init(void);
95void memory_usage_block_free(size_t size);
96size_t memory_usage_block_num(void);
97size_t memory_usage_current(void);
107size_t memory_usage_peak(void);
108void memory_usage_peak_reset(void);
109
118extern void (*mem_clearmemlist)(void);
119
120/* Prototypes for counted allocator functions */
121size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
122void MEM_lockfree_freeN(void *vmemh, mem_guarded::internal::DestructorType destructor_type);
124void *MEM_lockfree_reallocN_id(void *vmemh,
125 size_t len,
128void *MEM_lockfree_recallocN_id(void *vmemh,
129 size_t len,
135 size_t size,
141 size_t size,
145 size_t alignment,
146 const char *str,
147 mem_guarded::internal::DestructorType destructor_type)
150 size_t size,
151 size_t alignment,
155 size_t size,
156 size_t alignment,
161void MEM_lockfree_callbackmemlist(void (*func)(void *));
163void MEM_lockfree_set_error_callback(void (*func)(const char *));
170
172
173#ifndef NDEBUG
174const char *MEM_lockfree_name_ptr(void *vmemh);
175void MEM_lockfree_name_ptr_set(void *vmemh, const char *str);
176#endif
177
178/* Prototypes for fully guarded allocator functions */
179size_t MEM_guarded_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
180void MEM_guarded_freeN(void *vmemh, mem_guarded::internal::DestructorType destructor_type);
182void *MEM_guarded_reallocN_id(void *vmemh,
183 size_t len,
186void *MEM_guarded_recallocN_id(void *vmemh,
187 size_t len,
193 size_t size,
199 size_t size,
203 size_t alignment,
204 const char *str,
205 mem_guarded::internal::DestructorType destructor_type)
207void *MEM_guarded_malloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str)
209void *MEM_guarded_calloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str)
212void MEM_guarded_printmemlist(void);
213void MEM_guarded_callbackmemlist(void (*func)(void *));
215void MEM_guarded_set_error_callback(void (*func)(const char *));
219unsigned int MEM_guarded_get_memory_blocks_in_use(void);
222
223void mem_guarded_clearmemlist(void);
224
225#ifndef NDEBUG
226const char *MEM_guarded_name_ptr(void *vmemh);
227void MEM_guarded_name_ptr_set(void *vmemh, const char *str);
228#endif
#define ATTR_MALLOC
#define ATTR_ALLOC_SIZE(...)
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define str(s)
bool leak_detector_has_run
char free_after_leak_detection_message[]
void aligned_free(void *ptr)
Definition mallocn.cc:99
void(*) mem_clearmemlist(void)
Definition mallocn.cc:70
void * aligned_malloc(size_t size, size_t alignment)
Definition mallocn.cc:77
void * MEM_guarded_calloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str)
size_t MEM_guarded_allocN_len(const void *vmemh)
void * MEM_guarded_mallocN(size_t len, const char *str)
size_t MEM_guarded_get_peak_memory()
void MEM_guarded_freeN(void *vmemh, const DestructorType destructor_type)
void * MEM_guarded_calloc_arrayN(size_t len, size_t size, const char *str)
void MEM_guarded_reset_peak_memory()
void MEM_guarded_set_error_callback(void(*func)(const char *))
bool MEM_guarded_consistency_check()
void MEM_guarded_printmemlist_pydict()
void MEM_guarded_callbackmemlist(void(*func)(void *))
void MEM_guarded_printmemlist()
uint MEM_guarded_get_memory_blocks_in_use()
void * MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str, const DestructorType destructor_type)
void * MEM_guarded_callocN(size_t len, const char *str)
void * MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str)
void MEM_guarded_printmemlist_stats()
void * MEM_guarded_malloc_arrayN(size_t len, size_t size, const char *str)
void * MEM_guarded_dupallocN(const void *vmemh)
size_t MEM_guarded_get_memory_in_use()
void * MEM_guarded_recallocN_id(void *vmemh, size_t len, const char *str)
void MEM_guarded_name_ptr_set(void *vmemh, const char *str)
void MEM_guarded_set_memory_debug()
const char * MEM_guarded_name_ptr(void *vmemh)
void mem_guarded_clearmemlist()
void * MEM_guarded_malloc_arrayN_aligned(const size_t len, const size_t size, const size_t alignment, const char *str)
size_t memory_usage_current(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_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void memory_usage_block_alloc(size_t size)
void MEM_lockfree_reset_peak_memory(void)
void mem_lockfree_clearmemlist(void)
void MEM_lockfree_name_ptr_set(void *vmemh, const char *str)
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_lockfree_set_error_callback(void(*func)(const char *))
void MEM_lockfree_printmemlist_stats(void)
void MEM_lockfree_freeN(void *vmemh, mem_guarded::internal::DestructorType destructor_type)
size_t memory_usage_block_num(void)
void MEM_lockfree_set_memory_debug(void)
void memory_usage_init(void)
void * MEM_lockfree_reallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
bool MEM_lockfree_consistency_check(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_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str, mem_guarded::internal::DestructorType destructor_type) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
void memory_usage_block_free(size_t size)
size_t memory_usage_peak(void)
void MEM_lockfree_printmemlist_pydict(void)
void * MEM_lockfree_recallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void memory_usage_peak_reset(void)
void * MEM_lockfree_malloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * MEM_lockfree_mallocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void MEM_lockfree_printmemlist(void)
size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT
const char * MEM_lockfree_name_ptr(void *vmemh)
void MEM_lockfree_callbackmemlist(void(*func)(void *))
void * MEM_lockfree_calloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
size_t MEM_lockfree_get_memory_in_use(void)
unsigned int MEM_lockfree_get_memory_blocks_in_use(void)
size_t MEM_lockfree_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263