avoid chrono inclusion when clock_gettime is available (#695)

* avoid chrono inclusion when clock_gettime is available

* fix build
This commit is contained in:
Schrodinger ZHU Yifan
2024-11-26 16:04:45 -05:00
committed by GitHub
parent 33b7f656ab
commit cd91793def
4 changed files with 36 additions and 3 deletions

View File

@@ -10,10 +10,19 @@
#include "aal_concept.h"
#include "aal_consts.h"
#include <chrono>
#if __has_include(<time.h>)
# include <time.h>
# ifdef CLOCK_MONOTONIC
# define SNMALLOC_TICK_USE_CLOCK_GETTIME
# endif
#endif
#include <cstdint>
#include <utility>
#ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME
# include <chrono>
#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<uint64_t>(buf.tv_sec) * 1000'000'000 +
static_cast<uint64_t>(buf.tv_nsec);
# undef SNMALLOC_TICK_USE_CLOCK_GETTIME
#else
auto tick = std::chrono::high_resolution_clock::now();
return static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
tick.time_since_epoch())
.count());
#endif
}
else
{

View File

@@ -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
{
/**

View File

@@ -6,6 +6,7 @@
#if defined(SNMALLOC_BACKTRACE_HEADER)
# include SNMALLOC_BACKTRACE_HEADER
#endif
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>

View File

@@ -4,8 +4,6 @@
#include "pal_consts.h"
#include "pal_ds.h"
#include <chrono>
namespace snmalloc
{
template<typename PalTime>