From b47b92749f87786ccbf0e6487654ceac9b7cdfb7 Mon Sep 17 00:00:00 2001 From: schrodingerzhu Date: Thu, 9 Jan 2025 20:14:13 +0800 Subject: [PATCH] [wip] address CRs --- src/snmalloc/backend_helpers/buddy.h | 5 ++- src/snmalloc/ds_core/bits.h | 8 ++--- src/snmalloc/ds_core/helpers.h | 10 +++--- src/snmalloc/ds_core/redblacktree.h | 42 +++++++++------------- src/snmalloc/global/memcpy.h | 3 +- src/snmalloc/mem/corealloc.h | 3 +- src/snmalloc/mem/freelist.h | 6 ++-- src/snmalloc/mem/remotecache.h | 6 ++-- src/snmalloc/pal/pal_linux.h | 6 ++-- src/snmalloc/pal/pal_posix.h | 4 +-- src/snmalloc/stl/array.h | 9 +++++ src/snmalloc/stl/cxx/array.h | 15 ++++++++ src/snmalloc/stl/cxx/utility.h | 1 + src/snmalloc/stl/gnu/algorithm.h | 53 ---------------------------- src/snmalloc/stl/gnu/array.h | 53 +++++++--------------------- src/snmalloc/stl/gnu/utility.h | 8 +++++ 16 files changed, 86 insertions(+), 146 deletions(-) create mode 100644 src/snmalloc/stl/array.h create mode 100644 src/snmalloc/stl/cxx/array.h delete mode 100644 src/snmalloc/stl/gnu/algorithm.h diff --git a/src/snmalloc/backend_helpers/buddy.h b/src/snmalloc/backend_helpers/buddy.h index 21ab0acdb..58cafacb1 100644 --- a/src/snmalloc/backend_helpers/buddy.h +++ b/src/snmalloc/backend_helpers/buddy.h @@ -1,7 +1,6 @@ #pragma once #include "../ds/ds.h" -#include "snmalloc/proxy/algorithm.h" namespace snmalloc { @@ -24,7 +23,7 @@ namespace snmalloc RBTree tree{}; }; - proxy::Array entries{}; + stl::Array entries{}; // All RBtrees at or above this index should be empty. size_t empty_at_or_above{0}; @@ -168,7 +167,7 @@ namespace snmalloc { if (Rep::equal(Rep::null, addr) || Rep::compare(e, addr)) { - addr = proxy::exchange(e, addr); + addr = stl::exchange(e, addr); } } diff --git a/src/snmalloc/ds_core/bits.h b/src/snmalloc/ds_core/bits.h index ea932c7cb..1ff6426ea 100644 --- a/src/snmalloc/ds_core/bits.h +++ b/src/snmalloc/ds_core/bits.h @@ -371,9 +371,9 @@ namespace snmalloc } /** - * Implementation of `proxy::min` + * Implementation of `std::min` * - * `proxy::min` is in ``, so pulls in a lot of unneccessary code + * `bits::min` is in ``, so pulls in a lot of unneccessary code * We write our own to reduce the code that potentially needs reviewing. */ template @@ -383,9 +383,9 @@ namespace snmalloc } /** - * Implementation of `proxy::max` + * Implementation of `std::max` * - * `proxy::max` is in ``, so pulls in a lot of unneccessary code + * `bits::max` is in ``, so pulls in a lot of unneccessary code * We write our own to reduce the code that potentially needs reviewing. */ template diff --git a/src/snmalloc/ds_core/helpers.h b/src/snmalloc/ds_core/helpers.h index fc204f461..3008da4f5 100644 --- a/src/snmalloc/ds_core/helpers.h +++ b/src/snmalloc/ds_core/helpers.h @@ -50,7 +50,7 @@ namespace snmalloc }; static constexpr size_t rlength = bits::next_pow2_const(length); - proxy::Array array; + stl::Array array; public: constexpr const T& operator[](const size_t i) const @@ -65,7 +65,7 @@ namespace snmalloc }; #else template - using ModArray = proxy::Array; + using ModArray = stl::Array; #endif /** @@ -144,7 +144,7 @@ namespace snmalloc /** * The buffer that is used to store the formatted output. */ - proxy::Array buffer; + stl::Array buffer; /** * Space in the buffer, excluding a trailing null terminator. @@ -254,7 +254,7 @@ namespace snmalloc append_char('-'); s = 0 - s; } - proxy::Array buf{{0}}; + stl::Array buf{{0}}; const char digits[] = "0123456789"; for (long i = static_cast(buf.size() - 1); i >= 0; i--) { @@ -284,7 +284,7 @@ namespace snmalloc { append_char('0'); append_char('x'); - proxy::Array buf{{0}}; + stl::Array buf{{0}}; const char hexdigits[] = "0123456789abcdef"; // Length of string including null terminator static_assert(sizeof(hexdigits) == 0x11); diff --git a/src/snmalloc/ds_core/redblacktree.h b/src/snmalloc/ds_core/redblacktree.h index 0e3814ded..7fd3c61b8 100644 --- a/src/snmalloc/ds_core/redblacktree.h +++ b/src/snmalloc/ds_core/redblacktree.h @@ -1,6 +1,6 @@ #pragma once -#include "snmalloc/proxy/array.h" +#include "snmalloc/stl/array.h" #include #include @@ -18,9 +18,9 @@ namespace snmalloc */ template concept RBRepTypes = requires() { - typename Rep::Handle; - typename Rep::Contents; - }; + typename Rep::Handle; + typename Rep::Contents; + }; /** * The representation must define operations on the holder and contents @@ -41,29 +41,17 @@ namespace snmalloc template concept RBRepMethods = requires(typename Rep::Handle hp, typename Rep::Contents k, bool b) { - { - Rep::get(hp) - } -> ConceptSame; - { - Rep::set(hp, k) - } -> ConceptSame; - { - Rep::is_red(k) - } -> ConceptSame; - { - Rep::set_red(k, b) - } -> ConceptSame; - { - Rep::ref(b, k) - } -> ConceptSame; - { - Rep::null - } -> ConceptSameModRef; + { Rep::get(hp) } -> ConceptSame; + { Rep::set(hp, k) } -> ConceptSame; + { Rep::is_red(k) } -> ConceptSame; + { Rep::set_red(k, b) } -> ConceptSame; + { Rep::ref(b, k) } -> ConceptSame; + { Rep::null } -> ConceptSameModRef; { typename Rep::Handle{const_cast< stl::remove_const_t>*>( &Rep::root)} - } -> ConceptSame; + } -> ConceptSame; }; template @@ -260,7 +248,7 @@ namespace snmalloc { friend class RBTree; - proxy::Array path; + stl::Array path; size_t length = 0; RBPath(typename Rep::Handle root) @@ -496,7 +484,8 @@ namespace snmalloc */ path.move(true); while (path.move(false)) - {} + { + } K curr = path.curr(); @@ -747,7 +736,8 @@ namespace snmalloc auto path = get_root_path(); while (path.move(true)) - {} + { + } K result = path.curr(); diff --git a/src/snmalloc/global/memcpy.h b/src/snmalloc/global/memcpy.h index 3664aed2d..eaac1e97f 100644 --- a/src/snmalloc/global/memcpy.h +++ b/src/snmalloc/global/memcpy.h @@ -1,6 +1,5 @@ #pragma once #include "bounds_checks.h" -#include "snmalloc/proxy/algorithm.h" namespace snmalloc { @@ -157,7 +156,7 @@ namespace snmalloc */ SNMALLOC_UNUSED_FUNCTION static constexpr size_t LargestRegisterSize = - proxy::max(sizeof(uint64_t), sizeof(void*)); + bits::max(sizeof(uint64_t), sizeof(void*)); /** * Hook for architecture-specific optimisations. diff --git a/src/snmalloc/mem/corealloc.h b/src/snmalloc/mem/corealloc.h index e32838935..f82ccc1c2 100644 --- a/src/snmalloc/mem/corealloc.h +++ b/src/snmalloc/mem/corealloc.h @@ -6,7 +6,6 @@ #include "pool.h" #include "remotecache.h" #include "sizeclasstable.h" -#include "snmalloc/stl/algorithm.h" #include "snmalloc/stl/new.h" #include "ticker.h" @@ -223,7 +222,7 @@ namespace snmalloc pointer_offset(curr, rsize).template as_static()) { size_t insert_index = entropy.sample(count); - curr->next = proxy::exchange( + curr->next = stl::exchange( pointer_offset(bumpptr, insert_index * rsize) .template as_static() ->next, diff --git a/src/snmalloc/mem/freelist.h b/src/snmalloc/mem/freelist.h index 60c744f18..fd91f6dcc 100644 --- a/src/snmalloc/mem/freelist.h +++ b/src/snmalloc/mem/freelist.h @@ -701,11 +701,11 @@ namespace snmalloc */ // Pointer to the first element. - proxy::Array head{nullptr}; + stl::Array head{nullptr}; // Pointer to the reference to the last element. // In the empty case end[i] == &head[i] // This enables branch free enqueuing. - proxy::Array end{nullptr}; + stl::Array end{nullptr}; [[nodiscard]] Object::BQueuePtr* cast_end(uint32_t ix) const { @@ -724,7 +724,7 @@ namespace snmalloc } SNMALLOC_NO_UNIQUE_ADDRESS - proxy::Array length{}; + stl::Array length{}; public: constexpr Builder() = default; diff --git a/src/snmalloc/mem/remotecache.h b/src/snmalloc/mem/remotecache.h index ec531d573..3d5ed70b8 100644 --- a/src/snmalloc/mem/remotecache.h +++ b/src/snmalloc/mem/remotecache.h @@ -32,8 +32,8 @@ namespace snmalloc { static_assert(RINGS > 0); - proxy::Array, RINGS> open_builder; - proxy::Array open_meta = {0}; + stl::Array, RINGS> open_builder; + stl::Array open_meta = {0}; SNMALLOC_FAST_PATH size_t ring_set(typename Config::PagemapEntry::SlabMetadata* meta) @@ -190,7 +190,7 @@ namespace snmalloc template struct RemoteDeallocCache { - proxy::Array, REMOTE_SLOTS> list; + stl::Array, REMOTE_SLOTS> list; RemoteDeallocCacheBatchingImpl batching; diff --git a/src/snmalloc/pal/pal_linux.h b/src/snmalloc/pal/pal_linux.h index 836644a28..ffa16eb25 100644 --- a/src/snmalloc/pal/pal_linux.h +++ b/src/snmalloc/pal/pal_linux.h @@ -3,7 +3,7 @@ #if defined(__linux__) # include "../ds_core/ds_core.h" # include "pal_posix.h" -# include "snmalloc/proxy/array.h" +# include "snmalloc/stl/array.h" # include # include @@ -184,8 +184,8 @@ namespace snmalloc // protected routine. if (false == syscall_not_working.load(stl::memory_order_relaxed)) { - auto current = proxy::begin(buffer); - auto target = proxy::end(buffer); + auto current = stl::begin(buffer); + auto target = stl::end(buffer); while (auto length = target - current) { // Reading data via syscall from system entropy pool. diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 0afb83ae7..463b71071 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -418,8 +418,8 @@ namespace snmalloc auto fd = open("/dev/urandom", flags, 0); if (fd > 0) { - auto current = proxy::begin(buffer); - auto target = proxy::end(buffer); + auto current = stl::begin(buffer); + auto target = stl::end(buffer); while (auto length = static_cast(target - current)) { ret = read(fd, current, length); diff --git a/src/snmalloc/stl/array.h b/src/snmalloc/stl/array.h new file mode 100644 index 000000000..f398da0e0 --- /dev/null +++ b/src/snmalloc/stl/array.h @@ -0,0 +1,9 @@ +#pragma once + +#include "snmalloc/stl/common.h" + +#if SNMALLOC_USE_SELF_VENDORED_STL +# include "snmalloc/stl/gnu/array.h" +#else +# include "snmalloc/stl/cxx/array.h" +#endif diff --git a/src/snmalloc/stl/cxx/array.h b/src/snmalloc/stl/cxx/array.h new file mode 100644 index 000000000..9918f3087 --- /dev/null +++ b/src/snmalloc/stl/cxx/array.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace snmalloc +{ + namespace proxy + { + template + using Array = std::array; + + using std::begin; + using std::end; + } // namespace proxy +} // namespace snmalloc diff --git a/src/snmalloc/stl/cxx/utility.h b/src/snmalloc/stl/cxx/utility.h index 0853239b2..93ee5df18 100644 --- a/src/snmalloc/stl/cxx/utility.h +++ b/src/snmalloc/stl/cxx/utility.h @@ -5,6 +5,7 @@ namespace snmalloc namespace stl { using std::declval; + using std::exchange; using std::forward; using std::move; template diff --git a/src/snmalloc/stl/gnu/algorithm.h b/src/snmalloc/stl/gnu/algorithm.h deleted file mode 100644 index db3c38040..000000000 --- a/src/snmalloc/stl/gnu/algorithm.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include "snmalloc/ds_core/defines.h" - -#ifndef SNMALLOC_USE_SELF_VENDORED_STL -# define SNMALLOC_USE_SELF_VENDORED_STL 0 -#endif - -#if SNMALLOC_USE_SELF_VENDORED_STL -# include "snmalloc/proxy/utility.h" -# if !defined(__GNUC__) && !defined(__clang__) -# error "cannot use vendored STL without GNU/Clang extensions" -# endif - -namespace snmalloc -{ - namespace proxy - { - template - constexpr SNMALLOC_FAST_PATH const T& max(const T& a, const T& b) - { - return a < b ? b : a; - } - - template - constexpr SNMALLOC_FAST_PATH const T& min(const T& a, const T& b) - { - return a < b ? a : b; - } - - template - constexpr SNMALLOC_FAST_PATH A exchange(A& obj, B&& new_value) - { - A old_value = move(obj); - obj = forward(new_value); - return old_value; - } - - } // namespace proxy -} // namespace snmalloc -#else -# include - -namespace snmalloc -{ - namespace proxy - { - using std::exchange; - using std::max; - using std::min; - } // namespace proxy -} // namespace snmalloc -#endif diff --git a/src/snmalloc/stl/gnu/array.h b/src/snmalloc/stl/gnu/array.h index 2733ba388..e4f2630a5 100644 --- a/src/snmalloc/stl/gnu/array.h +++ b/src/snmalloc/stl/gnu/array.h @@ -1,29 +1,17 @@ #pragma once -#include "snmalloc/ds_core/defines.h" - -#ifndef SNMALLOC_USE_SELF_VENDORED_STL -# define SNMALLOC_USE_SELF_VENDORED_STL 0 -#endif - -#if SNMALLOC_USE_SELF_VENDORED_STL - -# if !defined(__GNUC__) && !defined(__clang__) -# error "cannot use vendored STL without GNU/Clang extensions" -# endif - -# include +#include namespace snmalloc { - namespace proxy + namespace stl { template struct Array { // N is potentially 0, so we mark it with __extension__ attribute. - // expose this to public to allow aggregate initialization - __extension__ T storage[N]; + // Expose this to public to allow aggregate initialization + __extension__ T _storage[N]; [[nodiscard]] constexpr SNMALLOC_FAST_PATH size_t size() const { @@ -32,12 +20,12 @@ namespace snmalloc constexpr T& operator[](size_t i) { - return storage[i]; + return _storage[i]; } constexpr const T& operator[](size_t i) const { - return storage[i]; + return _storage[i]; } using value_type = T; @@ -47,32 +35,32 @@ namespace snmalloc [[nodiscard]] constexpr SNMALLOC_FAST_PATH iterator begin() { - return &storage[0]; + return &_storage[0]; } [[nodiscard]] constexpr SNMALLOC_FAST_PATH const_iterator begin() const { - return &storage[0]; + return &_storage[0]; } [[nodiscard]] constexpr SNMALLOC_FAST_PATH iterator end() { - return &storage[N]; + return &_storage[N]; } [[nodiscard]] constexpr SNMALLOC_FAST_PATH const_iterator end() const { - return &storage[N]; + return &_storage[N]; } [[nodiscard]] constexpr SNMALLOC_FAST_PATH T* data() { - return &storage[0]; + return &_storage[0]; } [[nodiscard]] constexpr SNMALLOC_FAST_PATH const T* data() const { - return &storage[0]; + return &_storage[0]; } }; @@ -123,20 +111,5 @@ namespace snmalloc { return &a[N]; } - } // namespace proxy -} // namespace snmalloc -#else -# include - -namespace snmalloc -{ - namespace proxy - { - template - using Array = std::array; - - using std::begin; - using std::end; - } // namespace proxy + } // namespace stl } // namespace snmalloc -#endif diff --git a/src/snmalloc/stl/gnu/utility.h b/src/snmalloc/stl/gnu/utility.h index 193613097..03a07c921 100644 --- a/src/snmalloc/stl/gnu/utility.h +++ b/src/snmalloc/stl/gnu/utility.h @@ -64,5 +64,13 @@ namespace snmalloc T1 first; T2 second; }; + + template + constexpr SNMALLOC_FAST_PATH A exchange(A& obj, B&& new_value) + { + A old_value = move(obj); + obj = forward(new_value); + return old_value; + } } // namespace stl } // namespace snmalloc