@@ -8,7 +8,6 @@ terms of the MIT license. A copy of the license can be found in the file
8
8
#ifndef MIMALLOC_INTERNAL_H
9
9
#define MIMALLOC_INTERNAL_H
10
10
11
-
12
11
// --------------------------------------------------------------------------
13
12
// This file contains the internal API's of mimalloc and various utility
14
13
// functions and macros.
@@ -17,6 +16,11 @@ terms of the MIT license. A copy of the license can be found in the file
17
16
#include " types.h"
18
17
#include " track.h"
19
18
19
+
20
+ // --------------------------------------------------------------------------
21
+ // Compiler defines
22
+ // --------------------------------------------------------------------------
23
+
20
24
#if (MI_DEBUG>0)
21
25
#define mi_trace_message (...) _mi_trace_message(__VA_ARGS__)
22
26
#else
@@ -30,38 +34,70 @@ terms of the MIT license. A copy of the license can be found in the file
30
34
#define mi_decl_noinline __declspec (noinline)
31
35
#define mi_decl_thread __declspec (thread)
32
36
#define mi_decl_cache_align __declspec (align(MI_CACHE_LINE))
37
+ #define mi_decl_noreturn __declspec(noreturn)
33
38
#define mi_decl_weak
34
39
#define mi_decl_hidden
40
+ #define mi_decl_cold
35
41
#elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc
36
42
#define mi_decl_noinline __attribute__ ((noinline))
37
43
#define mi_decl_thread __thread
38
44
#define mi_decl_cache_align __attribute__ ((aligned(MI_CACHE_LINE)))
45
+ #define mi_decl_noreturn __attribute__((noreturn))
39
46
#define mi_decl_weak __attribute__ ((weak))
40
47
#define mi_decl_hidden __attribute__ ((visibility(" hidden" )))
48
+ #if (__GNUC__ >= 4) || defined(__clang__)
49
+ #define mi_decl_cold __attribute__ ((cold))
50
+ #else
51
+ #define mi_decl_cold
52
+ #endif
41
53
#elif __cplusplus >= 201103L // c++11
42
54
#define mi_decl_noinline
43
55
#define mi_decl_thread thread_local
44
56
#define mi_decl_cache_align alignas (MI_CACHE_LINE)
57
+ #define mi_decl_noreturn [[noreturn]]
45
58
#define mi_decl_weak
46
59
#define mi_decl_hidden
60
+ #define mi_decl_cold
47
61
#else
48
62
#define mi_decl_noinline
49
63
#define mi_decl_thread __thread // hope for the best :-)
50
64
#define mi_decl_cache_align
65
+ #define mi_decl_noreturn
51
66
#define mi_decl_weak
52
67
#define mi_decl_hidden
68
+ #define mi_decl_cold
53
69
#endif
54
70
55
- #if defined(__EMSCRIPTEN__) && !defined(__wasi__)
56
- #define __wasi__
71
+ #if defined(__GNUC__) || defined(__clang__)
72
+ #define mi_unlikely (x ) (__builtin_expect(!!(x),false ))
73
+ #define mi_likely (x ) (__builtin_expect(!!(x),true ))
74
+ #elif (defined(__cplusplus) && (__cplusplus >= 202002L)) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
75
+ #define mi_unlikely (x ) (x) [[unlikely]]
76
+ #define mi_likely (x ) (x) [[likely]]
77
+ #else
78
+ #define mi_unlikely (x ) (x)
79
+ #define mi_likely (x ) (x)
80
+ #endif
81
+
82
+ #ifndef __has_builtin
83
+ #define __has_builtin (x ) 0
57
84
#endif
58
85
59
86
#if defined(__cplusplus)
60
- #define mi_decl_externc extern " C"
87
+ #define mi_decl_externc extern " C"
61
88
#else
62
89
#define mi_decl_externc
63
90
#endif
64
91
92
+ #if defined(__EMSCRIPTEN__) && !defined(__wasi__)
93
+ #define __wasi__
94
+ #endif
95
+
96
+
97
+ // --------------------------------------------------------------------------
98
+ // Internal functions
99
+ // --------------------------------------------------------------------------
100
+
65
101
// "libc.c"
66
102
#include < stdarg.h>
67
103
int _mi_vsnprintf (char * buf, size_t bufsize, const char * fmt, va_list args);
@@ -256,26 +292,6 @@ bool _mi_page_is_valid(mi_page_t* page);
256
292
#endif
257
293
258
294
259
- // ------------------------------------------------------
260
- // Branches
261
- // ------------------------------------------------------
262
-
263
- #if defined(__GNUC__) || defined(__clang__)
264
- #define mi_unlikely (x ) (__builtin_expect(!!(x),false ))
265
- #define mi_likely (x ) (__builtin_expect(!!(x),true ))
266
- #elif (defined(__cplusplus) && (__cplusplus >= 202002L)) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
267
- #define mi_unlikely (x ) (x) [[unlikely]]
268
- #define mi_likely (x ) (x) [[likely]]
269
- #else
270
- #define mi_unlikely (x ) (x)
271
- #define mi_likely (x ) (x)
272
- #endif
273
-
274
- #ifndef __has_builtin
275
- #define __has_builtin (x ) 0
276
- #endif
277
-
278
-
279
295
/* -----------------------------------------------------------
280
296
Error codes passed to `_mi_fatal_error`
281
297
All are recoverable but EFAULT is a serious error and aborts by default in secure mode.
@@ -300,6 +316,32 @@ bool _mi_page_is_valid(mi_page_t* page);
300
316
#endif
301
317
302
318
319
+ // ------------------------------------------------------
320
+ // Assertions
321
+ // ------------------------------------------------------
322
+
323
+ #if (MI_DEBUG)
324
+ // use our own assertion to print without memory allocation
325
+ mi_decl_noreturn mi_decl_cold void _mi_assert_fail (const char * assertion, const char * fname, unsigned int line, const char * func) mi_attr_noexcept;
326
+ #define mi_assert (expr ) ((expr) ? (void )0 : _mi_assert_fail(#expr,__FILE__,__LINE__,__func__))
327
+ #else
328
+ #define mi_assert (x )
329
+ #endif
330
+
331
+ #if (MI_DEBUG>1)
332
+ #define mi_assert_internal mi_assert
333
+ #else
334
+ #define mi_assert_internal (x )
335
+ #endif
336
+
337
+ #if (MI_DEBUG>2)
338
+ #define mi_assert_expensive mi_assert
339
+ #else
340
+ #define mi_assert_expensive (x )
341
+ #endif
342
+
343
+
344
+
303
345
/* -----------------------------------------------------------
304
346
Inlined definitions
305
347
----------------------------------------------------------- */
0 commit comments