Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_malloc_freelist/freelist_malloc.h" |
| 16 | |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 17 | #include <memory> |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 18 | #include <span> |
| 19 | |
| 20 | #include "gtest/gtest.h" |
| 21 | #include "pw_allocator/freelist_heap.h" |
| 22 | |
| 23 | namespace pw::allocator { |
| 24 | |
| 25 | TEST(FreeListMalloc, ReplacingMalloc) { |
| 26 | constexpr size_t kAllocSize = 256; |
| 27 | constexpr size_t kReallocSize = 512; |
| 28 | constexpr size_t kCallocNum = 4; |
| 29 | constexpr size_t kCallocSize = 64; |
| 30 | constexpr size_t zero = 0; |
| 31 | |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 32 | auto deleter = [](void* ptr) { free(ptr); }; |
| 33 | |
| 34 | std::unique_ptr<void, decltype(deleter)> ptr1(malloc(kAllocSize), deleter); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 35 | const FreeListHeap::HeapStats& freelist_heap_stats = |
| 36 | pw_freelist_heap->heap_stats(); |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 37 | ASSERT_NE(ptr1.get(), nullptr); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 38 | EXPECT_EQ(freelist_heap_stats.bytes_allocated, kAllocSize); |
| 39 | EXPECT_EQ(freelist_heap_stats.cumulative_allocated, kAllocSize); |
| 40 | EXPECT_EQ(freelist_heap_stats.cumulative_freed, zero); |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 41 | |
| 42 | std::unique_ptr<void, decltype(deleter)> ptr2( |
| 43 | realloc(ptr1.release(), kReallocSize), deleter); |
| 44 | ASSERT_NE(ptr2.get(), nullptr); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 45 | EXPECT_EQ(freelist_heap_stats.bytes_allocated, kReallocSize); |
| 46 | EXPECT_EQ(freelist_heap_stats.cumulative_allocated, |
| 47 | kAllocSize + kReallocSize); |
| 48 | EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize); |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 49 | |
| 50 | std::unique_ptr<void, decltype(deleter)> ptr3(calloc(kCallocNum, kCallocSize), |
| 51 | deleter); |
| 52 | ASSERT_NE(ptr3.get(), nullptr); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 53 | EXPECT_EQ(freelist_heap_stats.bytes_allocated, |
| 54 | kReallocSize + kCallocNum * kCallocSize); |
| 55 | EXPECT_EQ(freelist_heap_stats.cumulative_allocated, |
| 56 | kAllocSize + kReallocSize + kCallocNum * kCallocSize); |
| 57 | EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize); |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 58 | free(ptr2.release()); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 59 | EXPECT_EQ(freelist_heap_stats.bytes_allocated, kCallocNum * kCallocSize); |
| 60 | EXPECT_EQ(freelist_heap_stats.cumulative_allocated, |
| 61 | kAllocSize + kReallocSize + kCallocNum * kCallocSize); |
| 62 | EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize + kReallocSize); |
Prashanth Swaminathan | 15bbbae | 2021-02-02 20:55:31 -0800 | [diff] [blame] | 63 | free(ptr3.release()); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 64 | EXPECT_EQ(freelist_heap_stats.bytes_allocated, zero); |
| 65 | EXPECT_EQ(freelist_heap_stats.cumulative_allocated, |
| 66 | kAllocSize + kReallocSize + kCallocNum * kCallocSize); |
| 67 | EXPECT_EQ(freelist_heap_stats.cumulative_freed, |
| 68 | kAllocSize + kReallocSize + kCallocNum * kCallocSize); |
| 69 | } |
| 70 | |
| 71 | } // namespace pw::allocator |