1 /*
2 * Copyright (C) 2016 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 #ifndef CHRE_UTIL_MEMORY_POOL_IMPL_H_
18 #define CHRE_UTIL_MEMORY_POOL_IMPL_H_
19
20 #include <cinttypes>
21 #include <utility>
22
23 #include "chre/util/container_support.h"
24 #include "chre/util/memory_pool.h"
25
26 namespace chre {
27 template <typename ElementType, size_t kSize>
MemoryPool()28 MemoryPool<ElementType, kSize>::MemoryPool() {
29 // Initialize the free block list. The initial condition is such that each
30 // block points to the next as being empty. The mFreeBlockCount is used to
31 // ensure that we never allocate out of bounds so we don't need to worry about
32 // the last block referring to one that is non-existent.
33 for (size_t i = 0; i < kSize; i++) {
34 blocks()[i].mNextFreeBlockIndex = i + 1;
35 }
36 }
37
38 template <typename ElementType, size_t kSize>
39 template <typename... Args>
allocate(Args &&...args)40 ElementType *MemoryPool<ElementType, kSize>::allocate(Args &&... args) {
41 if (mFreeBlockCount == 0) {
42 return nullptr;
43 }
44
45 size_t blockIndex = mNextFreeBlockIndex;
46 mNextFreeBlockIndex = blocks()[blockIndex].mNextFreeBlockIndex;
47 mFreeBlockCount--;
48
49 return new (&blocks()[blockIndex].mElement)
50 ElementType(std::forward<Args>(args)...);
51 }
52
53 template <typename ElementType, size_t kSize>
deallocate(ElementType * element)54 void MemoryPool<ElementType, kSize>::deallocate(ElementType *element) {
55 size_t blockIndex;
56 CHRE_ASSERT(getBlockIndex(element, &blockIndex));
57
58 blocks()[blockIndex].mElement.~ElementType();
59 blocks()[blockIndex].mNextFreeBlockIndex = mNextFreeBlockIndex;
60 mNextFreeBlockIndex = blockIndex;
61 mFreeBlockCount++;
62 }
63
64 template <typename ElementType, size_t kSize>
containsAddress(ElementType * element)65 bool MemoryPool<ElementType, kSize>::containsAddress(ElementType *element) {
66 size_t temp;
67 return getBlockIndex(element, &temp);
68 }
69
70 template <typename ElementType, size_t kSize>
getBlockIndex(ElementType * element,size_t * indexOutput)71 bool MemoryPool<ElementType, kSize>::getBlockIndex(ElementType *element,
72 size_t *indexOutput) {
73 uintptr_t elementAddress = reinterpret_cast<uintptr_t>(element);
74 uintptr_t baseAddress = reinterpret_cast<uintptr_t>(&blocks()[0].mElement);
75 *indexOutput = (elementAddress - baseAddress) / sizeof(MemoryPoolBlock);
76
77 return elementAddress >= baseAddress &&
78 elementAddress <=
79 reinterpret_cast<uintptr_t>(&blocks()[kSize - 1].mElement) &&
80 ((elementAddress - baseAddress) % sizeof(MemoryPoolBlock) == 0);
81 }
82
83 template <typename ElementType, size_t kSize>
getFreeBlockCount()84 size_t MemoryPool<ElementType, kSize>::getFreeBlockCount() const {
85 return mFreeBlockCount;
86 }
87
88 template <typename ElementType, size_t kSize>
89 typename MemoryPool<ElementType, kSize>::MemoryPoolBlock *
blocks()90 MemoryPool<ElementType, kSize>::blocks() {
91 return mBlocks.data();
92 }
93
94 } // namespace chre
95
96 #endif // CHRE_UTIL_MEMORY_POOL_IMPL_H_
97