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

Added custom allocators to the memory submodule #20

Merged
merged 10 commits into from
May 27, 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
5 changes: 5 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ ignore:
- "**/*-test.cpp" # ignore test harness code
- "**/config/*"
- "**/detail/*"
- "**/foundation/memory/allocator_traits.hpp"
- "**/foundation/memory/debugging.*"
- "**/foundation/memory/memory_pool_type.hpp"
- "**/foundation/memory/temporary_allocator.*"
- "**/foundation/memory/threading.hpp"
- "**/foundation/utility/*"
21 changes: 20 additions & 1 deletion flux-foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@ flux_static_library(foundation
COMMON
TEST
"flux/foundation/memory/detail/constexpr_memcpy-test.cpp"
"flux/foundation/memory/detail/debug_helpers-test.cpp"
"flux/foundation/memory/detail/fixed_stack-test.cpp"
"flux/foundation/memory/detail/free_list_array-test.cpp"
"flux/foundation/memory/detail/free_list-test.cpp"
"flux/foundation/memory/align-test.cpp"
"flux/foundation/memory/allocator_storage-test.cpp"
"flux/foundation/memory/construct-test.cpp"
"flux/foundation/memory/deleter-test.cpp"
"flux/foundation/memory/heap_allocator-test.cpp"
"flux/foundation/memory/memory_arena-test.cpp"
"flux/foundation/memory/memory_block-test.cpp"
"flux/foundation/memory/memory_pool-test.cpp"
"flux/foundation/memory/memory_pool_list-test.cpp"
"flux/foundation/memory/memory_stack-test.cpp"
"flux/foundation/memory/relocate-test.cpp"
"flux/foundation/memory/static_allocator-test.cpp"
"flux/foundation/memory/std_allocator_adapter-test.cpp"
"flux/foundation/memory/temporary_allocator-test.cpp"
"flux/foundation/memory/threading-test.cpp"
"flux/foundation/memory/uninitialized_algorithms-test.cpp"
"flux/foundation/memory/uninitialized_storage-test.cpp"
SOURCE
"flux/foundation/dummy.cpp"
"flux/foundation/memory/detail/debug_helpers.cpp"
"flux/foundation/memory/debugging.cpp"
"flux/foundation/memory/temporary_allocator.cpp"
LINK
flux::io
flux::platform)
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions flux-foundation/flux/foundation/memory.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#pragma once

#include <flux/foundation/memory/align.hpp>
#include <flux/foundation/memory/allocator_storage.hpp>
#include <flux/foundation/memory/allocator_traits.hpp>
#include <flux/foundation/memory/debugging.hpp>
#include <flux/foundation/memory/default_allocator.hpp>
#include <flux/foundation/memory/deleter.hpp>
#include <flux/foundation/memory/memory_arena.hpp>
#include <flux/foundation/memory/memory_block.hpp>
#include <flux/foundation/memory/memory_pool.hpp>
#include <flux/foundation/memory/memory_pool_list.hpp>
#include <flux/foundation/memory/memory_stack.hpp>
#include <flux/foundation/memory/static_allocator.hpp>
#include <flux/foundation/memory/std_allocator_adapter.hpp>
#include <flux/foundation/memory/temporary_allocator.hpp>
#include <flux/foundation/memory/uninitialized_algorithms.hpp>
#include <flux/foundation/memory/uninitialized_storage.hpp>
106 changes: 106 additions & 0 deletions flux-foundation/flux/foundation/memory/align-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <flux/foundation.hpp>

#include <catch2/catch.hpp>

using namespace flux::fou;

TEST_CASE("fou::align_offset", "[flux-memory/align.hpp]") {
auto ptr = reinterpret_cast<void*>(0);
REQUIRE(align_offset(ptr, 1) == 0u);
REQUIRE(align_offset(ptr, 16) == 0u);
ptr = reinterpret_cast<void*>(1);
REQUIRE(align_offset(ptr, 1) == 0u);
REQUIRE(align_offset(ptr, 16) == 15u);
ptr = reinterpret_cast<void*>(8);
REQUIRE(align_offset(ptr, 4) == 0u);
REQUIRE(align_offset(ptr, 8) == 0u);
REQUIRE(align_offset(ptr, 16) == 8u);
ptr = reinterpret_cast<void*>(16);
REQUIRE(align_offset(ptr, 16) == 0u);
ptr = reinterpret_cast<void*>(1025);
REQUIRE(align_offset(ptr, 16) == 15u);
}

TEST_CASE("fou::is_aligned", "[flux-memory/align.hpp]") {
auto ptr = reinterpret_cast<void*>(0);
REQUIRE(is_aligned(ptr, 1));
REQUIRE(is_aligned(ptr, 8));
REQUIRE(is_aligned(ptr, 16));
ptr = reinterpret_cast<void*>(1);
REQUIRE(is_aligned(ptr, 1));
REQUIRE(!is_aligned(ptr, 16));
ptr = reinterpret_cast<void*>(8);
REQUIRE(is_aligned(ptr, 1));
REQUIRE(is_aligned(ptr, 4));
REQUIRE(is_aligned(ptr, 8));
REQUIRE(!is_aligned(ptr, 16));
ptr = reinterpret_cast<void*>(16);
REQUIRE(is_aligned(ptr, 1));
REQUIRE(is_aligned(ptr, 8));
REQUIRE(is_aligned(ptr, 16));
ptr = reinterpret_cast<void*>(1025);
REQUIRE(is_aligned(ptr, 1));
REQUIRE(!is_aligned(ptr, 16));
}

