Skip to content

Commit

Permalink
[proxy](7/n) proxy array and algorithm headers
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Dec 21, 2024
1 parent ee52677 commit 9aa79fa
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/snmalloc/backend_helpers/buddy.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../ds/ds.h"
#include "snmalloc/proxy/algorithm.h"

namespace snmalloc
{
Expand All @@ -23,7 +24,7 @@ namespace snmalloc
RBTree<Rep> tree{};
};

std::array<Entry, MAX_SIZE_BITS - MIN_SIZE_BITS> entries{};
proxy::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 +168,7 @@ namespace snmalloc
{
if (Rep::equal(Rep::null, addr) || Rep::compare(e, addr))
{
addr = std::exchange(e, addr);
addr = proxy::exchange(e, addr);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/snmalloc/ds_core/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ namespace snmalloc
}

/**
* Implementation of `std::min`
* Implementation of `proxy::min`
*
* `std::min` is in `<algorithm>`, so pulls in a lot of unneccessary code
* `proxy::min` is in `<algorithm>`, so pulls in a lot of unneccessary code
* We write our own to reduce the code that potentially needs reviewing.
*/
template<typename T>
Expand All @@ -383,9 +383,9 @@ namespace snmalloc
}

/**
* Implementation of `std::max`
* Implementation of `proxy::max`
*
* `std::max` is in `<algorithm>`, so pulls in a lot of unneccessary code
* `proxy::max` is in `<algorithm>`, so pulls in a lot of unneccessary code
* We write our own to reduce the code that potentially needs reviewing.
*/
template<typename T>
Expand Down
6 changes: 5 additions & 1 deletion src/snmalloc/ds_core/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ namespace snmalloc
#define TOSTRING(expr) TOSTRING2(expr)
#define TOSTRING2(expr) #expr

#ifdef __cpp_lib_source_location
#ifndef SNMALLOC_USE_SELF_VENDORED_STL
# define SNMALLOC_USE_SELF_VENDORED_STL 0
#endif

#if defined(__cpp_lib_source_location) && !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
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/proxy/array.h"
#include "snmalloc/proxy/type_traits.h"
#include "snmalloc/proxy/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;
proxy::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 = proxy::Array<T, length>;
#endif

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace snmalloc
template<
typename Fn,
typename =
proxy::enable_if_t<!proxy::is_same_v<std::decay_t<Fn>, function_ref>>>
proxy::enable_if_t<!proxy::is_same_v<proxy::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;
proxy::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}};
proxy::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}};
proxy::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/proxy/array.h"

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

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

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

RBPath(typename Rep::Handle root)
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/global/memcpy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "bounds_checks.h"
#include "snmalloc/proxy/algorithm.h"

namespace snmalloc
{
Expand Down Expand Up @@ -156,7 +157,7 @@ namespace snmalloc
*/
SNMALLOC_UNUSED_FUNCTION
static constexpr size_t LargestRegisterSize =
std::max(sizeof(uint64_t), sizeof(void*));
proxy::max(sizeof(uint64_t), sizeof(void*));

/**
* Hook for architecture-specific optimisations.
Expand Down
3 changes: 2 additions & 1 deletion src/snmalloc/mem/corealloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pool.h"
#include "remotecache.h"
#include "sizeclasstable.h"
#include "snmalloc/proxy/algorithm.h"
#include "snmalloc/proxy/new.h"
#include "ticker.h"

Expand Down Expand Up @@ -222,7 +223,7 @@ namespace snmalloc
pointer_offset(curr, rsize).template as_static<PreAllocObject>())
{
size_t insert_index = entropy.sample(count);
curr->next = std::exchange(
curr->next = proxy::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};
proxy::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};
proxy::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{};
proxy::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/proxy/array.h"
#include "snmalloc/proxy/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};
proxy::Array<freelist::Builder<false, true>, RINGS> open_builder;
proxy::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;
proxy::Array<freelist::Builder<false>, REMOTE_SLOTS> list;

RemoteDeallocCacheBatchingImpl<Config> batching;

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

#ifndef SNMALLOC_USE_SELF_VENDORED_STL
# define SNMALLOC_USE_SELF_VENDORED_STL 0
#endif

// only define these if we are not using the vendored STL
#if !SNMALLOC_USE_SELF_VENDORED_STL
void* operator new(size_t size)
{
return snmalloc::libc::malloc(size);
Expand Down Expand Up @@ -111,3 +117,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
4 changes: 2 additions & 2 deletions src/snmalloc/pal/pal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ namespace snmalloc
// protected routine.
if (false == syscall_not_working.load(proxy::memory_order_relaxed))
{
auto current = std::begin(buffer);
auto target = std::end(buffer);
auto current = proxy::begin(buffer);
auto target = proxy::end(buffer);
while (auto length = target - current)
{
// Reading data via syscall from system entropy pool.
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/pal/pal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,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 = proxy::begin(buffer);
auto target = proxy::end(buffer);
while (auto length = static_cast<size_t>(target - current))
{
ret = read(fd, current, length);
Expand Down
53 changes: 53 additions & 0 deletions src/snmalloc/proxy/algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#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<typename T>
constexpr SNMALLOC_FAST_PATH const T& max(const T& a, const T& b)
{
return a < b ? b : a;
}

template<typename T>
constexpr SNMALLOC_FAST_PATH const T& min(const T& a, const T& b)
{
return a < b ? a : b;
}

template<typename A, typename B = A>
constexpr SNMALLOC_FAST_PATH A exchange(A& obj, B&& new_value)
{
A old_value = move(obj);
obj = forward<B>(new_value);
return old_value;
}

} // namespace proxy
} // namespace snmalloc
#else
# include <algorithm>

namespace snmalloc
{
namespace proxy
{
using std::exchange;
using std::max;
using std::min;
} // namespace proxy
} // namespace snmalloc
#endif
Loading

0 comments on commit 9aa79fa

Please sign in to comment.