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

Add allocator utilities #6

Merged
merged 5 commits into from
Aug 20, 2024
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
23 changes: 23 additions & 0 deletions include/yk/allocator/concepts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef YK_ALLOCATOR_CONCEPTS_HPP
#define YK_ALLOCATOR_CONCEPTS_HPP

#include <concepts>
#include <cstddef>

namespace yk {

namespace xo { // exposition-only

// C++26
// https://en.cppreference.com/w/cpp/named_req/Allocator
template <class Alloc>
concept simple_allocator = requires(Alloc alloc, std::size_t n) {
{ *alloc.allocate(n) } -> std::same_as<typename Alloc::value_type&>;
{ alloc.deallocate(alloc.allocate(n), n) };
} && std::copy_constructible<Alloc> && std::equality_comparable<Alloc>;

} // namespace xo

} // namespace yk

#endif
41 changes: 41 additions & 0 deletions include/yk/allocator/default_init_allocator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef YK_ALLOCATOR_DEFAULT_INIT_ALLOCATOR_HPP
#define YK_ALLOCATOR_DEFAULT_INIT_ALLOCATOR_HPP

#include "yk/allocator/concepts.hpp"

#include <memory>
#include <new>
#include <type_traits>
#include <utility>

namespace yk {

// http://stackoverflow.com/a/21028912/273767

template <class T, class A = std::allocator<T>>
class default_init_allocator : public A {
static_assert(xo::simple_allocator<A>);

using traits = std::allocator_traits<A>;

public:
template <class U>
struct rebind {
using other = default_init_allocator<U, typename traits::template rebind_alloc<U>>;
};

using A::A;

template <class U>
constexpr void construct(U* ptr) noexcept(std::is_nothrow_default_constructible_v<U>) {
::new (static_cast<void*>(ptr)) U;
}
template <class U, class... Args>
constexpr void construct(U* ptr, Args&&... args) noexcept(noexcept(traits::construct(static_cast<A&>(*this), ptr, std::forward<Args>(args)...))) {
traits::construct(static_cast<A&>(*this), ptr, std::forward<Args>(args)...);
}
};

} // namespace yk

#endif
28 changes: 28 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "yk/allocator/default_init_allocator.hpp"
#include "yk/util/forward_like.hpp"
#include "yk/util/pack_indexing.hpp"
#include "yk/util/reverse.hpp"
Expand All @@ -11,7 +12,10 @@

#include <boost/range/iterator_range.hpp>

#include <algorithm>
#include <cstdint>
#include <functional>
#include <memory>
#include <set>
#include <string>
#include <utility>
Expand Down Expand Up @@ -127,4 +131,28 @@ BOOST_AUTO_TEST_CASE(ToSubrange) {
BOOST_TEST((std::ranges::equal(v, yk::to_subrange(rng))));
}

BOOST_AUTO_TEST_CASE(Allocator) {
{
unsigned char uninitialized_storage[sizeof(std::uint32_t)] = {0xde, 0xad, 0xbe, 0xef};
alignas(std::uint32_t) unsigned char storage[sizeof(std::uint32_t)];
std::ranges::copy(uninitialized_storage, storage);

std::allocator<std::uint32_t> alloc;
std::allocator_traits<std::allocator<std::uint32_t>>::construct(alloc, reinterpret_cast<std::uint32_t*>(storage));

BOOST_TEST(!std::ranges::equal(uninitialized_storage, storage));
BOOST_TEST(std::ranges::equal(std::vector<unsigned char>{0, 0, 0, 0}, storage));
}
{
unsigned char uninitialized_storage[sizeof(std::uint32_t)] = {0xde, 0xad, 0xbe, 0xef};
alignas(std::uint32_t) unsigned char storage[sizeof(std::uint32_t)];
std::ranges::copy(uninitialized_storage, storage);

yk::default_init_allocator<std::uint32_t> alloc;
alloc.construct(storage);

BOOST_TEST(std::ranges::equal(uninitialized_storage, storage));
}
}

BOOST_AUTO_TEST_SUITE_END() // yk_util