• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/synchronized_expandable_memory_pool.h"
18 
19 #include "chre/util/synchronized_memory_pool.h"
20 #include "gtest/gtest.h"
21 
22 using chre::SynchronizedExpandableMemoryPool;
23 
24 namespace {
25 
26 class ConstructorCount {
27  public:
ConstructorCount(int value_)28   ConstructorCount(int value_) : value(value_) {
29     sConstructedCounter++;
30   }
~ConstructorCount()31   ~ConstructorCount() {
32     sConstructedCounter--;
33   }
getValue()34   int getValue() {
35     return value;
36   }
37 
38   static ssize_t sConstructedCounter;
39 
40  private:
41   const int value;
42 };
43 
44 ssize_t ConstructorCount::sConstructedCounter = 0;
45 
46 }  // namespace
47 
TEST(SynchronizedExpandAbleMemoryPool,InitStateTest)48 TEST(SynchronizedExpandAbleMemoryPool, InitStateTest) {
49   constexpr uint8_t blockSize = 3;
50   constexpr uint8_t maxBlockCount = 5;
51   constexpr uint8_t staticBlockCount = 3;
52   SynchronizedExpandableMemoryPool<int, blockSize, maxBlockCount>
53       testMemoryPool(staticBlockCount);
54 
55   ASSERT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount);
56   ASSERT_EQ(testMemoryPool.getBlockCount(), staticBlockCount);
57 }
58 
TEST(SynchronizedExpandAbleMemoryPool,OneAllocateAndDeallocate)59 TEST(SynchronizedExpandAbleMemoryPool, OneAllocateAndDeallocate) {
60   constexpr uint8_t blockSize = 3;
61   constexpr uint8_t maxBlockCount = 5;
62 
63   SynchronizedExpandableMemoryPool<ConstructorCount, blockSize, maxBlockCount>
64       testMemoryPool;
65   ASSERT_EQ(testMemoryPool.getBlockCount(), 1);
66 
67   ConstructorCount *temp = testMemoryPool.allocate(10);
68   ASSERT_NE(temp, nullptr);
69   ASSERT_EQ(ConstructorCount::sConstructedCounter, 1);
70   ASSERT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount - 1);
71   testMemoryPool.deallocate(temp);
72   ASSERT_EQ(ConstructorCount::sConstructedCounter, 0);
73   ASSERT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount);
74 }
75 
TEST(SynchronizedExpandAbleMemoryPool,HysteresisDeallocation)76 TEST(SynchronizedExpandAbleMemoryPool, HysteresisDeallocation) {
77   constexpr uint8_t blockSize = 3;
78   constexpr uint8_t maxBlockCount = 4;
79   constexpr uint8_t staticBlockCount = 2;
80 
81   SynchronizedExpandableMemoryPool<int, blockSize, maxBlockCount>
82       testMemoryPool(staticBlockCount);
83   int *tempDataPtrs[blockSize * maxBlockCount];
84 
85   for (int i = 0; i < blockSize * maxBlockCount; i++) {
86     tempDataPtrs[i] = testMemoryPool.allocate(i);
87   }
88   EXPECT_EQ(testMemoryPool.getBlockCount(), maxBlockCount);
89 
90   for (int i = blockSize * maxBlockCount - 1;
91        i >= (maxBlockCount - 1) * blockSize; i--) {
92     testMemoryPool.deallocate(tempDataPtrs[i]);
93   }
94 
95   // Should not remove the last block if it just got empty.
96   EXPECT_EQ(testMemoryPool.getBlockCount(), maxBlockCount);
97 
98   for (int i = 0; i < (maxBlockCount - 1) * blockSize; i++) {
99     testMemoryPool.deallocate(tempDataPtrs[i]);
100   }
101 
102   // Once it is empty, it should not still hold maxBlockCount as before.
103   EXPECT_EQ(testMemoryPool.getFreeSpaceCount(), blockSize * maxBlockCount);
104   EXPECT_EQ(testMemoryPool.getBlockCount(), staticBlockCount);
105 }