libmemalloc  v3.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
memalloc_utils.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2025 Rafael V. Volkmer
3 * SPDX-FileCopyrightText: <rafael.v.volkmer@gmail.com>
4 * SPDX-License-Identifier: MIT
5 */
6
27#pragma once
28
29/*< C++ Compatibility >*/
30#ifdef __cplusplus
31extern "C"
32{
33#endif
34
46#ifndef ARCH_ALIGNMENT
47 /* x86_64 processor [64-bit] - Vendor: AMD/Intel */
48 #if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__)
49 #define ARCH_ALIGNMENT (8U)
50
51 /* i386 processor [32-bit] - Vendor: Intel */
52 #elif defined(__i386__) || defined(_M_IX86) || defined(__386__)
53 #define ARCH_ALIGNMENT (4U)
54
55 /* aarch64 processor [64-bit] - Vendor: ARM */
56 #elif defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64__)
57 #define ARCH_ALIGNMENT (8U)
58
59 /* arm processor [32-bit] - Vendor: ARM (v7/v6) */
60 #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_6__) || defined(__arm__) \
61 || defined(_M_ARM)
62 #define ARCH_ALIGNMENT (4U)
63
64 /* riscv processor [64-bit] - Vendor: RISC-V */
65 #elif defined(__riscv) && (__riscv_xlen == 64)
66 #define ARCH_ALIGNMENT (8U)
67 /* riscv processor [32-bit] - Vendor: RISC-V */
68 #elif defined(__riscv) && (__riscv_xlen == 32)
69 #define ARCH_ALIGNMENT (4U)
70
71 /* powerpc processor [64-bit] - Vendor: IBM */
72 #elif defined(__powerpc64__) || defined(__PPC64__)
73 #define ARCH_ALIGNMENT (8U)
74 /* powerpc processor [32-bit] - Vendor: IBM */
75 #elif defined(__powerpc__) || defined(__PPC__)
76 #define ARCH_ALIGNMENT (4U)
77
78 /* s390x processor [64-bit] - Vendor: IBM */
79 #elif defined(__s390x__) || defined(__s390__)
80 #define ARCH_ALIGNMENT (8U)
81
82 /* mips processor [64-bit] - Vendor: MIPS */
83 #elif defined(__mips64) || defined(__mips64el__)
84 #define ARCH_ALIGNMENT (8U)
85 /* mips processor [32-bit] - Vendor: MIPS */
86 #elif defined(__mips__)
87 #define ARCH_ALIGNMENT (4U)
88
89 /* loongarch processor [64-bit] - Vendor: Loongson */
90 #elif defined(__loongarch__)
91 #define ARCH_ALIGNMENT (8U)
92
93 /* avr processor [8-bit] - Vendor: Microchip */
94 #elif defined(__AVR__)
95 #define ARCH_ALIGNMENT (2U)
96 /* avr32 processor [32-bit] - Vendor: Microchip */
97 #elif defined(__avr32__)
98 #define ARCH_ALIGNMENT (4U)
99
100 /* microblaze processor [32-bit] - Vendor: Xilinx */
101 #elif defined(__MICROBLAZE__)
102 #define ARCH_ALIGNMENT (4U)
103
104 /* sh processor [32-bit] - Vendor: Renesas */
105 #elif defined(__sh__)
106 #define ARCH_ALIGNMENT (4U)
107
108 /* arc processor [32-bit] - Vendor: Synopsys */
109 #elif defined(__arc__)
110 #define ARCH_ALIGNMENT (4U)
111
112 /* bfin processor [32-bit] - Vendor: Analog Devices */
113 #elif defined(__bfin__)
114 #define ARCH_ALIGNMENT (4U)
115
116 /* wasm processor [32-bit] - Vendor: WebAssembly/Emscripten */
117 #elif defined(__EMSCRIPTEN__) || defined(__wasm__)
118 #define ARCH_ALIGNMENT (4U)
119
120 /* sparc processor [32-bit] - Vendor: Oracle */
121 #elif defined(__sparc__) || defined(__sparc)
122 #define ARCH_ALIGNMENT (4U)
123 /* sparc processor [64-bit] - Vendor: Oracle (V9) */
124 #elif defined(__sparcv9__)
125 #define ARCH_ALIGNMENT (8U)
126
127 /* ia64 processor [64-bit] - Vendor: Intel Itanium */
128 #elif defined(__ia64__)
129 #define ARCH_ALIGNMENT (8U)
130
131 /* alpha processor [64-bit] - Vendor: DEC */
132 #elif defined(__alpha__)
133 #define ARCH_ALIGNMENT (8U)
134
135 /* pru processor [32-bit] - Vendor: TI */
136 #elif defined(__TI_PRU__) || defined(__PRU__)
137 #define ARCH_ALIGNMENT (4U)
138 /* PA-RISC [32-bit] – Vendor: HP PA-RISC */
139 #elif defined(__hppa__)
140 #define ARCH_ALIGNMENT (4U)
141
142 /* z/Arch [64-bit] – Vendor: IBM Z */
143 #elif defined(__zarch__)
144 #define ARCH_ALIGNMENT (8U)
145
146 /* SPU [32-bit] – Vendor: Cell Broadband */
147 #elif defined(__cell_spu__)
148 #define ARCH_ALIGNMENT (16U) /* SPU natural aligns to 16 bytes */
149
150 /* NVPTX [64-bit] – Vendor: NVIDIA PTX */
151 #elif defined(__NVPTX__) || defined(__PTX__)
152 #define ARCH_ALIGNMENT (8U)
153
154 /* eBPF [64-bit] – in-kernel VM */
155 #elif defined(__bpf__)
156 #define ARCH_ALIGNMENT (8U)
157
158 /* Elbrus E2K [64-bit] – Vendor: MCST */
159 #elif defined(__e2k__)
160 #define ARCH_ALIGNMENT (8U)
161
162 /* CRIS [32-bit] – Vendor: Axis Communications */
163 #elif defined(__CRIS__)
164 #define ARCH_ALIGNMENT (4U)
165
166 /* TILEGX [64-bit] – Vendor: Tilera */
167 #elif defined(__tilegx__)
168 #define ARCH_ALIGNMENT (8U)
169
170 /* Generic fallback */
171 #else
172 #error "[ERROR] Unknown Architecture: check 'ARCH_ALIGNMENT'"
173 #endif
174#endif
175
185#define ALIGN(val_) (((val_) + (ARCH_ALIGNMENT - 1U)) & ~(ARCH_ALIGNMENT - 1U))
186
197#define PTR_ERR(ptr_) ((void *)((intptr_t)(ptr_)))
198
215#ifndef __LIBMEMALLOC_API
216 #if defined(_WIN32)
217 #ifdef BUILDING_LIBMEMALLOC
218 #define __LIBMEMALLOC_API __declspec(dllexport)
219 #else
220 #define __LIBMEMALLOC_API __declspec(dllimport)
221 #endif
222 #else
223 #if defined(__GNUC__) || defined(__clang__)
224 #define __LIBMEMALLOC_API __attribute__((visibility("default")))
225 #else
226 #define __LIBMEMALLOC_API
227 #endif
228 #endif
229#endif
230
244#ifndef __ALIGN
245 #if defined(__GNUC__) || defined(__clang__)
246 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
247 #define __ALIGN \
248 __attribute__((scalar_storage_order("big-endian"), \
249 aligned(ARCH_ALIGNMENT)))
250 #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
251 #define __ALIGN \
252 __attribute__((scalar_storage_order("little-endian"), \
253 aligned(ARCH_ALIGNMENT)))
254 #else
255 #error "[ERROR] Unknown Endianness: check '__BYTE_ORDER__'"
256 #endif
257 #else
258 #define __ALIGN
259 #endif
260#endif
261
270#ifndef __UNUSED
271 #if defined(__GNUC__) || defined(__clang__)
272 #define __UNUSED __attribute__((unused))
273 #else
274 #define __UNUSED
275 #endif
276#endif
277
288#ifndef __GC_HOT
289 #if defined(__GNUC__) || defined(__clang__)
290 #define __GC_HOT __attribute__((hot))
291 #else
292 #define __GC_HOT
293 #endif
294#endif
295
306#ifndef __GC_COLD
307 #if defined(__GNUC__) || defined(__clang__)
308 #define __GC_COLD __attribute__((cold))
309 #else
310 #define __GC_COLD
311 #endif
312#endif
313
327#ifndef __LIBMEMALLOC_INTERNAL_MALLOC
328 #if defined(__GNUC__) || defined(__clang__)
329 #define __LIBMEMALLOC_INTERNAL_MALLOC \
330 __attribute__((malloc, \
331 alloc_size(2), \
332 warn_unused_result, \
333 returns_nonnull, \
334 zero_call_used_regs("all")))
335 #else
336 #define __LIBMEMALLOC_INTERNAL_MALLOC
337 #endif
338#endif
339
353#ifndef __LIBMEMALLOC_MALLOC
354 #if defined(__GNUC__) || defined(__clang__)
355 #define __LIBMEMALLOC_MALLOC \
356 __attribute__((malloc, \
357 alloc_size(1), \
358 warn_unused_result, \
359 returns_nonnull, \
360 zero_call_used_regs("all")))
361 #else
362 #define __LIBMEMALLOC_MALLOC
363 #endif
364#endif
365
378#ifndef __LIBMEMALLOC_REALLOC
379 #if defined(__GNUC__) || defined(__clang__)
380 #define __LIBMEMALLOC_REALLOC \
381 __attribute__((malloc, \
382 alloc_size(2), \
383 warn_unused_result, \
384 returns_nonnull, \
385 zero_call_used_regs("all")))
386 #else
387 #define __LIBMEMALLOC_REALLOC
388 #endif
389#endif
390
403#ifndef __LIBMEMALLOC_INTERNAL_REALLOC
404 #if defined(__GNUC__) || defined(__clang__)
405 #define __LIBMEMALLOC_INTERNAL_REALLOC \
406 __attribute__((malloc, \
407 alloc_size(3), \
408 warn_unused_result, \
409 returns_nonnull, \
410 zero_call_used_regs("all")))
411 #else
412 #define __LIBMEMALLOC_INTERNAL_REALLOC
413 #endif
414#endif
415
429#ifndef LIKELY
430 #if (defined(__has_builtin) && __has_builtin(__builtin_expect)) \
431 || defined(__GNUC__) || defined(__clang__)
432 #define LIKELY(cond_) (__builtin_expect(!!(cond_), 1))
433 #else
434 #define LIKELY(cond_) (!!(cond_))
435 #endif
436#endif
437
447#ifndef UNLIKELY
448 #if (defined(__has_builtin) && __has_builtin(__builtin_expect)) \
449 || defined(__GNUC__) || defined(__clang__)
450 #define UNLIKELY(cond_) (__builtin_expect(!!(cond_), 0))
451 #else
452 #define UNLIKELY(cond_) (!!(x))
453 #endif
454#endif
455
465#ifndef PREFETCH_R
466 #if (defined(__has_builtin) && __has_builtin(__builtin_prefetch)) \
467 || defined(__GNUC__) || defined(__clang__)
468 #define PREFETCH_R(addr_) __builtin_prefetch((addr_), 0, 1)
469 #else
470 #define PREFETCH_R(addr_) ((void)0)
471 #endif
472#endif
473
483#ifndef PREFETCH_W
484 #if (defined(__has_builtin) && __has_builtin(__builtin_prefetch)) \
485 || defined(__GNUC__) || defined(__clang__)
486 #define PREFETCH_W(addr_) __builtin_prefetch((addr_), 1, 1)
487 #else
488 #define PREFETCH_W(addr_) ((void)0)
489 #endif
490#endif
491
504#ifndef ASSUME_ALIGNED
505 #if (defined(__has_builtin) && __has_builtin(__builtin_assume_aligned)) \
506 || defined(__GNUC__) || defined(__clang__)
507 #define ASSUME_ALIGNED(ptr_, align_) \
508 __builtin_assume_aligned((ptr_), (align_))
509 #else
510 #define ASSUME_ALIGNED(ptr_, align_) (align_)
511 #endif
512#endif
513
514/*< C++ Compatibility >*/
515#ifdef __cplusplus
516}
517#endif
518
521/*< end of header file >*/