Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve TODO in aligned_allocator #2298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 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,8 @@ 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,
"Can only allocate POD structs");
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
Loading