Skip to content

Commit 7512b0e

Browse files
committed
merge from dev
2 parents 08b64e7 + 93a003e commit 7512b0e

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

include/mimalloc/internal.h

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ terms of the MIT license. A copy of the license can be found in the file
88
#ifndef MI_INTERNAL_H
99
#define MI_INTERNAL_H
1010

11-
1211
// --------------------------------------------------------------------------
1312
// This file contains the internal API's of mimalloc and various utility
1413
// functions and macros.
@@ -18,6 +17,17 @@ terms of the MIT license. A copy of the license can be found in the file
1817
#include "track.h"
1918
#include "bits.h"
2019

20+
21+
// --------------------------------------------------------------------------
22+
// Compiler defines
23+
// --------------------------------------------------------------------------
24+
25+
#if (MI_DEBUG>0)
26+
#define mi_trace_message(...) _mi_trace_message(__VA_ARGS__)
27+
#else
28+
#define mi_trace_message(...)
29+
#endif
30+
2131
#define mi_decl_cache_align mi_decl_align(64)
2232

2333
#if defined(_MSC_VER)
@@ -26,26 +36,59 @@ terms of the MIT license. A copy of the license can be found in the file
2636
#define mi_decl_noinline __declspec(noinline)
2737
#define mi_decl_thread __declspec(thread)
2838
#define mi_decl_align(a) __declspec(align(a))
39+
#define mi_decl_noreturn __declspec(noreturn)
2940
#define mi_decl_weak
3041
#define mi_decl_hidden
42+
#define mi_decl_cold
3143
#elif (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) // includes clang and icc
3244
#define mi_decl_noinline __attribute__((noinline))
3345
#define mi_decl_thread __thread
3446
#define mi_decl_align(a) __attribute__((aligned(a)))
47+
#define mi_decl_noreturn __attribute__((noreturn))
3548
#define mi_decl_weak __attribute__((weak))
3649
#define mi_decl_hidden __attribute__((visibility("hidden")))
50+
#if (__GNUC__ >= 4) || defined(__clang__)
51+
#define mi_decl_cold __attribute__((cold))
52+
#else
53+
#define mi_decl_cold
54+
#endif
3755
#elif __cplusplus >= 201103L // c++11
3856
#define mi_decl_noinline
3957
#define mi_decl_thread thread_local
40-
#define mi_decl_cache_align alignas(MI_CACHE_LINE)
58+
#define mi_decl_align(a) alignas(a)
59+
#define mi_decl_noreturn [[noreturn]]
4160
#define mi_decl_weak
4261
#define mi_decl_hidden
62+
#define mi_decl_cold
4363
#else
4464
#define mi_decl_noinline
4565
#define mi_decl_thread __thread // hope for the best :-)
4666
#define mi_decl_align(a)
67+
#define mi_decl_noreturn
4768
#define mi_decl_weak
4869
#define mi_decl_hidden
70+
#define mi_decl_cold
71+
#endif
72+
73+
#if defined(__GNUC__) || defined(__clang__)
74+
#define mi_unlikely(x) (__builtin_expect(!!(x),false))
75+
#define mi_likely(x) (__builtin_expect(!!(x),true))
76+
#elif (defined(__cplusplus) && (__cplusplus >= 202002L)) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
77+
#define mi_unlikely(x) (x) [[unlikely]]
78+
#define mi_likely(x) (x) [[likely]]
79+
#else
80+
#define mi_unlikely(x) (x)
81+
#define mi_likely(x) (x)
82+
#endif
83+
84+
#ifndef __has_builtin
85+
#define __has_builtin(x) 0
86+
#endif
87+
88+
#if defined(__cplusplus)
89+
#define mi_decl_externc extern "C"
90+
#else
91+
#define mi_decl_externc
4992
#endif
5093

5194
#if (defined(__GNUC__) && (__GNUC__ >= 7)) || defined(__clang__) // includes clang and icc
@@ -67,11 +110,10 @@ terms of the MIT license. A copy of the license can be found in the file
67110
#define __wasi__
68111
#endif
69112

70-
#if (MI_DEBUG>0)
71-
#define mi_trace_message(...) _mi_trace_message(__VA_ARGS__)
72-
#else
73-
#define mi_trace_message(...)
74-
#endif
113+
114+
// --------------------------------------------------------------------------
115+
// Internal functions
116+
// --------------------------------------------------------------------------
75117

76118

77119
// "libc.c"
@@ -261,7 +303,6 @@ bool _mi_page_is_valid(mi_page_t* page);
261303
#endif
262304

263305

264-
265306
/* -----------------------------------------------------------
266307
Assertions
267308
----------------------------------------------------------- */
@@ -344,6 +385,32 @@ typedef struct mi_option_desc_s {
344385
const char* legacy_name; // potential legacy option name
345386
} mi_option_desc_t;
346387

388+
// ------------------------------------------------------
389+
// Assertions
390+
// ------------------------------------------------------
391+
392+
#if (MI_DEBUG)
393+
// use our own assertion to print without memory allocation
394+
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;
395+
#define mi_assert(expr) ((expr) ? (void)0 : _mi_assert_fail(#expr,__FILE__,__LINE__,__func__))
396+
#else
397+
#define mi_assert(x)
398+
#endif
399+
400+
#if (MI_DEBUG>1)
401+
#define mi_assert_internal mi_assert
402+
#else
403+
#define mi_assert_internal(x)
404+
#endif
405+
406+
#if (MI_DEBUG>2)
407+
#define mi_assert_expensive mi_assert
408+
#else
409+
#define mi_assert_expensive(x)
410+
#endif
411+
412+
413+
347414
/* -----------------------------------------------------------
348415
Inlined definitions
349416
----------------------------------------------------------- */

src/options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ void _mi_warning_message(const char* fmt, ...) {
546546

547547

548548
#if MI_DEBUG
549-
void _mi_assert_fail(const char* assertion, const char* fname, unsigned line, const char* func ) {
549+
mi_decl_noreturn mi_decl_cold void _mi_assert_fail(const char* assertion, const char* fname, unsigned line, const char* func ) mi_attr_noexcept {
550550
_mi_fprintf(NULL, NULL, "mimalloc: assertion failed: at \"%s\":%u, %s\n assertion: \"%s\"\n", fname, line, (func==NULL?"":func), assertion);
551551
abort();
552552
}

0 commit comments

Comments
 (0)