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_SYSTEM_SYNCHRONIZED_MEMORY_POOL_H_ 18 #define CHRE_UTIL_SYSTEM_SYNCHRONIZED_MEMORY_POOL_H_ 19 20 #include "chre/platform/mutex.h" 21 #include "chre/util/memory_pool.h" 22 23 namespace chre { 24 25 /** 26 * This is a thread-safe version of the MemoryPool. 27 */ 28 template <typename ElementType, size_t kSize> 29 class SynchronizedMemoryPool : public NonCopyable { 30 public: 31 /** 32 * Function used to decide if an element in the pool matches a certain 33 * condition. 34 */ 35 using MatchingFunction = 36 typename MemoryPool<ElementType, kSize>::MatchingFunction; 37 38 /** 39 * Allocates space for an object, constructs it and returns the pointer to 40 * that object. This method is thread-safe and a lock will be acquired 41 * upon entry to this method. 42 * 43 * @param The arguments to be forwarded to the constructor of the object. 44 * @return A pointer to a constructed object or nullptr if the allocation 45 * fails. 46 */ 47 template <typename... Args> 48 ElementType *allocate(Args &&...args); 49 50 /** 51 * Releases the memory of a previously allocated element. The pointer provided 52 * here must be one that was produced by a previous call to the allocate() 53 * function. The destructor is invoked on the object. This method is 54 * thread-safe and a lock will be acquired upon entry to this method. 55 * 56 * @param A pointer to an element that was previously allocated by the 57 * allocate() function. 58 */ 59 void deallocate(ElementType *element); 60 61 /** 62 * Searches the active blocks in the memory pool, calling 63 * the matchingFunction. The first element such that 64 * matchingFunction returns true is returned, else nullptr. 65 * This method is thread-safe and a lock will be acquired 66 * upon entry to this method. 67 * 68 * @param matchingFunction The function to match. 69 * @param data The data passed to matchingFunction. 70 * @return the first matching element or nullptr if not found. 71 */ 72 ElementType *find(MatchingFunction *matchingFunction, void *data); 73 74 /** 75 * @return the number of unused blocks in this memory pool. 76 */ 77 size_t getFreeBlockCount(); 78 79 /** 80 * @return true if this memory pool is full. 81 */ full()82 bool full() { 83 return getFreeBlockCount() == 0; 84 } 85 86 private: 87 //! The mutex used to guard access to this memory pool. 88 Mutex mMutex; 89 90 //! The non-synchronized MemoryPool that is used to implement this thread-safe 91 //! version. 92 MemoryPool<ElementType, kSize> mMemoryPool; 93 }; 94 95 } // namespace chre 96 97 #include "chre/util/system/synchronized_memory_pool_impl.h" // IWYU pragma: export 98 99 #endif // CHRE_UTIL_SYSTEM_SYNCHRONIZED_MEMORY_POOL_H_ 100