TEST_CASE("fou::alignment_for", "[flux-memory/align.hpp]") {
static_assert(detail::max_alignment >= 8, "test case not working");
REQUIRE(alignment_for(1) == 1);
REQUIRE(alignment_for(2) == 2);
REQUIRE(alignment_for(3) == 2);
REQUIRE(alignment_for(4) == 4);
REQUIRE(alignment_for(5) == 4);
REQUIRE(alignment_for(6) == 4);
REQUIRE(alignment_for(7) == 4);
REQUIRE(alignment_for(8) == 8);
REQUIRE(alignment_for(9) == 8);
REQUIRE(alignment_for(100) == detail::max_alignment);
}

TEST_CASE("fou::ilog2", "[flux-memory/align.hpp]") {
SECTION("Check everything up to 2^16") {
for (::std::size_t i = 0; i != 16; ++i) {
auto power = 1u << i;
auto next_power = 2 * power;
for (auto x = power; x != next_power; ++x)
CHECK(ilog2(x) == i);
}
}

CHECK(ilog2(::std::size_t(1) << 32) == 32);
CHECK(ilog2((::std::size_t(1) << 32) + 44) == 32);
CHECK(ilog2((::std::size_t(1) << 32) + 2048) == 32);

CHECK(ilog2(::std::size_t(1) << 48) == 48);
CHECK(ilog2((::std::size_t(1) << 48) + 44) == 48);
CHECK(ilog2((::std::size_t(1) << 48) + 2048) == 48);

CHECK(ilog2(::std::size_t(1) << 63) == 63);
CHECK(ilog2((::std::size_t(1) << 63) + 44) == 63);
CHECK(ilog2((::std::size_t(1) << 63) + 2063) == 63);
}

TEST_CASE("fou::ilog2_ceil", "[flux-memory/align.hpp]") {
SECTION("Check everything up to 2^16") {
for (::std::size_t i = 0; i != 16; ++i) {
auto power = 1u << i;
CHECK(ilog2_ceil(power) == i);

auto next_power = 2 * power;
for (auto x = power + 1; x != next_power; ++x)
CHECK(ilog2_ceil(x) == i + 1);
}
}

CHECK(ilog2_ceil(::std::size_t(1) << 32) == 32);
CHECK(ilog2_ceil((::std::size_t(1) << 32) + 44) == 33);
CHECK(ilog2_ceil((::std::size_t(1) << 32) + 2048) == 33);

CHECK(ilog2_ceil(::std::size_t(1) << 48) == 48);
CHECK(ilog2_ceil((::std::size_t(1) << 48) + 44) == 49);
CHECK(ilog2_ceil((::std::size_t(1) << 48) + 2048) == 49);

CHECK(ilog2_ceil(::std::size_t(1) << 63) == 63);
CHECK(ilog2_ceil((::std::size_t(1) << 63) + 44) == 64);
CHECK(ilog2_ceil((::std::size_t(1) << 63) + 2063) == 64);
}
72 changes: 72 additions & 0 deletions flux-foundation/flux/foundation/memory/align.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once
#include <flux/meta.hpp>

#include <climits>
#include <cstddef>
#include <cstdint>

namespace flux::fou {

namespace detail {
// __STDCPP_DEFAULT_NEW_ALIGNMENT__
inline constexpr auto max_alignment = alignof(::std::max_align_t);
} // namespace detail

// clang-format off
template <meta::integral I>
constexpr bool is_pow2(I value) noexcept {
return value && !((value) & (value - 1));
}
// clang-format on

namespace detail {

constexpr auto ilog2_base(unsigned long long value) noexcept {
#if __has_builtin(__builtin_clzll)
return sizeof(value) * CHAR_BIT - static_cast<unsigned>(__builtin_clzll(value));
#else
auto clz = 64ull;
auto c = 32ull;
do {
auto tmp = value >> c;
if (tmp != 0ull) {
clz -= c;
value = tmp;
}
c = c >> 1ull;
} while (c != 0ull);
clz -= value ? 1ull : 0ull;

return 64ull - clz;
#endif
}

} // namespace detail

constexpr ::std::size_t ilog2(::std::size_t value) noexcept {
return detail::ilog2_base(value) - 1ull;
}

constexpr ::std::size_t ilog2_ceil(::std::size_t value) noexcept {
return detail::ilog2_base(value) - static_cast<::std::size_t>(is_pow2(value));
}

constexpr ::std::size_t alignment_for(::std::size_t size) noexcept {
return size >= detail::max_alignment ? detail::max_alignment
: (::std::size_t{1} << ilog2(size));
}

constexpr ::std::size_t align_offset(::std::uintptr_t address, ::std::size_t alignment) noexcept {
auto const offset = address & (alignment - 1);
return 0ull != offset ? (alignment - offset) : 0ull;
}

inline ::std::size_t align_offset(void* ptr, ::std::size_t alignment) noexcept {
return align_offset(reinterpret_cast<::std::uintptr_t>(ptr), alignment);
}

inline bool is_aligned(void* ptr, ::std::size_t alignment) noexcept {
return reinterpret_cast<::std::uintptr_t>(ptr) % alignment == 0ull;
}

} // namespace flux::fou
Loading