Skip to content

Commit

Permalink
Resolve TODO in aligned_allocator
Browse files Browse the repository at this point in the history
And updated corresponding test.
AllocateAligned was designed to take POD types only.
  • Loading branch information
scuzqy committed Aug 9, 2024
1 parent 5ef2ec0 commit df933ce
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hwy/aligned_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ class AlignedFreer {

template <typename T>
void operator()(T* aligned_pointer) const {
// TODO(deymo): assert that we are using a POD type T.
FreeAlignedBytes(aligned_pointer, free_, opaque_ptr_);
}

Expand All @@ -251,6 +250,7 @@ using AlignedFreeUniquePtr = std::unique_ptr<T, AlignedFreer>;
template <typename T>
AlignedFreeUniquePtr<T[]> AllocateAligned(const size_t items, AllocPtr alloc,
FreePtr free, void* opaque) {
static_assert(std::is_trivial<T>::value && std::is_standard_layout<T>::value);
return AlignedFreeUniquePtr<T[]>(
detail::AllocateAlignedItems<T>(items, alloc, opaque),
AlignedFreer(free, opaque));
Expand Down
17 changes: 2 additions & 15 deletions hwy/aligned_allocator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ TEST(AlignedAllocatorTest, TestEmptyAlignedUniquePtr) {
}

TEST(AlignedAllocatorTest, TestEmptyAlignedFreeUniquePtr) {
AlignedFreeUniquePtr<SampleObject<32>> ptr(nullptr, AlignedFreer());
AlignedFreeUniquePtr<SampleObject<32>[]> arr(nullptr, AlignedFreer());
AlignedFreeUniquePtr<std::array<char, 32>> ptr(nullptr, AlignedFreer());
AlignedFreeUniquePtr<std::array<char, 32>[]> arr(nullptr, AlignedFreer());
}

TEST(AlignedAllocatorTest, TestCustomAlloc) {
Expand Down Expand Up @@ -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<SampleObject<24>>(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;
Expand Down

0 comments on commit df933ce

Please sign in to comment.