Skip to content

Commit

Permalink
avoid chrono inclusion when clock_gettime is available (#695)
Browse files Browse the repository at this point in the history
* avoid chrono inclusion when clock_gettime is available

* fix build
  • Loading branch information
SchrodingerZhu authored Nov 26, 2024
1 parent 33b7f65 commit cd91793
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/snmalloc/aal/aal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) || \
Expand Down Expand Up @@ -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
{
Expand Down
9 changes: 9 additions & 0 deletions src/snmalloc/ds_core/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/snmalloc/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 0 additions & 2 deletions src/snmalloc/pal/pal_timer_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "pal_consts.h"
#include "pal_ds.h"

#include <chrono>

namespace snmalloc
{
template<typename PalTime>
Expand Down

0 comments on commit cd91793

Please sign in to comment.