[proxy](6/n) self-vendor utility headers (#721)

This commit is contained in:
Schrodinger ZHU Yifan
2025-01-09 18:25:35 +08:00
committed by GitHub
parent e874617d8e
commit 0027f02396
22 changed files with 141 additions and 28 deletions

View File

@@ -222,7 +222,7 @@ We can now provide our new version of the `with` function that uses the combinin
template<typename F> template<typename F>
inline void with(CombiningLock& lock, F&& f) inline void with(CombiningLock& lock, F&& f)
{ {
CombiningLockNodeTempl<F> node(std::forward<F>(f)); CombiningLockNodeTempl<F> node(stl::forward<F>(f));
// **************ACQUIRE************** // **************ACQUIRE**************
// Add ourselves to the end of the queue. // Add ourselves to the end of the queue.

View File

@@ -16,8 +16,9 @@
# define SNMALLOC_TICK_USE_CLOCK_GETTIME # define SNMALLOC_TICK_USE_CLOCK_GETTIME
# endif # endif
#endif #endif
#include "snmalloc/stl/utility.h"
#include <stdint.h> #include <stdint.h>
#include <utility>
#ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME #ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME
# include <chrono> # include <chrono>

View File

@@ -87,7 +87,7 @@ namespace snmalloc
* (remote, sizeclass, slab_metadata) * (remote, sizeclass, slab_metadata)
* where slab_metadata, is the second element of the pair return. * 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, LocalState& local_state,
size_t size, size_t size,
uintptr_t ras, uintptr_t ras,

View File

@@ -1,8 +1,11 @@
#pragma once #pragma once
#include "../backend_helpers/backend_helpers.h" #include "../backend_helpers/backend_helpers.h"
#include "snmalloc/stl/type_traits.h"
#include "standard_range.h" #include "standard_range.h"
#include <stddef.h>
namespace snmalloc namespace snmalloc
{ {
/** /**

View File

@@ -3,8 +3,7 @@
#include "../ds/ds.h" #include "../ds/ds.h"
#include "../mem/mem.h" #include "../mem/mem.h"
#include "snmalloc/stl/atomic.h" #include "snmalloc/stl/atomic.h"
#include "snmalloc/stl/utility.h"
#include <utility>
namespace snmalloc namespace snmalloc
{ {
@@ -115,7 +114,7 @@ namespace snmalloc
*/ */
template<bool fixed_range_ = fixed_range> template<bool fixed_range_ = fixed_range>
static SNMALLOC_FAST_PATH 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() get_bounds()
{ {
static_assert(fixed_range_ == fixed_range, "Don't set SFINAE parameter!"); static_assert(fixed_range_ == fixed_range, "Don't set SFINAE parameter!");

View File

@@ -256,7 +256,7 @@ namespace snmalloc
reinterpret_cast<CombiningLockNodeTempl*>(self); reinterpret_cast<CombiningLockNodeTempl*>(self);
self_templ->f(); self_templ->f();
}), }),
f(std::forward<F>(f_)) f(stl::forward<F>(f_))
{ {
attach_slow(lock); attach_slow(lock);
} }
@@ -290,6 +290,6 @@ namespace snmalloc
// There is contention for the lock, we need to take the slow path // There is contention for the lock, we need to take the slow path
// with the queue. // with the queue.
CombiningLockNodeTempl<F> node(lock, std::forward<F>(f)); CombiningLockNodeTempl<F> node(lock, stl::forward<F>(f));
} }
} // namespace snmalloc } // namespace snmalloc

View File

@@ -115,7 +115,7 @@ namespace snmalloc
* Returns usable range after pagemap has been allocated * Returns usable range after pagemap has been allocated
*/ */
template<bool has_bounds_ = has_bounds> 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) init(void* b, size_t s)
{ {
SNMALLOC_ASSERT(!is_initialised()); SNMALLOC_ASSERT(!is_initialised());
@@ -250,7 +250,7 @@ namespace snmalloc
} }
template<bool has_bounds_ = has_bounds> 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()); SNMALLOC_ASSERT(is_initialised());

View File

