Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proxy](7/n) self vendor array and algorithm headers #722

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ if(SNMALLOC_REMOTE_BATCH_PROCESS_SIZE)
endif()

if(SNMALLOC_USE_SELF_VENDORED_STL)
target_compile_definitions(snmalloc INTERFACE SNMALLOC_USE_SELF_VENDORED_STL=1)
else()
target_compile_definitions(snmalloc INTERFACE SNMALLOC_USE_SELF_VENDORED_STL=0)
target_compile_definitions(snmalloc INTERFACE SNMALLOC_USE_SELF_VENDORED_STL)
endif()

# https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/backend_helpers/buddy.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace snmalloc
RBTree<Rep> tree{};
};

std::array<Entry, MAX_SIZE_BITS - MIN_SIZE_BITS> entries{};
stl::Array<Entry, MAX_SIZE_BITS - MIN_SIZE_BITS> entries{};
// All RBtrees at or above this index should be empty.
size_t empty_at_or_above{0};

Expand Down Expand Up @@ -167,7 +167,7 @@ namespace snmalloc
{
if (Rep::equal(Rep::null, addr) || Rep::compare(e, addr))
{
addr = std::exchange(e, addr);
addr = stl::exchange(e, addr);
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/snmalloc/ds_core/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ namespace snmalloc
#define TOSTRING(expr) TOSTRING2(expr)
#define TOSTRING2(expr) #expr

#ifdef __cpp_lib_source_location
#if defined(__cpp_lib_source_location) && \
!defined(SNMALLOC_USE_SELF_VENDORED_STL)
# include <source_location>
# define SNMALLOC_CURRENT_LINE std::source_location::current().line()
# define SNMALLOC_CURRENT_FILE std::source_location::current().file_name()
Expand Down Expand Up @@ -203,6 +204,13 @@ namespace snmalloc
# define SNMALLOC_UNINITIALISED
#endif

// Check that the vendored STL is only used with GNU/Clang extensions.
#ifdef SNMALLOC_USE_SELF_VENDORED_STL
# if !defined(__GNUC__) && !defined(__clang__)
# error "cannot use vendored STL without GNU/Clang extensions"
# endif
#endif

namespace snmalloc
{
/**
Expand Down
16 changes: 8 additions & 8 deletions src/snmalloc/ds_core/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include "bits.h"
#include "snmalloc/ds_core/defines.h"
#include "snmalloc/stl/array.h"
#include "snmalloc/stl/type_traits.h"
#include "snmalloc/stl/utility.h"

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

namespace snmalloc
Expand Down Expand Up @@ -50,7 +50,7 @@ namespace snmalloc
};

static constexpr size_t rlength = bits::next_pow2_const(length);
std::array<TWrap, rlength> array;
stl::Array<TWrap, rlength> array;

public:
constexpr const T& operator[](const size_t i) const
Expand All @@ -65,7 +65,7 @@ namespace snmalloc
};
#else
template<size_t length, typename T>
using ModArray = std::array<T, length>;
using ModArray = stl::Array<T, length>;
#endif

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace snmalloc
template<
typename Fn,
typename =
stl::enable_if_t<!stl::is_same_v<std::decay_t<Fn>, function_ref>>>
stl::enable_if_t<!stl::is_same_v<stl::decay_t<Fn>, function_ref>>>
function_ref(Fn&& fn)
{
data_ = static_cast<void*>(&fn);
Expand Down Expand Up @@ -144,7 +144,7 @@ namespace snmalloc
/**
* The buffer that is used to store the formatted output.
*/
std::array<char, BufferSize> buffer;
stl::Array<char, BufferSize> buffer;

/**
* Space in the buffer, excluding a trailing null terminator.
Expand Down Expand Up @@ -208,7 +208,7 @@ namespace snmalloc
/**
* Append a nullptr
*/
void append(std::nullptr_t)
void append(decltype(nullptr))
{
append("(nullptr)");
}
Expand Down Expand Up @@ -254,7 +254,7 @@ namespace snmalloc
append_char('-');
s = 0 - s;
}
std::array<char, 20> buf{{0}};
stl::Array<char, 20> buf{{0}};
const char digits[] = "0123456789";
for (long i = static_cast<long>(buf.size() - 1); i >= 0; i--)
{
Expand Down Expand Up @@ -284,7 +284,7 @@ namespace snmalloc
{
append_char('0');
append_char('x');
std::array<char, 16> buf{{0}};
stl::Array<char, 16> buf{{0}};
const char hexdigits[] = "0123456789abcdef";
// Length of string including null terminator
static_assert(sizeof(hexdigits) == 0x11);
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/ds_core/ptrwrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ namespace snmalloc
/**
* nullptr is implicitly constructable at any bounds type
*/
constexpr SNMALLOC_FAST_PATH CapPtr(const std::nullptr_t n)
constexpr SNMALLOC_FAST_PATH CapPtr(const decltype(nullptr) n)
: unsafe_capptr(n)
{}

Expand Down Expand Up @@ -465,7 +465,7 @@ namespace snmalloc
/**
* nullptr is constructable at any bounds type
*/
constexpr SNMALLOC_FAST_PATH AtomicCapPtr(const std::nullptr_t n)
constexpr SNMALLOC_FAST_PATH AtomicCapPtr(const decltype(nullptr) n)
: unsafe_capptr(n)
{}

Expand Down
5 changes: 3 additions & 2 deletions src/snmalloc/ds_core/redblacktree.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

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

#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -259,7 +260,7 @@ namespace snmalloc
{
friend class RBTree;

std::array<RBStep, 128> path;
stl::Array<RBStep, 128> path;
size_t length = 0;

RBPath(typename Rep::Handle root)
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/global/memcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace snmalloc
*/
SNMALLOC_UNUSED_FUNCTION
static constexpr size_t LargestRegisterSize =
std::max(sizeof(uint64_t), sizeof(void*));
bits::max(sizeof(uint64_t), sizeof(void*));

/**
* Hook for architecture-specific optimisations.
Expand Down
2 changes: 1 addition & 1 deletion src/snmalloc/mem/corealloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace snmalloc
pointer_offset(curr, rsize).template as_static<PreAllocObject>())
{
size_t insert_index = entropy.sample(count);
curr->next = std::exchange(
curr->next = stl::exchange(
pointer_offset(bumpptr, insert_index * rsize)
.template as_static<PreAllocObject>()
->next,
Expand Down
6 changes: 3 additions & 3 deletions src/snmalloc/mem/freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,11 @@ namespace snmalloc
*/

// Pointer to the first element.
std::array<void*, LENGTH> head{nullptr};
stl::Array<void*, LENGTH> head{nullptr};
// Pointer to the reference to the last element.
// In the empty case end[i] == &head[i]
// This enables branch free enqueuing.
std::array<void**, LENGTH> end{nullptr};
stl::Array<void**, LENGTH> end{nullptr};

[[nodiscard]] Object::BQueuePtr<BQueue>* cast_end(uint32_t ix) const
{
Expand All @@ -724,7 +724,7 @@ namespace snmalloc
}

SNMALLOC_NO_UNIQUE_ADDRESS
std::array<uint16_t, RANDOM ? 2 : (TRACK_LENGTH ? 1 : 0)> length{};
stl::Array<uint16_t, RANDOM ? 2 : (TRACK_LENGTH ? 1 : 0)> length{};

public:
constexpr Builder() = default;
Expand Down
9 changes: 4 additions & 5 deletions src/snmalloc/mem/remotecache.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#include "metadata.h"
#include "remoteallocator.h"
#include "sizeclasstable.h"
#include "snmalloc/stl/array.h"
#include "snmalloc/stl/atomic.h"

#include <array>

namespace snmalloc
{

Expand All @@ -33,8 +32,8 @@ namespace snmalloc
{
static_assert(RINGS > 0);

std::array<freelist::Builder<false, true>, RINGS> open_builder;
std::array<address_t, RINGS> open_meta = {0};
stl::Array<freelist::Builder<false, true>, RINGS> open_builder;
stl::Array<address_t, RINGS> open_meta = {0};

SNMALLOC_FAST_PATH size_t
ring_set(typename Config::PagemapEntry::SlabMetadata* meta)
Expand Down Expand Up @@ -191,7 +190,7 @@ namespace snmalloc
template<typename Config>
struct RemoteDeallocCache
{
std::array<freelist::Builder<false>, REMOTE_SLOTS> list;
stl::Array<freelist::Builder<false>, REMOTE_SLOTS> list;

RemoteDeallocCacheBatchingImpl<Config> batching;

Expand Down
3 changes: 3 additions & 0 deletions src/snmalloc/override/new.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# endif
#endif

// only define these if we are not using the vendored STL
#ifndef SNMALLOC_USE_SELF_VENDORED_STL
void* operator new(size_t size)
{
return snmalloc::libc::malloc(size);
Expand Down Expand Up @@ -111,3 +113,4 @@ void operator delete[](void* p, size_t size, std::align_val_t val) EXCEPTSPEC
size = snmalloc::aligned_size(size_t(val), size);
snmalloc::libc::free_sized(p, size);
}
#endif
2 changes: 0 additions & 2 deletions src/snmalloc/pal/pal_concept.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# include "pal_consts.h"
# include "pal_ds.h"

# include <utility>

namespace snmalloc
{
/*
Expand Down
5 changes: 3 additions & 2 deletions src/snmalloc/pal/pal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if defined(__linux__)
# include "../ds_core/ds_core.h"
# include "pal_posix.h"
# include "snmalloc/stl/array.h"

# include <string.h>
# include <sys/mman.h>
Expand Down Expand Up @@ -183,8 +184,8 @@ namespace snmalloc
// protected routine.
if (false == syscall_not_working.load(stl::memory_order_relaxed))
{
auto current = std::begin(buffer);
auto target = std::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.
Expand Down
5 changes: 3 additions & 2 deletions src/snmalloc/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if defined(SNMALLOC_BACKTRACE_HEADER)
# include SNMALLOC_BACKTRACE_HEADER
#endif
#include "snmalloc/stl/array.h"
#include "snmalloc/stl/utility.h"

#include <errno.h>
Expand Down Expand Up @@ -417,8 +418,8 @@ namespace snmalloc
auto fd = open("/dev/urandom", flags, 0);
if (fd > 0)
{
auto current = std::begin(buffer);
auto target = std::end(buffer);
auto current = stl::begin(buffer);
auto target = stl::end(buffer);
while (auto length = static_cast<size_t>(target - current))
{
ret = read(fd, current, length);
Expand Down
7 changes: 7 additions & 0 deletions src/snmalloc/stl/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#ifdef SNMALLOC_USE_SELF_VENDORED_STL
# include "snmalloc/stl/gnu/array.h"
#else
# include "snmalloc/stl/cxx/array.h"
#endif
4 changes: 1 addition & 3 deletions src/snmalloc/stl/atomic.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include "snmalloc/stl/common.h"

#if SNMALLOC_USE_SELF_VENDORED_STL
#ifdef SNMALLOC_USE_SELF_VENDORED_STL
# include "snmalloc/stl/gnu/atomic.h"
#else
# include "snmalloc/stl/cxx/atomic.h"
Expand Down
15 changes: 0 additions & 15 deletions src/snmalloc/stl/common.h

This file was deleted.

15 changes: 15 additions & 0 deletions src/snmalloc/stl/cxx/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <array>

namespace snmalloc
{
namespace stl
{
template<typename T, size_t N>
using Array = std::array<T, N>;

using std::begin;
using std::end;
} // namespace stl
} // namespace snmalloc
1 change: 1 addition & 0 deletions src/snmalloc/stl/cxx/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace snmalloc
namespace stl
{
using std::declval;
using std::exchange;
using std::forward;
using std::move;
template<class T1, class T2>
Expand Down
Loading
Loading