Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
  • Loading branch information
mutouyun committed Sep 17, 2023
1 parent 019a60b commit ec602f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/libpmr/allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include <algorithm> // std::swap

#include "libimp/log.h"
#include "libpmr/allocator.h"

LIBPMR_NAMESPACE_BEG_
Expand All @@ -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);
}

Expand Down
4 changes: 0 additions & 4 deletions src/libpmr/monotonic_buffer_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t>(tail_ - head_);
if (std::align(alignment, bytes, p, s) == nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion test/pmr/test_pmr_memory_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TEST(memory_resource, traits) {
EXPECT_FALSE(pmr::has_allocate<void>::value);
EXPECT_FALSE(pmr::has_allocate<int>::value);
EXPECT_FALSE(pmr::has_allocate<std::vector<int>>::value);
EXPECT_TRUE (pmr::has_allocate<std::allocator<int>>::value);
EXPECT_FALSE(pmr::has_allocate<std::allocator<int>>::value);
#if defined(LIBIMP_CPP_17) && defined(__cpp_lib_memory_resource)
EXPECT_TRUE (pmr::has_allocate<std::pmr::memory_resource>::value);
EXPECT_TRUE (pmr::has_allocate<std::pmr::polymorphic_allocator<int>>::value);
Expand Down
12 changes: 6 additions & 6 deletions test/pmr/test_pmr_monotonic_buffer_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<imp::byte, 4096> buffer;
Expand All @@ -119,15 +119,15 @@ 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);
ASSERT_EQ(p, buffer.data());
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);
}

0 comments on commit ec602f5

Please sign in to comment.