@@ -24,7 +24,7 @@ namespace snmalloc
constexpr DebugFlagWord() = default; constexpr DebugFlagWord() = default;
template<typename... Args> template<typename... Args>
constexpr DebugFlagWord(Args&&... args) : flag(std::forward<Args>(args)...) constexpr DebugFlagWord(Args&&... args) : flag(stl::forward<Args>(args)...)
{} {}
/** /**
@@ -87,7 +87,7 @@ namespace snmalloc
template<typename... Args> template<typename... Args>
constexpr ReleaseFlagWord(Args&&... args) constexpr ReleaseFlagWord(Args&&... args)
: flag(std::forward<Args>(args)...) : flag(stl::forward<Args>(args)...)
{} {}
void set_owner() {} void set_owner() {}

View File

@@ -3,6 +3,7 @@
#include "bits.h" #include "bits.h"
#include "snmalloc/ds_core/defines.h" #include "snmalloc/ds_core/defines.h"
#include "snmalloc/stl/type_traits.h" #include "snmalloc/stl/type_traits.h"
#include "snmalloc/stl/utility.h"
#include <array> #include <array>
#include <stddef.h> #include <stddef.h>
@@ -357,8 +358,8 @@ namespace snmalloc
if (fmt[0] == '{' && fmt[1] == '}') if (fmt[0] == '{' && fmt[1] == '}')
{ {
append(std::forward<Head>(head)); append(stl::forward<Head>(head));
return append(fmt + 2, std::forward<Tail>(tail)...); return append(fmt + 2, stl::forward<Tail>(tail)...);
} }
append_char(*fmt); append_char(*fmt);
@@ -374,7 +375,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH MessageBuilder(const char* fmt, Args&&... args) SNMALLOC_FAST_PATH MessageBuilder(const char* fmt, Args&&... args)
{ {
buffer[SafeLength] = 0; buffer[SafeLength] = 0;
append(fmt, std::forward<Args>(args)...); append(fmt, stl::forward<Args>(args)...);
append_char('\0'); append_char('\0');
} }

View File

@@ -3,6 +3,7 @@
#include "../aal/aal.h" #include "../aal/aal.h"
#include "../ds_core/ds_core.h" #include "../ds_core/ds_core.h"
#include "snmalloc/stl/type_traits.h" #include "snmalloc/stl/type_traits.h"
#include "snmalloc/stl/utility.h"
#include <stdint.h> #include <stdint.h>
@@ -98,7 +99,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH bool is_empty() SNMALLOC_FAST_PATH bool is_empty()
{ {
static_assert( 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"); "T->node must be Node for T");
head.invariant(); head.invariant();
return head.next == &head; return head.next == &head;

View File

@@ -4,7 +4,7 @@
# include "../ds/ds.h" # include "../ds/ds.h"
# include "sizeclasstable.h" # include "sizeclasstable.h"
# include <cstddef> # include <stddef.h>
namespace snmalloc namespace snmalloc
{ {
@@ -107,7 +107,7 @@ namespace snmalloc
{ {
Backend::alloc_chunk(local_state, size, ras, sizeclass) Backend::alloc_chunk(local_state, size, ras, sizeclass)
} -> ConceptSame< } -> ConceptSame<
std::pair<capptr::Chunk<void>, typename Backend::SlabMetadata*>>; stl::Pair<capptr::Chunk<void>, typename Backend::SlabMetadata*>>;
} && } &&
requires(LocalState* local_state, size_t size) { requires(LocalState* local_state, size_t size) {
{ {

View File

@@ -46,8 +46,8 @@ namespace snmalloc
constexpr SNMALLOC_FAST_PATH auto has_domesticate(int) -> stl::enable_if_t< constexpr SNMALLOC_FAST_PATH auto has_domesticate(int) -> stl::enable_if_t<
stl::is_same_v< stl::is_same_v<
decltype(Config::capptr_domesticate( decltype(Config::capptr_domesticate(
std::declval<typename Config::LocalState*>(), stl::declval<typename Config::LocalState*>(),
std::declval<CapPtr<T, B>>())), stl::declval<CapPtr<T, B>>())),
CapPtr< CapPtr<
T, T,
typename B::template with_wildness< typename B::template with_wildness<

View File

@@ -906,7 +906,7 @@ namespace snmalloc
template<bool RANDOM_ = RANDOM> template<bool RANDOM_ = RANDOM>
stl::enable_if_t< stl::enable_if_t<
!RANDOM_, !RANDOM_,
std::pair< stl::Pair<
Object::BHeadPtr<BView, BQueue>, Object::BHeadPtr<BView, BQueue>,
Object::BHeadPtr<BView, BQueue>>> Object::BHeadPtr<BView, BQueue>>>
extract_segment(const FreeListKey& key, address_t key_tweak) extract_segment(const FreeListKey& key, address_t key_tweak)

View File

@@ -21,8 +21,9 @@
# include "external_alloc.h" # include "external_alloc.h"
#endif #endif
#include "snmalloc/stl/utility.h"
#include <string.h> #include <string.h>
#include <utility>
namespace snmalloc namespace snmalloc
{ {

View File

@@ -620,7 +620,7 @@ namespace snmalloc
* available objects for this slab metadata. * available objects for this slab metadata.
*/ */
template<typename Domesticator> template<typename Domesticator>
static SNMALLOC_FAST_PATH std::pair<freelist::HeadPtr, bool> static SNMALLOC_FAST_PATH stl::Pair<freelist::HeadPtr, bool>
alloc_free_list( alloc_free_list(
Domesticator domesticate, Domesticator domesticate,
FrontendSlabMetadata* meta, FrontendSlabMetadata* meta,

View File

@@ -104,7 +104,7 @@ namespace snmalloc
} }
template<SNMALLOC_CONCEPT(IsConfigLazy) Config, typename Domesticator_queue> 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( open_free_ring(
capptr::Alloc<BatchedRemoteMessage> m, capptr::Alloc<BatchedRemoteMessage> m,
size_t objsize, size_t objsize,
@@ -242,7 +242,7 @@ namespace snmalloc
} }
template<SNMALLOC_CONCEPT(IsConfigLazy) Config, typename Domesticator_queue> 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( open_free_ring(
capptr::Alloc<SingletonRemoteMessage> m, capptr::Alloc<SingletonRemoteMessage> m,
size_t, size_t,

View File

@@ -168,14 +168,14 @@ namespace snmalloc
template<size_t BufferSize, typename... Args> template<size_t BufferSize, typename... Args>
[[noreturn]] inline void report_fatal_error(Args... 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()); DefaultPal::error(msg.get_message());
} }
template<size_t BufferSize, typename... Args> template<size_t BufferSize, typename... Args>
inline void message(Args... args) inline void message(Args... args)
{ {
MessageBuilder<BufferSize> msg{std::forward<Args>(args)...}; MessageBuilder<BufferSize> msg{stl::forward<Args>(args)...};
MessageBuilder<BufferSize> msg_tid{ MessageBuilder<BufferSize> msg_tid{
"{}: {}", debug_get_tid(), msg.get_message()}; "{}: {}", debug_get_tid(), msg.get_message()};
DefaultPal::message(msg_tid.get_message()); DefaultPal::message(msg_tid.get_message());

View File

@@ -5,6 +5,8 @@
#if defined(SNMALLOC_BACKTRACE_HEADER) #if defined(SNMALLOC_BACKTRACE_HEADER)
# include SNMALLOC_BACKTRACE_HEADER # include SNMALLOC_BACKTRACE_HEADER
#endif #endif
#include "snmalloc/stl/utility.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
@@ -15,7 +17,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <unistd.h> #include <unistd.h>
#include <utility>
#if __has_include(<sys/random.h>) #if __has_include(<sys/random.h>)
# include <sys/random.h> # include <sys/random.h>

View File

@@ -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

View File

@@ -390,5 +390,21 @@ namespace snmalloc
template<class T> template<class T>
using add_const_t = const 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 stl
} // namespace snmalloc } // namespace snmalloc

View File

@@ -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

View File

@@ -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