Blender V4.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
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#ifdef __GNUC__
49# define LIKELY(x) __builtin_expect(!!(x), 1)
50# define UNLIKELY(x) __builtin_expect(!!(x), 0)
51#else
52# define LIKELY(x) (x)
53# define UNLIKELY(x) (x)
54#endif
55
56#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
57/* Needed for `memalign` on Linux and `_aligned_alloc` on Windows. */
58
59# include <malloc.h>
60#else
61/* Apple's malloc is 16-byte aligned, and does not have `malloc.h`, so include `stdilb` instead. */
62# include <stdlib.h>
63#endif
64
65/* visual studio 2012 does not define inline for C */
66#ifdef _MSC_VER
67# define MEM_INLINE static __inline
68#else
69# define MEM_INLINE static inline
70#endif
71
72#define IS_POW2(a) (((a) & ((a)-1)) == 0)
73
74/* Extra padding which needs to be applied on MemHead to make it aligned. */
75#define MEMHEAD_ALIGN_PADDING(alignment) \
76 ((size_t)alignment - (sizeof(MemHeadAligned) % (size_t)alignment))
77
78/* Real pointer returned by the `malloc` or `aligned_alloc`. */
79#define MEMHEAD_REAL_PTR(memh) ((char *)memh - MEMHEAD_ALIGN_PADDING(memh->alignment))
80
81#include "mallocn_inline.hh"
82
83#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT sizeof(void *)
84
85void *aligned_malloc(size_t size, size_t alignment);
86void aligned_free(void *ptr);
87
88extern bool leak_detector_has_run;
90
91void memory_usage_init(void);
92void memory_usage_block_alloc(size_t size);
93void memory_usage_block_free(size_t size);
94size_t memory_usage_block_num(void);
95size_t memory_usage_current(void);
96size_t memory_usage_peak(void);
98
107extern void (*mem_clearmemlist)(void);
108
109/* Prototypes for counted allocator functions */
110size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
111void MEM_lockfree_freeN(void *vmemh, mem_guarded::internal::AllocationType allocation_type);
113void *MEM_lockfree_reallocN_id(void *vmemh,
114 size_t len,
117void *MEM_lockfree_recallocN_id(void *vmemh,
118 size_t len,
124 size_t size,
130 size_t size,
134 size_t alignment,
135 const char *str,
136 mem_guarded::internal::AllocationType allocation_type)
139 size_t size,
140 size_t alignment,
145void MEM_lockfree_callbackmemlist(void (*func)(void *));
147void MEM_lockfree_set_error_callback(void (*func)(const char *));
154
156
157#ifndef NDEBUG
158const char *MEM_lockfree_name_ptr(void *vmemh);
159void MEM_lockfree_name_ptr_set(void *vmemh, const char *str);
160#endif
161
162/* Prototypes for fully guarded allocator functions */
163size_t MEM_guarded_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT;
164void MEM_guarded_freeN(void *vmemh, mem_guarded::internal::AllocationType allocation_type);
166void *MEM_guarded_reallocN_id(void *vmemh,
167 size_t len,
170void *MEM_guarded_recallocN_id(void *vmemh,
171 size_t len,
177 size_t size,
183 size_t size,
187 size_t alignment,
188 const char *str,
189 mem_guarded::internal::AllocationType allocation_type)
191void *MEM_guarded_calloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str)
194void MEM_guarded_printmemlist(void);
195void MEM_guarded_callbackmemlist(void (*func)(void *));
197void MEM_guarded_set_error_callback(void (*func)(const char *));
201unsigned int MEM_guarded_get_memory_blocks_in_use(void);
204
205void mem_guarded_clearmemlist(void);
206
207#ifndef NDEBUG
208const char *MEM_guarded_name_ptr(void *vmemh);
209void MEM_guarded_name_ptr_set(void *vmemh, const char *str);
210#endif
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_MALLOC
#define ATTR_ALLOC_SIZE(...)
#define ATTR_NONNULL(...)
int len
#define str(s)
void * MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
size_t memory_usage_current(void)
unsigned int MEM_guarded_get_memory_blocks_in_use(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_guarded_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 MEM_lockfree_freeN(void *vmemh, mem_guarded::internal::AllocationType allocation_type)
void aligned_free(void *ptr)
Definition mallocn.cc:96
void * MEM_guarded_recallocN_id(void *vmemh, size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
void memory_usage_block_alloc(size_t size)
size_t MEM_guarded_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT
void MEM_lockfree_reset_peak_memory(void)
void mem_lockfree_clearmemlist(void)
size_t MEM_guarded_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT
void MEM_lockfree_name_ptr_set(void *vmemh, const char *str)
void * MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str, mem_guarded::internal::AllocationType allocation_type) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
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_guarded_set_error_callback(void(*func)(const char *))
void MEM_lockfree_printmemlist_stats(void)
size_t memory_usage_block_num(void)
void MEM_lockfree_set_memory_debug(void)
void memory_usage_init(void)
void MEM_guarded_printmemlist(void)
void MEM_guarded_printmemlist_pydict(void)
void MEM_guarded_callbackmemlist(void(*func)(void *))
void(* mem_clearmemlist)(void)
Definition mallocn.cc:67
void * MEM_guarded_dupallocN(const void *vmemh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool leak_detector_has_run
void * MEM_guarded_callocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool MEM_guarded_consistency_check(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_guarded_freeN(void *vmemh, mem_guarded::internal::AllocationType allocation_type)
void mem_guarded_clearmemlist(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 memory_usage_block_free(size_t size)
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_set_memory_debug(void)
void MEM_guarded_printmemlist_stats(void)
size_t memory_usage_peak(void)
void MEM_lockfree_printmemlist_pydict(void)
char free_after_leak_detection_message[]
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_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)
void MEM_guarded_name_ptr_set(void *vmemh, const char *str)
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 * aligned_malloc(size_t size, size_t alignment)
Definition mallocn.cc:74
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
const char * MEM_guarded_name_ptr(void *vmemh)
void MEM_guarded_reset_peak_memory(void)
size_t MEM_lockfree_get_memory_in_use(void)
size_t MEM_guarded_get_memory_in_use(void)
unsigned int MEM_lockfree_get_memory_blocks_in_use(void)
void * MEM_guarded_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_peak_memory(void) ATTR_WARN_UNUSED_RESULT
void * MEM_guarded_mallocN(size_t len, const char *str) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void * MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str, mem_guarded::internal::AllocationType allocation_type) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
PointerRNA * ptr
Definition wm_files.cc:4126