Skip to content

Commit

Permalink
avoid chrono inclusion when clock_gettime is available
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Nov 24, 2024
1 parent 69e280c commit 2639fd2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/snmalloc/aal/aal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
#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
#else
# include <chrono>
#endif
#include <cstdint>
#include <utility>

Expand Down Expand Up @@ -169,11 +176,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 2639fd2

Please sign in to comment.