From df933ceb2c2e5422adf1a29b9026e3d5de40e756 Mon Sep 17 00:00:00 2001 From: scuzqy Date: Sat, 10 Aug 2024 01:07:01 +0800 Subject: [PATCH] Resolve TODO in aligned_allocator And updated corresponding test. AllocateAligned was designed to take POD types only. --- hwy/aligned_allocator.h | 2 +- hwy/aligned_allocator_test.cc | 17 ++--------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/hwy/aligned_allocator.h b/hwy/aligned_allocator.h index 6274c5d192..a96df0ec69 100644 --- a/hwy/aligned_allocator.h +++ b/hwy/aligned_allocator.h @@ -232,7 +232,6 @@ class AlignedFreer { template void operator()(T* aligned_pointer) const { - // TODO(deymo): assert that we are using a POD type T. FreeAlignedBytes(aligned_pointer, free_, opaque_ptr_); } @@ -251,6 +250,7 @@ using AlignedFreeUniquePtr = std::unique_ptr; template AlignedFreeUniquePtr AllocateAligned(const size_t items, AllocPtr alloc, FreePtr free, void* opaque) { + static_assert(std::is_trivial::value && std::is_standard_layout::value); return AlignedFreeUniquePtr( detail::AllocateAlignedItems(items, alloc, opaque), AlignedFreer(free, opaque)); diff --git a/hwy/aligned_allocator_test.cc b/hwy/aligned_allocator_test.cc index 83e77534a3..9994c5594e 100644 --- a/hwy/aligned_allocator_test.cc +++ b/hwy/aligned_allocator_test.cc @@ -145,8 +145,8 @@ TEST(AlignedAllocatorTest, TestEmptyAlignedUniquePtr) { } TEST(AlignedAllocatorTest, TestEmptyAlignedFreeUniquePtr) { - AlignedFreeUniquePtr> ptr(nullptr, AlignedFreer()); - AlignedFreeUniquePtr[]> arr(nullptr, AlignedFreer()); + AlignedFreeUniquePtr> ptr(nullptr, AlignedFreer()); + AlignedFreeUniquePtr[]> arr(nullptr, AlignedFreer()); } TEST(AlignedAllocatorTest, TestCustomAlloc) { @@ -228,19 +228,6 @@ TEST(AlignedAllocatorTest, TestAllocMultipleInt) { HWY_ASSERT(ret != size_t{0}); } -TEST(AlignedAllocatorTest, TestAllocateAlignedObjectWithoutDestructor) { - int counter = 0; - { - // This doesn't call the constructor. - auto obj = AllocateAligned>(1); - HWY_ASSERT(obj); - obj[0].counter_ = &counter; - } - // Destroying the unique_ptr shouldn't have called the destructor of the - // SampleObject<24>. - HWY_ASSERT_EQ(0, counter); -} - TEST(AlignedAllocatorTest, TestMakeUniqueAlignedArrayWithCustomAlloc) { FakeAllocator fake_alloc; int counter = 0;