Skip to content

Commit

Permalink
[proxy](6/n) self-vendor utility headers (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu authored Jan 9, 2025
1 parent e874617 commit 0027f02
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/combininglock.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ We can now provide our new version of the `with` function that uses the combinin
template<typename F>
inline void with(CombiningLock& lock, F&& f)
{
CombiningLockNodeTempl<F> node(std::forward<F>(f));
CombiningLockNodeTempl<F> node(stl::forward<F>(f));

// **************ACQUIRE**************
// Add ourselves to the end of the queue.
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/aal/aal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
# define SNMALLOC_TICK_USE_CLOCK_GETTIME
# endif
#endif
#include "snmalloc/stl/utility.h"

#include <stdint.h>
#include <utility>

#ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME
# include <chrono>
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/backend/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace snmalloc
* (remote, sizeclass, slab_metadata)
* where slab_metadata, is the second element of the pair return.
*/
static std::pair<capptr::Chunk<void>, SlabMetadata*> alloc_chunk(
static stl::Pair<capptr::Chunk<void>, SlabMetadata*> alloc_chunk(
LocalState& local_state,
size_t size,
uintptr_t ras,
Expand Down
3 changes: 3 additions & 0 deletions src/snmalloc/backend/fixedglobalconfig.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#pragma once

#include "../backend_helpers/backend_helpers.h"
#include "snmalloc/stl/type_traits.h"
#include "standard_range.h"

#include <stddef.h>

namespace snmalloc
{
/**
Expand Down
5 changes: 2 additions & 3 deletions src/snmalloc/backend_helpers/pagemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "../ds/ds.h"
#include "../mem/mem.h"
#include "snmalloc/stl/atomic.h"

#include <utility>
#include "snmalloc/stl/utility.h"

namespace snmalloc
{
Expand Down Expand Up @@ -115,7 +114,7 @@ namespace snmalloc
*/
template<bool fixed_range_ = fixed_range>
static SNMALLOC_FAST_PATH
stl::enable_if_t<fixed_range_, std::pair<address_t, address_t>>
stl::enable_if_t<fixed_range_, stl::Pair<address_t, address_t>>
get_bounds()
{
static_assert(fixed_range_ == fixed_range, "Don't set SFINAE parameter!");
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/ds/combininglock.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace snmalloc
reinterpret_cast<CombiningLockNodeTempl*>(self);
self_templ->f();
}),
f(std::forward<F>(f_))
f(stl::forward<F>(f_))
{
attach_slow(lock);
}
Expand Down Expand Up @@ -290,6 +290,6 @@ namespace snmalloc

// There is contention for the lock, we need to take the slow path
// with the queue.
CombiningLockNodeTempl<F> node(lock, std::forward<F>(f));
CombiningLockNodeTempl<F> node(lock, stl::forward<F>(f));
}
} // namespace snmalloc
4 changes: 2 additions & 2 deletions src/snmalloc/ds/pagemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace snmalloc
* Returns usable range after pagemap has been allocated
*/
template<bool has_bounds_ = has_bounds>
stl::enable_if_t<has_bounds_, std::pair<void*, size_t>>
stl::enable_if_t<has_bounds_, stl::Pair<void*, size_t>>
init(void* b, size_t s)
{
SNMALLOC_ASSERT(!is_initialised());
Expand Down Expand Up @@ -250,7 +250,7 @@ namespace snmalloc
}

template<bool has_bounds_ = has_bounds>
stl::enable_if_t<has_bounds_, std::pair<address_t, size_t>> get_bounds()
stl::enable_if_t<has_bounds_, stl::Pair<address_t, size_t>> get_bounds()
{
SNMALLOC_ASSERT(is_initialised());

Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/ds_aal/flaglock.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace snmalloc
constexpr DebugFlagWord() = default;

template<typename... Args>
constexpr DebugFlagWord(Args&&... args) : flag(std::forward<Args>(args)...)
constexpr DebugFlagWord(Args&&... args) : flag(stl::forward<Args>(args)...)
{}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ namespace snmalloc

template<typename... Args>
constexpr ReleaseFlagWord(Args&&... args)
: flag(std::forward<Args>(args)...)
: flag(stl::forward<Args>(args)...)
{}

void set_owner() {}
Expand Down
7 changes: 4 additions & 3 deletions src/snmalloc/ds_core/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "bits.h"
#include "snmalloc/ds_core/defines.h"
#include "snmalloc/stl/type_traits.h"
#include "snmalloc/stl/utility.h"

#include <array>
#include <stddef.h>
Expand Down Expand Up @@ -357,8 +358,8 @@ namespace snmalloc

if (fmt[0] == '{' && fmt[1] == '}')
{
append(std::forward<Head>(head));
return append(fmt + 2, std::forward<Tail>(tail)...);
append(stl::forward<Head>(head));
return append(fmt + 2, stl::forward<Tail>(tail)...);
}

append_char(*fmt);
Expand All @@ -374,7 +375,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH MessageBuilder(const char* fmt, Args&&... args)
{
buffer[SafeLength] = 0;
append(fmt, std::forward<Args>(args)...);
append(fmt, stl::forward<Args>(args)...);
append_char('\0');
}

Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/ds_core/seqset.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../aal/aal.h"
#include "../ds_core/ds_core.h"
#include "snmalloc/stl/type_traits.h"
#include "snmalloc/stl/utility.h"

#include <stdint.h>

Expand Down Expand Up @@ -98,7 +99,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH bool is_empty()
{
static_assert(
stl::is_same_v<Node, decltype(std::declval<T>().node)>,
stl::is_same_v<Node, decltype(stl::declval<T>().node)>,
"T->node must be Node for T");
head.invariant();
return head.next == &head;
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/mem/backend_concept.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# include "../ds/ds.h"
# include "sizeclasstable.h"

# include <cstddef>
# include <stddef.h>

namespace snmalloc
{
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace snmalloc
{
Backend::alloc_chunk(local_state, size, ras, sizeclass)
} -> ConceptSame<
std::pair<capptr::Chunk<void>, typename Backend::SlabMetadata*>>;
stl::Pair<capptr::Chunk<void>, typename Backend::SlabMetadata*>>;
} &&
requires(LocalState* local_state, size_t size) {
{
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/mem/backend_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace snmalloc
constexpr SNMALLOC_FAST_PATH auto has_domesticate(int) -> stl::enable_if_t<
stl::is_same_v<
decltype(Config::capptr_domesticate(
std::declval<typename Config::LocalState*>(),
std::declval<CapPtr<T, B>>())),
stl::declval<typename Config::LocalState*>(),
stl::declval<CapPtr<T, B>>())),
CapPtr<
T,
typename B::template with_wildness<
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/mem/freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ namespace snmalloc
template<bool RANDOM_ = RANDOM>
stl::enable_if_t<
!RANDOM_,
std::pair<
stl::Pair<
Object::BHeadPtr<BView, BQueue>,
Object::BHeadPtr<BView, BQueue>>>
extract_segment(const FreeListKey& key, address_t key_tweak)
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/mem/localalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
# include "external_alloc.h"
#endif

#include "snmalloc/stl/utility.h"

#include <string.h>
#include <utility>

namespace snmalloc
{
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/mem/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ namespace snmalloc
* available objects for this slab metadata.
*/
template<typename Domesticator>
static SNMALLOC_FAST_PATH std::pair<freelist::HeadPtr, bool>
static SNMALLOC_FAST_PATH stl::Pair<freelist::HeadPtr, bool>
alloc_free_list(
Domesticator domesticate,
FrontendSlabMetadata* meta,
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/mem/remoteallocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace snmalloc
}

template<SNMALLOC_CONCEPT(IsConfigLazy) Config, typename Domesticator_queue>
SNMALLOC_FAST_PATH static std::pair<freelist::HeadPtr, uint16_t>
SNMALLOC_FAST_PATH static stl::Pair<freelist::HeadPtr, uint16_t>
open_free_ring(
capptr::Alloc<BatchedRemoteMessage> m,
size_t objsize,
Expand Down Expand Up @@ -242,7 +242,7 @@ namespace snmalloc
}

template<SNMALLOC_CONCEPT(IsConfigLazy) Config, typename Domesticator_queue>
SNMALLOC_FAST_PATH static std::pair<freelist::HeadPtr, uint16_t>
SNMALLOC_FAST_PATH static stl::Pair<freelist::HeadPtr, uint16_t>
open_free_ring(
capptr::Alloc<SingletonRemoteMessage> m,
size_t,
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ namespace snmalloc
template<size_t BufferSize, typename... Args>
[[noreturn]] inline void report_fatal_error(Args... args)
{
MessageBuilder<BufferSize> msg{std::forward<Args>(args)...};
MessageBuilder<BufferSize> msg{stl::forward<Args>(args)...};
DefaultPal::error(msg.get_message());
}

template<size_t BufferSize, typename... Args>
inline void message(Args... args)
{
MessageBuilder<BufferSize> msg{std::forward<Args>(args)...};
MessageBuilder<BufferSize> msg{stl::forward<Args>(args)...};
MessageBuilder<BufferSize> msg_tid{
"{}: {}", debug_get_tid(), msg.get_message()};
DefaultPal::message(msg_tid.get_message());
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#if defined(SNMALLOC_BACKTRACE_HEADER)
# include SNMALLOC_BACKTRACE_HEADER
#endif
#include "snmalloc/stl/utility.h"

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
Expand All @@ -15,7 +17,6 @@
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
#include <utility>

#if __has_include(<sys/random.h>)
# include <sys/random.h>
Expand Down
13 changes: 13 additions & 0 deletions src/snmalloc/stl/cxx/utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <utility>

namespace snmalloc
{
namespace stl
{
using std::declval;
using std::forward;
using std::move;
template<class T1, class T2>
using Pair = std::pair<T1, T2>;
} // namespace stl
} // namespace snmalloc
16 changes: 16 additions & 0 deletions src/snmalloc/stl/gnu/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,5 +390,21 @@ namespace snmalloc
template<class T>
using add_const_t = const T;

#if __has_builtin(__is_lvalue_reference)
template<class T>
inline constexpr bool is_lvalue_reference_v = __is_lvalue_reference(T);
#else
template<typename T>
struct is_lvalue_reference : public false_type
{};

template<typename T>
struct is_lvalue_reference<T&> : public true_type
{};

template<typename T>
inline constexpr bool is_lvalue_reference_v = is_lvalue_reference<T>::value;
#endif

} // namespace stl
} // namespace snmalloc
68 changes: 68 additions & 0 deletions src/snmalloc/stl/gnu/utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once

#include "snmalloc/stl/type_traits.h"

// This is used by clang to provide better analysis of lifetimes.
#if __has_cpp_attribute(_Clang::__lifetimebound__)
# define SNMALLOC_LIFETIMEBOUND [[_Clang::__lifetimebound__]]
#else
# define SNMALLOC_LIFETIMEBOUND
#endif

namespace snmalloc
{
namespace stl
{
template<class T>
[[nodiscard]] inline constexpr T&&
forward(remove_reference_t<T>& ref) noexcept
{
return static_cast<T&&>(ref);
}

template<class T>
[[nodiscard]] inline constexpr T&&
forward(SNMALLOC_LIFETIMEBOUND remove_reference_t<T>&& ref) noexcept
{
static_assert(
!is_lvalue_reference_v<T>, "cannot forward an rvalue as an lvalue");
return static_cast<T&&>(ref);
}

template<class T>
[[nodiscard]] inline constexpr remove_reference_t<T>&&
move(SNMALLOC_LIFETIMEBOUND T&& ref) noexcept
{
#ifdef __clang__
using U [[gnu::nodebug]] = remove_reference_t<T>;
#else
using U = remove_reference_t<T>;
#endif
return static_cast<U&&>(ref);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
// First try instantiation with reference types, then fall back to value
// types. Use int to prioritize reference types.
template<class T>
T&& declval_impl(int);
template<class T>
T declval_impl(long);
#pragma GCC diagnostic pop

template<class T>
constexpr inline decltype(declval_impl<T>(0)) declval() noexcept
{
static_assert(
!is_same_v<T, T>, "declval cannot be used in an evaluation context");
}

template<class T1, class T2>
struct Pair
{
T1 first;
T2 second;
};
} // namespace stl
} // namespace snmalloc
9 changes: 9 additions & 0 deletions src/snmalloc/stl/utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "snmalloc/stl/common.h"

#if SNMALLOC_USE_SELF_VENDORED_STL
# include "snmalloc/stl/gnu/utility.h"
#else
# include "snmalloc/stl/cxx/utility.h"
#endif

0 comments on commit 0027f02

Please sign in to comment.