Skip to content

Commit

Permalink
[proxy](7/n) proxy array headers
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Dec 21, 2024
1 parent ee52677 commit 97b2d9a
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 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{};
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
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
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
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
118 changes: 118 additions & 0 deletions src/snmalloc/proxy/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#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 <stddef.h>

namespace snmalloc
{
namespace proxy
{
template<typename T, size_t N>
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];

[[nodiscard]] constexpr SNMALLOC_FAST_PATH size_t size() const
{
return N;
}

constexpr T& operator[](size_t i)
{
return storage[i];
}

constexpr const T& operator[](size_t i) const
{
return storage[i];
}

using value_type = T;
using size_type = size_t;
using iterator = T*;
using const_iterator = const T*;

[[nodiscard]] constexpr SNMALLOC_FAST_PATH iterator begin()
{
return &storage[0];
}

[[nodiscard]] constexpr SNMALLOC_FAST_PATH const_iterator begin() const
{
return &storage[0];
}

[[nodiscard]] constexpr SNMALLOC_FAST_PATH iterator end()
{
return &storage[N];
}

[[nodiscard]] constexpr SNMALLOC_FAST_PATH const_iterator end() const
{
return &storage[N];
}

[[nodiscard]] constexpr SNMALLOC_FAST_PATH T* data()
{
return &storage[0];
}

[[nodiscard]] constexpr SNMALLOC_FAST_PATH const T* data() const
{
return &storage[0];
}
};

template<typename T, size_t N>
constexpr T* begin(Array<T, N>& a)
{
return a.begin();
}

template<typename T, size_t N>
constexpr T* end(Array<T, N>& a)
{
return a.end();
}

template<typename T, size_t N>
constexpr const T* begin(const Array<T, N>& a)
{
return a.begin();
}

template<typename T, size_t N>
constexpr const T* end(const Array<T, N>& a)
{
return a.end();
}
} // namespace proxy
} // namespace snmalloc
#else
# include <array>

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

using std::begin;
using std::end;
} // namespace proxy
} // namespace snmalloc
#endif
2 changes: 2 additions & 0 deletions src/test/func/memory/memory.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <array>
#include <iostream>
#include <snmalloc/snmalloc.h>
#include <test/opt.h>
#include <test/setup.h>
#include <test/xoroshiro.h>
#include <unordered_set>
#include <vector>

#if ((defined(__linux__) && !defined(__ANDROID__)) || defined(__sun)) || \
defined(__OpenBSD__) && !defined(SNMALLOC_QEMU_WORKAROUND)
/*
Expand Down

0 comments on commit 97b2d9a

Please sign in to comment.