From a392b88787e7da073c0a8c00bd22520e00e67b85 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Mon, 14 Nov 2022 21:32:00 +0800 Subject: [PATCH] upd: [pmr] helper trait for allocator --- include/libimp/span.h | 2 +- include/libpmr/allocator.h | 29 +++++++++++++++++++++++++++ include/libpmr/memory_resource.h | 6 +++--- src/libpmr/memory_resource.cpp | 5 ++--- test/pmr/test_pmr_allocator.cpp | 12 +++++++++++ test/pmr/test_pmr_memory_resource.cpp | 4 ++-- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/include/libimp/span.h b/include/libimp/span.h index 814c06e..2511d29 100644 --- a/include/libimp/span.h +++ b/include/libimp/span.h @@ -31,7 +31,7 @@ LIBIMP_NAMESPACE_BEG_ namespace detail { -/// @brief helper trait for span +/// @brief Helper trait for span. template using array_convertible = std::is_convertible; diff --git a/include/libpmr/allocator.h b/include/libpmr/allocator.h index f323090..f28fc45 100644 --- a/include/libpmr/allocator.h +++ b/include/libpmr/allocator.h @@ -6,10 +6,37 @@ */ #pragma once +#include + +#include "libimp/export.h" + #include "libpmr/def.h" #include "libpmr/memory_resource.h" LIBPMR_NAMESPACE_BEG_ +namespace detail { + +/// @brief Helper trait for allocator. + +template +struct has_allocate : std::false_type {}; + +template +struct has_allocate().allocate(std::declval())), void * + >::value>::type> : std::true_type {}; + +template +struct has_deallocate : std::false_type {}; + +template +struct has_deallocate().deallocate(std::declval(), + std::declval())) + > : std::true_type {}; + +} // namespace detail /** * @brief An allocator which exhibits different allocation behavior @@ -23,6 +50,8 @@ LIBPMR_NAMESPACE_BEG_ * @see https://en.cppreference.com/w/cpp/memory/memory_resource * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator */ +class LIBIMP_EXPORT allocator { +}; LIBPMR_NAMESPACE_END_ diff --git a/include/libpmr/memory_resource.h b/include/libpmr/memory_resource.h index 328a640..fb05132 100644 --- a/include/libpmr/memory_resource.h +++ b/include/libpmr/memory_resource.h @@ -6,7 +6,7 @@ */ #pragma once -#include // std::size_t +#include // std::size_t, std::max_align_t #include "libimp/export.h" #include "libpmr/def.h" @@ -24,11 +24,11 @@ public: /// @brief Allocates storage with a size of at least bytes bytes, aligned to the specified alignment. /// @remark Returns nullptr if storage of the requested size and alignment cannot be obtained. /// @see https://en.cppreference.com/w/cpp/memory/memory_resource/do_allocate - void *do_allocate(std::size_t bytes, std::size_t alignment) noexcept; + void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; /// @brief Deallocates the storage pointed to by p. /// @see https://en.cppreference.com/w/cpp/memory/memory_resource/deallocate - void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) noexcept; + void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; }; LIBPMR_NAMESPACE_END_ diff --git a/src/libpmr/memory_resource.cpp b/src/libpmr/memory_resource.cpp index ddc7c70..ef9fe95 100644 --- a/src/libpmr/memory_resource.cpp +++ b/src/libpmr/memory_resource.cpp @@ -1,6 +1,5 @@ #include // std::aligned_alloc -#include // std::max_align_t #include #include "libimp/detect_plat.h" @@ -37,7 +36,7 @@ bool verify_args(::LIBIMP_::log::gripper &log, std::size_t bytes, std::size_t al * * @return void * - nullptr if storage of the requested size and alignment cannot be obtained. */ -void *new_delete_resource::do_allocate(std::size_t bytes, std::size_t alignment) noexcept { +void *new_delete_resource::allocate(std::size_t bytes, std::size_t alignment) noexcept { LIBIMP_LOG_(); if (!verify_args(log, bytes, alignment)) { return nullptr; @@ -83,7 +82,7 @@ void *new_delete_resource::do_allocate(std::size_t bytes, std::size_t alignment) * * @param p must have been returned by a prior call to new_delete_resource::do_allocate(bytes, alignment). */ -void new_delete_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) noexcept { +void new_delete_resource::deallocate(void *p, std::size_t bytes, std::size_t alignment) noexcept { LIBIMP_LOG_(); if (p == nullptr) { return; diff --git a/test/pmr/test_pmr_allocator.cpp b/test/pmr/test_pmr_allocator.cpp index 2282cc8..d90abf9 100644 --- a/test/pmr/test_pmr_allocator.cpp +++ b/test/pmr/test_pmr_allocator.cpp @@ -1,7 +1,19 @@ +#include +#include + #include "gtest/gtest.h" #include "libpmr/allocator.h" +TEST(allocator, detail) { + EXPECT_FALSE(pmr::detail::has_allocate::value); + EXPECT_FALSE(pmr::detail::has_allocate::value); + EXPECT_FALSE(pmr::detail::has_allocate>::value); + EXPECT_TRUE (pmr::detail::has_allocate>::value); + EXPECT_TRUE (pmr::detail::has_allocate::value); + EXPECT_TRUE (pmr::detail::has_allocate>::value); +} + TEST(allocator, construct) { } \ No newline at end of file diff --git a/test/pmr/test_pmr_memory_resource.cpp b/test/pmr/test_pmr_memory_resource.cpp index 92a6a21..5c662aa 100644 --- a/test/pmr/test_pmr_memory_resource.cpp +++ b/test/pmr/test_pmr_memory_resource.cpp @@ -9,13 +9,13 @@ namespace { template void *test_mr(T &&mr, std::size_t bytes, std::size_t alignment) { - auto p = std::forward(mr).do_allocate(bytes, alignment); + auto p = std::forward(mr).allocate(bytes, alignment); if (alignment == 0) { EXPECT_EQ(p, nullptr); } else if (p != nullptr) { EXPECT_EQ((std::size_t)p % alignment, 0); } - std::forward(mr).do_deallocate(p, bytes, alignment); + std::forward(mr).deallocate(p, bytes, alignment); return p; }