Skip to content

Commit

Permalink
[proxy](5/n) use c headers instead of c++ headers
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Dec 21, 2024
1 parent dbe7e47 commit 73558e3
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/snmalloc/aal/aal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# define SNMALLOC_TICK_USE_CLOCK_GETTIME
# endif
#endif
#include <cstdint>
#include <stdint.h>
#include <utility>

#ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/aal/aal_arm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# endif
#endif

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

namespace snmalloc
{
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/aal/aal_consts.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <cstdint>
#include <stdint.h>

namespace snmalloc
{
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/aal/address.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "../ds_core/ds_core.h"

#include <cstdint>
#include <stdint.h>

namespace snmalloc
{
Expand Down
6 changes: 3 additions & 3 deletions src/snmalloc/ds/aba.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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> linked;
alignas(2 * sizeof(size_t)) proxy::Atomic<Linked> linked;
Independent independent;
};

Expand Down
20 changes: 10 additions & 10 deletions src/snmalloc/ds_core/bits.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once

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

// #define USE_LZCNT

#include "defines.h"
#include "snmalloc/proxy/atomic.h"
#include "snmalloc/proxy/type_traits.h"

#include <climits>
#include <cstdint>
#include <limits.h>
#include <stdint.h>
#if defined(_MSC_VER)
# include <intrin.h>
# include <intsafe.h>
#endif

Expand Down Expand Up @@ -95,15 +95,15 @@ namespace snmalloc
return BITS - index - 1;
# endif
#else
if constexpr (proxy::is_same_v<unsigned long, std::size_t>)
if constexpr (proxy::is_same_v<unsigned long, size_t>)
{
return static_cast<size_t>(__builtin_clzl(x));
}
else if constexpr (proxy::is_same_v<unsigned long long, std::size_t>)
else if constexpr (proxy::is_same_v<unsigned long long, size_t>)
{
return static_cast<size_t>(__builtin_clzll(x));
}
else if constexpr (proxy::is_same_v<unsigned int, std::size_t>)
else if constexpr (proxy::is_same_v<unsigned int, size_t>)
{
return static_cast<size_t>(__builtin_clz(x));
}
Expand Down Expand Up @@ -182,15 +182,15 @@ namespace snmalloc
return _tzcnt_u32(static_cast<unsigned int>(x));
# endif
#else
if constexpr (proxy::is_same_v<unsigned long, std::size_t>)
if constexpr (proxy::is_same_v<unsigned long, size_t>)
{
return static_cast<size_t>(__builtin_ctzl(x));
}
else if constexpr (proxy::is_same_v<unsigned long long, std::size_t>)
else if constexpr (proxy::is_same_v<unsigned long long, size_t>)
{
return static_cast<size_t>(__builtin_ctzll(x));
}
else if constexpr (proxy::is_same_v<unsigned int, std::size_t>)
else if constexpr (proxy::is_same_v<unsigned int, size_t>)
{
return static_cast<size_t>(__builtin_ctz(x));
}
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/ds_core/defines.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <cstddef>
#include <stddef.h>

#if defined(_MSC_VER) && !defined(__clang__)
// 28 is FAST_FAIL_INVALID_BUFFER_ACCESS. Not using the symbolic constant to
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/ds_core/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "snmalloc/proxy/type_traits.h"

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

namespace snmalloc
{
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/ds_core/mitigations.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "defines.h"

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

namespace snmalloc
{
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/ds_core/redblacktree.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <array>
#include <cstddef>
#include <cstdint>
#include <stddef.h>
#include <stdint.h>

namespace snmalloc
{
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/ds_core/seqset.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../ds_core/ds_core.h"
#include "snmalloc/proxy/type_traits.h"

#include <cstdint>
#include <stdint.h>

namespace snmalloc
{
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/mem/entropy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../pal/pal.h"
#include "snmalloc/proxy/type_traits.h"

#include <cstdint>
#include <stdint.h>

namespace snmalloc
{
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 @@ -37,7 +37,7 @@
#include "entropy.h"
#include "snmalloc/proxy/new.h"

#include <cstdint>
#include <stdint.h>

namespace snmalloc
{
Expand Down
15 changes: 13 additions & 2 deletions src/snmalloc/mem/localalloc.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -888,8 +889,18 @@ namespace snmalloc
auto sizeclass = entry.get_sizeclass();
return snmalloc::remaining_bytes(sizeclass, p);
#else
return reinterpret_cast<size_t>(
std::numeric_limits<decltype(p)>::max() - p);
constexpr address_t mask = static_cast<address_t>(-1);
constexpr bool is_signed = mask < 0;
constexpr address_t sign_bit = static_cast<address_t>(1)
<< (CHAR_BIT * sizeof(address_t) - 1);
if constexpr (is_signed)
{
return (mask ^ sign_bit) - p;
}
else
{
return mask - p;
}
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/mem/ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../ds_core/ds_core.h"

#include <cstdint>
#include <stdint.h>

namespace snmalloc
{
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/override/jemalloc_compat.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "override.h"

#include <errno.h>
#include <limits.h>
#include <string.h>

using namespace snmalloc;
Expand Down Expand Up @@ -182,7 +183,7 @@ extern "C"
return allocm_err_not_moved;
}

if (std::numeric_limits<size_t>::max() - size > extra)
if (SIZE_MAX - size > extra)
{
alloc_size = f.aligned_size(size + extra);
}
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/override/rust.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define SNMALLOC_NAME_MANGLE(a) sn_##a
#include "snmalloc/snmalloc.h"

#include <cstring>
#include <string.h>

#ifndef SNMALLOC_EXPORT
# define SNMALLOC_EXPORT
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/snmalloc/pal/pal_concept.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace snmalloc
template<typename PAL>
concept IsPAL_static_sizes =
requires() {
typename proxy::integral_constant<std::size_t, PAL::address_bits>;
typename proxy::integral_constant<std::size_t, PAL::page_size>;
typename proxy::integral_constant<size_t, PAL::address_bits>;
typename proxy::integral_constant<size_t, PAL::page_size>;
};

/**
Expand All @@ -48,7 +48,7 @@ namespace snmalloc
* PALs expose a basic library of memory operations.
*/
template<typename PAL>
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<void>;
Expand Down Expand Up @@ -85,7 +85,7 @@ namespace snmalloc
* Absent any feature flags, the PAL must support a crude primitive allocator
*/
template<typename PAL>
concept IsPAL_reserve = requires(PAL p, std::size_t sz) {
concept IsPAL_reserve = requires(PAL p, size_t sz) {
{
PAL::reserve(sz)
} noexcept -> ConceptSame<void*>;
Expand All @@ -95,7 +95,7 @@ namespace snmalloc
* Some PALs expose a richer allocator which understands aligned allocations
*/
template<typename PAL>
concept IsPAL_reserve_aligned = requires(std::size_t sz) {
concept IsPAL_reserve_aligned = requires(size_t sz) {
{
PAL::template reserve_aligned<true>(sz)
} noexcept -> ConceptSame<void*>;
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/pal/pal_noalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "pal_consts.h"
#include "pal_timer_default.h"

#include <cstring>
#include <string.h>

namespace snmalloc
{
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 @@ -6,17 +6,18 @@
#if defined(SNMALLOC_BACKTRACE_HEADER)
# include SNMALLOC_BACKTRACE_HEADER
#endif
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
#include <utility>

#if __has_include(<sys/random.h>)
# include <sys/random.h>
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/test/func/jemalloc/jemalloc.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <functional>
#include <limits.h>
#include <stdio.h>
#include <test/helpers.h>
#include <test/setup.h>
Expand Down Expand Up @@ -288,7 +289,7 @@ namespace
{
START_TEST("allocm out-of-memory behaviour");
void* ptr = nullptr;
int ret = Allocm(&ptr, nullptr, std::numeric_limits<size_t>::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 "
Expand Down

0 comments on commit 73558e3

Please sign in to comment.