1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chre/util/system/synchronized_expandable_memory_pool.h"
18
19 #include "gtest/gtest.h"
20
21 using chre::SynchronizedExpandableMemoryPool;
22
23 namespace {
24
25 class ConstructorCount {
26 public:
ConstructorCount(int value_)27 ConstructorCount(int value_) : value(value_) {
28 sConstructedCounter++;
29 }
~ConstructorCount()30 ~ConstructorCount() {
31 sConstructedCounter--;
32 }
getValue()33 int getValue() {
34 return value;
35 }
36
37 static ssize_t sConstructedCounter;
38
39 private:
40 const int value;
41 };
42
43 ssize_t ConstructorCount::sConstructedCounter = 0;
44
45 } // namespace
46
TEST(SynchronizedExpandAbleMemoryPool,InitStateTest)47 TEST(SynchronizedExpandAbleMemoryPool, InitStateTest) {
48 constexpr uint8_t blockSize = 3;
49 constexpr uint8_t maxBlockCount = 5;
50 constexpr uint8_t staticBlockCount = 3;
51 SynchronizedExpandableMemoryPool<int, blockSize, maxBlockCount>
52 testMemoryPool(staticBlockCount);
53
54 ASSERT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount);
55 ASSERT_EQ(testMemoryPool.getBlockCount(), staticBlockCount);
56 }
57
TEST(SynchronizedExpandAbleMemoryPool,OneAllocateAndDeallocate)58 TEST(SynchronizedExpandAbleMemoryPool, OneAllocateAndDeallocate) {
59 constexpr uint8_t blockSize = 3;
60 constexpr uint8_t maxBlockCount = 5;
61
62 SynchronizedExpandableMemoryPool<ConstructorCount, blockSize, maxBlockCount>
63 testMemoryPool;
64 ASSERT_EQ(testMemoryPool.getBlockCount(), 1);
65
66 ConstructorCount *temp = testMemoryPool.allocate(10);
67 ASSERT_NE(temp, nullptr);
68 ASSERT_EQ(ConstructorCount::sConstructedCounter, 1);
69 ASSERT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount - 1);
70 testMemoryPool.deallocate(temp);
71 ASSERT_EQ(ConstructorCount::sConstructedCounter, 0);
72 ASSERT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount);
73 }
74
TEST(SynchronizedExpandAbleMemoryPool,HysteresisDeallocation)75 TEST(SynchronizedExpandAbleMemoryPool, HysteresisDeallocation) {
76 constexpr uint8_t blockSize = 3;
77 constexpr uint8_t maxBlockCount = 4;
78 constexpr uint8_t staticBlockCount = 2;
79
80 SynchronizedExpandableMemoryPool<int, blockSize, maxBlockCount>
81 testMemoryPool(staticBlockCount);
82 int *tempDataPtrs[blockSize * maxBlockCount];
83
84 for (int i = 0; i < blockSize * maxBlockCount; i++) {
85 tempDataPtrs[i] = testMemoryPool.allocate(i);
86 }
87 EXPECT_EQ(testMemoryPool.getBlockCount(), maxBlockCount);
88
89 for (int i = blockSize * maxBlockCount - 1;
90 i >= (maxBlockCount - 1) * blockSize; i--) {
91 testMemoryPool.deallocate(tempDataPtrs[i]);
92 }
93
94 // Should not remove the last block if it just got empty.
95 EXPECT_EQ(testMemoryPool.getBlockCount(), maxBlockCount);
96
97 for (int i = 0; i < (maxBlockCount - 1) * blockSize; i++) {
98 testMemoryPool.deallocate(tempDataPtrs[i]);
99 }
100
101 // Once it is empty, it should not still hold maxBlockCount as before.
102 EXPECT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount);
103 EXPECT_EQ(testMemoryPool.getBlockCount(), staticBlockCount);
104 }
105