diff --git a/src/libpmr/allocator.cpp b/src/libpmr/allocator.cpp index 68eb9fc4..4f91848e 100644 --- a/src/libpmr/allocator.cpp +++ b/src/libpmr/allocator.cpp @@ -1,6 +1,7 @@ #include // std::swap +#include "libimp/log.h" #include "libpmr/allocator.h" LIBPMR_NAMESPACE_BEG_ @@ -25,10 +26,20 @@ void allocator::swap(allocator &other) noexcept { } void *allocator::allocate(std::size_t s, std::size_t a) const { + LIBIMP_LOG_(); + if ((a & (a - 1)) != 0) { + log.error("failed: allocate alignment is not a power of 2."); + return nullptr; + } return get_holder().alloc(s, a); } void allocator::deallocate(void *p, std::size_t s, std::size_t a) const { + LIBIMP_LOG_(); + if ((a & (a - 1)) != 0) { + log.error("failed: allocate alignment is not a power of 2."); + return; + } get_holder().dealloc(p, s, a); } diff --git a/src/libpmr/monotonic_buffer_resource.cpp b/src/libpmr/monotonic_buffer_resource.cpp index 7f2efddc..f43d7237 100644 --- a/src/libpmr/monotonic_buffer_resource.cpp +++ b/src/libpmr/monotonic_buffer_resource.cpp @@ -92,10 +92,6 @@ void *monotonic_buffer_resource::allocate(std::size_t bytes, std::size_t alignme log.error("failed: allocate bytes = 0."); return nullptr; } - if ((alignment & (alignment - 1)) != 0) { - log.error("failed: allocate alignment is not a power of 2."); - return nullptr; - } void *p = head_; auto s = static_cast(tail_ - head_); if (std::align(alignment, bytes, p, s) == nullptr) { diff --git a/test/pmr/test_pmr_memory_resource.cpp b/test/pmr/test_pmr_memory_resource.cpp index f90e4d3b..b624135b 100644 --- a/test/pmr/test_pmr_memory_resource.cpp +++ b/test/pmr/test_pmr_memory_resource.cpp @@ -28,7 +28,7 @@ TEST(memory_resource, traits) { EXPECT_FALSE(pmr::has_allocate::value); EXPECT_FALSE(pmr::has_allocate::value); EXPECT_FALSE(pmr::has_allocate>::value); - EXPECT_TRUE (pmr::has_allocate>::value); + EXPECT_FALSE(pmr::has_allocate>::value); #if defined(LIBIMP_CPP_17) && defined(__cpp_lib_memory_resource) EXPECT_TRUE (pmr::has_allocate::value); EXPECT_TRUE (pmr::has_allocate>::value); diff --git a/test/pmr/test_pmr_monotonic_buffer_resource.cpp b/test/pmr/test_pmr_monotonic_buffer_resource.cpp index fc0bdd5c..a4fe77aa 100644 --- a/test/pmr/test_pmr_monotonic_buffer_resource.cpp +++ b/test/pmr/test_pmr_monotonic_buffer_resource.cpp @@ -102,13 +102,13 @@ TEST(monotonic_buffer_resource, release) { tmp.release(); ASSERT_EQ(dummy.allocated, 0); ASSERT_NE(tmp.allocate(1024), nullptr); - ASSERT_GE(dummy.allocated, 1024); - ASSERT_LE(dummy.allocated, 1024 * 1.5); + ASSERT_GE(dummy.allocated, 1024u); + ASSERT_LE(dummy.allocated, 1024u * 1.5); tmp.release(); ASSERT_EQ(dummy.allocated, 0); ASSERT_NE(tmp.allocate(1024), nullptr); - ASSERT_GE(dummy.allocated, 1024); - ASSERT_LE(dummy.allocated, 1024 * 1.5); + ASSERT_GE(dummy.allocated, 1024u); + ASSERT_LE(dummy.allocated, 1024u * 1.5); } ASSERT_EQ(dummy.allocated, 0); std::array buffer; @@ -119,7 +119,7 @@ TEST(monotonic_buffer_resource, release) { ASSERT_EQ(dummy.allocated, 0); p = tmp.allocate(10240); ASSERT_NE(p, buffer.data()); - ASSERT_LE(dummy.allocated, 10240 + 1024); + ASSERT_LE(dummy.allocated, 10240u + 1024u); tmp.release(); ASSERT_EQ(dummy.allocated, 0); p = tmp.allocate(1024); @@ -127,7 +127,7 @@ TEST(monotonic_buffer_resource, release) { ASSERT_EQ(dummy.allocated, 0); p = tmp.allocate(10240); ASSERT_NE(p, buffer.data()); - ASSERT_LE(dummy.allocated, 10240 + 1024); + ASSERT_LE(dummy.allocated, 10240u + 1024u); } ASSERT_EQ(dummy.allocated, 0); }