diff --git a/src/snmalloc/aal/aal.h b/src/snmalloc/aal/aal.h index 92baaf1..dae6231 100644 --- a/src/snmalloc/aal/aal.h +++ b/src/snmalloc/aal/aal.h @@ -10,10 +10,19 @@ #include "aal_concept.h" #include "aal_consts.h" -#include +#if __has_include() +# include +# ifdef CLOCK_MONOTONIC +# define SNMALLOC_TICK_USE_CLOCK_GETTIME +# endif +#endif #include #include +#ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME +# include +#endif + #if ( \ defined(__i386__) || defined(_M_IX86) || defined(_X86_) || \ defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || \ @@ -169,11 +178,27 @@ namespace snmalloc if constexpr ( (Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters) { +#ifdef SNMALLOC_TICK_USE_CLOCK_GETTIME + // the buf is populated by clock_gettime + SNMALLOC_UNINITIALISED timespec buf; + // we can skip the error checking here: + // * EFAULT: for out-of-bound pointers (buf is always valid stack + // memory) + // * EINVAL: for invalid clock_id (we only use CLOCK_MONOTONIC enforced + // by POSIX.1) + // Notice that clock_gettime is a usually a vDSO call, so the overhead + // is minimal. + ::clock_gettime(CLOCK_MONOTONIC, &buf); + return static_cast(buf.tv_sec) * 1000'000'000 + + static_cast(buf.tv_nsec); +# undef SNMALLOC_TICK_USE_CLOCK_GETTIME +#else auto tick = std::chrono::high_resolution_clock::now(); return static_cast( std::chrono::duration_cast( tick.time_since_epoch()) .count()); +#endif } else { diff --git a/src/snmalloc/ds_core/defines.h b/src/snmalloc/ds_core/defines.h index 6b36d6e..d50939a 100644 --- a/src/snmalloc/ds_core/defines.h +++ b/src/snmalloc/ds_core/defines.h @@ -194,6 +194,15 @@ namespace snmalloc # endif #endif +// Used to suppress pattern filling for potentially unintialized variables with +// automatic storage duration. +// https://clang.llvm.org/docs/AttributeReference.html#uninitialized +#ifdef __clang__ +# define SNMALLOC_UNINITIALISED [[clang::uninitialized]] +#else +# define SNMALLOC_UNINITIALISED +#endif + namespace snmalloc { /** diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 3702676..1214ff3 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -6,6 +6,7 @@ #if defined(SNMALLOC_BACKTRACE_HEADER) # include SNMALLOC_BACKTRACE_HEADER #endif +#include #include #include #include diff --git a/src/snmalloc/pal/pal_timer_default.h b/src/snmalloc/pal/pal_timer_default.h index c7761ef..d70abd5 100644 --- a/src/snmalloc/pal/pal_timer_default.h +++ b/src/snmalloc/pal/pal_timer_default.h @@ -4,8 +4,6 @@ #include "pal_consts.h" #include "pal_ds.h" -#include - namespace snmalloc { template