mirror of
https://fuchsia.googlesource.com/third_party/pigweed.googlesource.com/pigweed/pigweed
synced 2024-09-20 22:00:58 +00:00
pw_malloc_freelist: Avoid memory leak in unittest
Defer pointer release when test assertions fail. Change-Id: Ife3d03203bb89874854c0213aca92a8801df246b Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/31640 Reviewed-by: Keir Mierle <keir@google.com> Commit-Queue: Prashanth Swaminathan <prashanthsw@google.com>
This commit is contained in:
parent
f40762d84a
commit
15bbbae1d1
|
@ -14,6 +14,7 @@
|
|||
|
||||
#include "pw_malloc_freelist/freelist_malloc.h"
|
||||
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
@ -28,32 +29,38 @@ TEST(FreeListMalloc, ReplacingMalloc) {
|
|||
constexpr size_t kCallocSize = 64;
|
||||
constexpr size_t zero = 0;
|
||||
|
||||
void* ptr1 = malloc(kAllocSize);
|
||||
auto deleter = [](void* ptr) { free(ptr); };
|
||||
|
||||
std::unique_ptr<void, decltype(deleter)> ptr1(malloc(kAllocSize), deleter);
|
||||
const FreeListHeap::HeapStats& freelist_heap_stats =
|
||||
pw_freelist_heap->heap_stats();
|
||||
ASSERT_NE(ptr1, nullptr);
|
||||
ASSERT_NE(ptr1.get(), nullptr);
|
||||
EXPECT_EQ(freelist_heap_stats.bytes_allocated, kAllocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_allocated, kAllocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_freed, zero);
|
||||
void* ptr2 = realloc(ptr1, kReallocSize);
|
||||
ASSERT_NE(ptr2, nullptr);
|
||||
|
||||
std::unique_ptr<void, decltype(deleter)> ptr2(
|
||||
realloc(ptr1.release(), kReallocSize), deleter);
|
||||
ASSERT_NE(ptr2.get(), nullptr);
|
||||
EXPECT_EQ(freelist_heap_stats.bytes_allocated, kReallocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
|
||||
kAllocSize + kReallocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize);
|
||||
void* ptr3 = calloc(kCallocNum, kCallocSize);
|
||||
ASSERT_NE(ptr3, nullptr);
|
||||
|
||||
std::unique_ptr<void, decltype(deleter)> ptr3(calloc(kCallocNum, kCallocSize),
|
||||
deleter);
|
||||
ASSERT_NE(ptr3.get(), nullptr);
|
||||
EXPECT_EQ(freelist_heap_stats.bytes_allocated,
|
||||
kReallocSize + kCallocNum * kCallocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
|
||||
kAllocSize + kReallocSize + kCallocNum * kCallocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize);
|
||||
free(ptr2);
|
||||
free(ptr2.release());
|
||||
EXPECT_EQ(freelist_heap_stats.bytes_allocated, kCallocNum * kCallocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
|
||||
kAllocSize + kReallocSize + kCallocNum * kCallocSize);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize + kReallocSize);
|
||||
free(ptr3);
|
||||
free(ptr3.release());
|
||||
EXPECT_EQ(freelist_heap_stats.bytes_allocated, zero);
|
||||
EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
|
||||
kAllocSize + kReallocSize + kCallocNum * kCallocSize);
|
||||
|
|
Loading…
Reference in New Issue
Block a user