From 73558e350b34e217c8f820eb973e31aa6f536eec Mon Sep 17 00:00:00 2001 From: schrodingerzhu Date: Sat, 21 Dec 2024 01:04:09 -0500 Subject: [PATCH] [proxy](5/n) use c headers instead of c++ headers --- src/snmalloc/aal/aal.h | 2 +- src/snmalloc/aal/aal_arm.h | 2 +- src/snmalloc/aal/aal_consts.h | 2 +- src/snmalloc/aal/address.h | 2 +- src/snmalloc/ds/aba.h | 6 +++--- src/snmalloc/ds_core/bits.h | 20 ++++++++++---------- src/snmalloc/ds_core/defines.h | 2 +- src/snmalloc/ds_core/helpers.h | 2 +- src/snmalloc/ds_core/mitigations.h | 2 +- src/snmalloc/ds_core/redblacktree.h | 4 ++-- src/snmalloc/ds_core/seqset.h | 2 +- src/snmalloc/mem/entropy.h | 2 +- src/snmalloc/mem/freelist.h | 2 +- src/snmalloc/mem/localalloc.h | 15 +++++++++++++-- src/snmalloc/mem/ticker.h | 2 +- src/snmalloc/override/jemalloc_compat.cc | 3 ++- src/snmalloc/override/rust.cc | 4 ++-- src/snmalloc/pal/pal_concept.h | 10 +++++----- src/snmalloc/pal/pal_noalloc.h | 2 +- src/snmalloc/pal/pal_posix.h | 3 ++- src/test/func/jemalloc/jemalloc.cc | 3 ++- 21 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/snmalloc/aal/aal.h b/src/snmalloc/aal/aal.h index d6db1df2a..8c97f27c6 100644 --- a/src/snmalloc/aal/aal.h +++ b/src/snmalloc/aal/aal.h @@ -16,7 +16,7 @@ # define SNMALLOC_TICK_USE_CLOCK_GETTIME # endif #endif -#include +#include #include #ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME diff --git a/src/snmalloc/aal/aal_arm.h b/src/snmalloc/aal/aal_arm.h index 11013c44c..c076ce7fc 100644 --- a/src/snmalloc/aal/aal_arm.h +++ b/src/snmalloc/aal/aal_arm.h @@ -12,7 +12,7 @@ # endif #endif -#include +#include namespace snmalloc { diff --git a/src/snmalloc/aal/aal_consts.h b/src/snmalloc/aal/aal_consts.h index 8990a41df..7b3d22a21 100644 --- a/src/snmalloc/aal/aal_consts.h +++ b/src/snmalloc/aal/aal_consts.h @@ -1,5 +1,5 @@ #pragma once -#include +#include namespace snmalloc { diff --git a/src/snmalloc/aal/address.h b/src/snmalloc/aal/address.h index 6c1a760bf..91cce9421 100644 --- a/src/snmalloc/aal/address.h +++ b/src/snmalloc/aal/address.h @@ -1,7 +1,7 @@ #pragma once #include "../ds_core/ds_core.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/ds/aba.h b/src/snmalloc/ds/aba.h index 2d5784822..fac2354af 100644 --- a/src/snmalloc/ds/aba.h +++ b/src/snmalloc/ds/aba.h @@ -27,7 +27,7 @@ namespace snmalloc class ABA { public: - struct alignas(2 * sizeof(std::size_t)) Linked + struct alignas(2 * sizeof(size_t)) Linked { T* ptr{nullptr}; uintptr_t aba{0}; @@ -43,13 +43,13 @@ namespace snmalloc sizeof(Linked) == sizeof(Independent), "Expecting identical struct sizes in union"); static_assert( - sizeof(Linked) == (2 * sizeof(std::size_t)), + sizeof(Linked) == (2 * sizeof(size_t)), "Expecting ABA to be the size of two pointers"); private: union { - alignas(2 * sizeof(std::size_t)) proxy::Atomic linked; + alignas(2 * sizeof(size_t)) proxy::Atomic linked; Independent independent; }; diff --git a/src/snmalloc/ds_core/bits.h b/src/snmalloc/ds_core/bits.h index 3cfe43e71..8a1468a4b 100644 --- a/src/snmalloc/ds_core/bits.h +++ b/src/snmalloc/ds_core/bits.h @@ -1,7 +1,6 @@ #pragma once -#include -#include +#include // #define USE_LZCNT @@ -9,9 +8,10 @@ #include "snmalloc/proxy/atomic.h" #include "snmalloc/proxy/type_traits.h" -#include -#include +#include +#include #if defined(_MSC_VER) +# include # include #endif @@ -95,15 +95,15 @@ namespace snmalloc return BITS - index - 1; # endif #else - if constexpr (proxy::is_same_v) + if constexpr (proxy::is_same_v) { return static_cast(__builtin_clzl(x)); } - else if constexpr (proxy::is_same_v) + else if constexpr (proxy::is_same_v) { return static_cast(__builtin_clzll(x)); } - else if constexpr (proxy::is_same_v) + else if constexpr (proxy::is_same_v) { return static_cast(__builtin_clz(x)); } @@ -182,15 +182,15 @@ namespace snmalloc return _tzcnt_u32(static_cast(x)); # endif #else - if constexpr (proxy::is_same_v) + if constexpr (proxy::is_same_v) { return static_cast(__builtin_ctzl(x)); } - else if constexpr (proxy::is_same_v) + else if constexpr (proxy::is_same_v) { return static_cast(__builtin_ctzll(x)); } - else if constexpr (proxy::is_same_v) + else if constexpr (proxy::is_same_v) { return static_cast(__builtin_ctz(x)); } diff --git a/src/snmalloc/ds_core/defines.h b/src/snmalloc/ds_core/defines.h index d50939ad0..9638b5527 100644 --- a/src/snmalloc/ds_core/defines.h +++ b/src/snmalloc/ds_core/defines.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #if defined(_MSC_VER) && !defined(__clang__) // 28 is FAST_FAIL_INVALID_BUFFER_ACCESS. Not using the symbolic constant to diff --git a/src/snmalloc/ds_core/helpers.h b/src/snmalloc/ds_core/helpers.h index 2c8c55623..d0455800c 100644 --- a/src/snmalloc/ds_core/helpers.h +++ b/src/snmalloc/ds_core/helpers.h @@ -5,7 +5,7 @@ #include "snmalloc/proxy/type_traits.h" #include -#include +#include namespace snmalloc { diff --git a/src/snmalloc/ds_core/mitigations.h b/src/snmalloc/ds_core/mitigations.h index 2370f2951..7742b2df7 100644 --- a/src/snmalloc/ds_core/mitigations.h +++ b/src/snmalloc/ds_core/mitigations.h @@ -1,7 +1,7 @@ #pragma once #include "defines.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/ds_core/redblacktree.h b/src/snmalloc/ds_core/redblacktree.h index 22444e0bb..37b90ddb2 100644 --- a/src/snmalloc/ds_core/redblacktree.h +++ b/src/snmalloc/ds_core/redblacktree.h @@ -1,8 +1,8 @@ #pragma once #include -#include -#include +#include +#include namespace snmalloc { diff --git a/src/snmalloc/ds_core/seqset.h b/src/snmalloc/ds_core/seqset.h index 17648212f..19c65025c 100644 --- a/src/snmalloc/ds_core/seqset.h +++ b/src/snmalloc/ds_core/seqset.h @@ -4,7 +4,7 @@ #include "../ds_core/ds_core.h" #include "snmalloc/proxy/type_traits.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/mem/entropy.h b/src/snmalloc/mem/entropy.h index cbb825238..5669e7307 100644 --- a/src/snmalloc/mem/entropy.h +++ b/src/snmalloc/mem/entropy.h @@ -4,7 +4,7 @@ #include "../pal/pal.h" #include "snmalloc/proxy/type_traits.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/mem/freelist.h b/src/snmalloc/mem/freelist.h index e7f0df7d1..e62ef8ca1 100644 --- a/src/snmalloc/mem/freelist.h +++ b/src/snmalloc/mem/freelist.h @@ -37,7 +37,7 @@ #include "entropy.h" #include "snmalloc/proxy/new.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/mem/localalloc.h b/src/snmalloc/mem/localalloc.h index 8d2be6fa6..101ba7b9e 100644 --- a/src/snmalloc/mem/localalloc.h +++ b/src/snmalloc/mem/localalloc.h @@ -1,5 +1,6 @@ #pragma once +#include "snmalloc/aal/address.h" #if defined(_MSC_VER) # define ALLOCATOR __declspec(allocator) __declspec(restrict) #elif __has_attribute(malloc) @@ -888,8 +889,18 @@ namespace snmalloc auto sizeclass = entry.get_sizeclass(); return snmalloc::remaining_bytes(sizeclass, p); #else - return reinterpret_cast( - std::numeric_limits::max() - p); + constexpr address_t mask = static_cast(-1); + constexpr bool is_signed = mask < 0; + constexpr address_t sign_bit = static_cast(1) + << (CHAR_BIT * sizeof(address_t) - 1); + if constexpr (is_signed) + { + return (mask ^ sign_bit) - p; + } + else + { + return mask - p; + } #endif } diff --git a/src/snmalloc/mem/ticker.h b/src/snmalloc/mem/ticker.h index 2bce041e0..c48ff193e 100644 --- a/src/snmalloc/mem/ticker.h +++ b/src/snmalloc/mem/ticker.h @@ -2,7 +2,7 @@ #include "../ds_core/ds_core.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/override/jemalloc_compat.cc b/src/snmalloc/override/jemalloc_compat.cc index 79206fed7..0f478451a 100644 --- a/src/snmalloc/override/jemalloc_compat.cc +++ b/src/snmalloc/override/jemalloc_compat.cc @@ -1,6 +1,7 @@ #include "override.h" #include +#include #include using namespace snmalloc; @@ -182,7 +183,7 @@ extern "C" return allocm_err_not_moved; } - if (std::numeric_limits::max() - size > extra) + if (SIZE_MAX - size > extra) { alloc_size = f.aligned_size(size + extra); } diff --git a/src/snmalloc/override/rust.cc b/src/snmalloc/override/rust.cc index 4a5dcdaf9..980ba8db5 100644 --- a/src/snmalloc/override/rust.cc +++ b/src/snmalloc/override/rust.cc @@ -1,7 +1,7 @@ #define SNMALLOC_NAME_MANGLE(a) sn_##a #include "snmalloc/snmalloc.h" -#include +#include #ifndef SNMALLOC_EXPORT # define SNMALLOC_EXPORT @@ -39,7 +39,7 @@ extern "C" SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(rust_realloc)( void* p = ThreadAlloc::get().alloc(aligned_new_size); if (p) { - std::memcpy(p, ptr, old_size < new_size ? old_size : new_size); + memcpy(p, ptr, old_size < new_size ? old_size : new_size); ThreadAlloc::get().dealloc(ptr, aligned_old_size); } return p; diff --git a/src/snmalloc/pal/pal_concept.h b/src/snmalloc/pal/pal_concept.h index d3dda8fd2..deab9cb9b 100644 --- a/src/snmalloc/pal/pal_concept.h +++ b/src/snmalloc/pal/pal_concept.h @@ -30,8 +30,8 @@ namespace snmalloc template concept IsPAL_static_sizes = requires() { - typename proxy::integral_constant; - typename proxy::integral_constant; + typename proxy::integral_constant; + typename proxy::integral_constant; }; /** @@ -48,7 +48,7 @@ namespace snmalloc * PALs expose a basic library of memory operations. */ template - concept IsPAL_memops = requires(void* vp, std::size_t sz) { + concept IsPAL_memops = requires(void* vp, size_t sz) { { PAL::notify_not_using(vp, sz) } noexcept -> ConceptSame; @@ -85,7 +85,7 @@ namespace snmalloc * Absent any feature flags, the PAL must support a crude primitive allocator */ template - concept IsPAL_reserve = requires(PAL p, std::size_t sz) { + concept IsPAL_reserve = requires(PAL p, size_t sz) { { PAL::reserve(sz) } noexcept -> ConceptSame; @@ -95,7 +95,7 @@ namespace snmalloc * Some PALs expose a richer allocator which understands aligned allocations */ template - concept IsPAL_reserve_aligned = requires(std::size_t sz) { + concept IsPAL_reserve_aligned = requires(size_t sz) { { PAL::template reserve_aligned(sz) } noexcept -> ConceptSame; diff --git a/src/snmalloc/pal/pal_noalloc.h b/src/snmalloc/pal/pal_noalloc.h index 49b0d4b6a..590aefe4b 100644 --- a/src/snmalloc/pal/pal_noalloc.h +++ b/src/snmalloc/pal/pal_noalloc.h @@ -8,7 +8,7 @@ #include "pal_consts.h" #include "pal_timer_default.h" -#include +#include namespace snmalloc { diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 1214ff327..73f174a68 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -6,17 +6,18 @@ #if defined(SNMALLOC_BACKTRACE_HEADER) # include SNMALLOC_BACKTRACE_HEADER #endif -#include #include #include #include #include +#include #include #include #include #include #include #include + #if __has_include() # include #endif diff --git a/src/test/func/jemalloc/jemalloc.cc b/src/test/func/jemalloc/jemalloc.cc index 5baddd76e..0e34e1fc3 100644 --- a/src/test/func/jemalloc/jemalloc.cc +++ b/src/test/func/jemalloc/jemalloc.cc @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -288,7 +289,7 @@ namespace { START_TEST("allocm out-of-memory behaviour"); void* ptr = nullptr; - int ret = Allocm(&ptr, nullptr, std::numeric_limits::max() / 2, 0); + int ret = Allocm(&ptr, nullptr, SIZE_MAX / 2, 0); EXPECT( (ptr == nullptr) && (ret == OUR_ALLOCM_ERR_OOM), "Expected massive allocation to fail with out of memory ({}), received "