blob: ec4bf77de0f67f0983d1149fd8fd1651dbe8138b [file] [log] [blame]
Chenghan Zhoud4f44d22020-06-18 15:42:06 -04001// 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 Swaminathan15bbbae2021-02-02 20:55:31 -080017#include <memory>
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040018#include <span>
19
20#include "gtest/gtest.h"
21#include "pw_allocator/freelist_heap.h"
22
23namespace pw::allocator {
24
25TEST(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 Swaminathan15bbbae2021-02-02 20:55:31 -080032 auto deleter = [](void* ptr) { free(ptr); };
33
34 std::unique_ptr<void, decltype(deleter)> ptr1(malloc(kAllocSize), deleter);
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040035 const FreeListHeap::HeapStats& freelist_heap_stats =
36 pw_freelist_heap->heap_stats();
Prashanth Swaminathan15bbbae2021-02-02 20:55:31 -080037 ASSERT_NE(ptr1.get(), nullptr);
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040038 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 Swaminathan15bbbae2021-02-02 20:55:31 -080041
42 std::unique_ptr<void, decltype(deleter)> ptr2(
43 realloc(ptr1.release(), kReallocSize), deleter);
44 ASSERT_NE(ptr2.get(), nullptr);
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040045 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 Swaminathan15bbbae2021-02-02 20:55:31 -080049
50 std::unique_ptr<void, decltype(deleter)> ptr3(calloc(kCallocNum, kCallocSize),
51 deleter);
52 ASSERT_NE(ptr3.get(), nullptr);
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040053 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 Swaminathan15bbbae2021-02-02 20:55:31 -080058 free(ptr2.release());
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040059 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 Swaminathan15bbbae2021-02-02 20:55:31 -080063 free(ptr3.release());
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040064 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