From f60cbb7245bddab013f9afc09fa8284091d74785 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Thu, 3 Mar 2016 14:38:10 -0800 Subject: [PATCH] Use unique_ptr to free objects when ASSERT* fail. Now need c++14 and clang to compile std::make_unique. BUG: 27479198 Change-Id: Ib0f2279de85c6259330d5436257f6b978420601a --- libion/tests/Android.mk | 3 +- libion/tests/allocate_test.cpp | 8 ++-- libion/tests/device_test.cpp | 76 ++++++++++++---------------------- 3 files changed, 32 insertions(+), 55 deletions(-) diff --git a/libion/tests/Android.mk b/libion/tests/Android.mk index 894f90e4a..d4d07c232 100644 --- a/libion/tests/Android.mk +++ b/libion/tests/Android.mk @@ -17,8 +17,9 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_MODULE := ion-unit-tests -LOCAL_CFLAGS += -g -Wall -Werror -std=gnu++11 -Wno-missing-field-initializers +LOCAL_CFLAGS += -g -Wall -Werror -Wno-missing-field-initializers LOCAL_SHARED_LIBRARIES += libion LOCAL_C_INCLUDES := $(LOCAL_PATH)/../kernel-headers LOCAL_SRC_FILES := \ diff --git a/libion/tests/allocate_test.cpp b/libion/tests/allocate_test.cpp index e26b30205..3c4524e54 100644 --- a/libion/tests/allocate_test.cpp +++ b/libion/tests/allocate_test.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include @@ -90,7 +91,7 @@ TEST_F(Allocate, RepeatedAllocate) TEST_F(Allocate, Zeroed) { - void *zeroes = calloc(4096, 1); + auto zeroes_ptr = std::make_unique(4096); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -125,14 +126,11 @@ TEST_F(Allocate, Zeroed) ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, map_fd, 0); ASSERT_TRUE(ptr != NULL); - ASSERT_EQ(0, memcmp(ptr, zeroes, 4096)); + ASSERT_EQ(0, memcmp(ptr, zeroes_ptr.get(), 4096)); ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(zeroes); - } TEST_F(Allocate, Large) diff --git a/libion/tests/device_test.cpp b/libion/tests/device_test.cpp index 6f6e1bdcf..0be52bf27 100644 --- a/libion/tests/device_test.cpp +++ b/libion/tests/device_test.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -133,8 +134,8 @@ void Device::dirtyCache(void *ptr, size_t size) TEST_F(Device, KernelReadCached) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -161,14 +162,12 @@ TEST_F(Device, KernelReadCached) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, KernelWriteCached) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (int i = 0; i < 4096; i++) ((char *)buf)[i] = i; @@ -195,14 +194,12 @@ TEST_F(Device, KernelWriteCached) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, DMAReadCached) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -227,14 +224,12 @@ TEST_F(Device, DMAReadCached) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, DMAWriteCached) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (int i = 0; i < 4096; i++) ((char *)buf)[i] = i; @@ -261,14 +256,12 @@ TEST_F(Device, DMAWriteCached) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, KernelReadCachedNeedsSync) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -295,14 +288,12 @@ TEST_F(Device, KernelReadCachedNeedsSync) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, KernelWriteCachedNeedsSync) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (int i = 0; i < 4096; i++) ((char *)buf)[i] = i; @@ -329,14 +320,12 @@ TEST_F(Device, KernelWriteCachedNeedsSync) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, DMAReadCachedNeedsSync) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -363,14 +352,12 @@ TEST_F(Device, DMAReadCachedNeedsSync) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, DMAWriteCachedNeedsSync) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (int i = 0; i < 4096; i++) ((char *)buf)[i] = i; @@ -399,13 +386,11 @@ TEST_F(Device, DMAWriteCachedNeedsSync) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, KernelRead) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -432,14 +417,12 @@ TEST_F(Device, KernelRead) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, KernelWrite) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (int i = 0; i < 4096; i++) ((char *)buf)[i] = i; @@ -466,14 +449,12 @@ TEST_F(Device, KernelWrite) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, DMARead) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask); @@ -498,14 +479,12 @@ TEST_F(Device, DMARead) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, DMAWrite) { - void *alloc = malloc(8192 + 1024); - void *buf = (void *)(ALIGN((unsigned long)alloc, 4096) + 1024); + auto alloc_ptr = std::make_unique(8192 + 1024); + void *buf = (void *)(ALIGN((unsigned long)alloc_ptr.get(), 4096) + 1024); for (int i = 0; i < 4096; i++) ((char *)buf)[i] = i; @@ -532,13 +511,12 @@ TEST_F(Device, DMAWrite) ASSERT_EQ(0, munmap(ptr, 4096)); ASSERT_EQ(0, close(map_fd)); } - - free(alloc); } TEST_F(Device, IsCached) { - void *buf = malloc(4096); + auto buf_ptr = std::make_unique(4096); + void *buf = buf_ptr.get(); for (unsigned int heapMask : m_allHeaps) { SCOPED_TRACE(::testing::Message() << "heap " << heapMask);