1 #ifndef DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
2 #define DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
5 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <memory> ///< for std::unique_ptr
25 #include <dali/internal/common/dummy-memory-pool.h>
26 #include <dali/internal/common/fixed-size-memory-pool.h>
33 * @brief Helper for allocating/deallocating objects using a memory pool.
35 * This is a helper class for creating and destroying objects of a single given type.
36 * The type may be a class or POD.
40 class MemoryPoolObjectAllocator
44 * Constant for use with FixedSizePool. We allow the fixed size pools to grow from 32
45 * to 1M entries per block, but maxing the blocks at 27 allows for many millions of
46 * elements to be quickly indexed using a 32 bit key.
48 const uint32_t POOL_MAX_BLOCK_COUNT = 27;
53 MemoryPoolObjectAllocator(bool forceUseMemoryPool = false)
55 mMemoryPoolEnabled(true)
57 #if !defined(__LP64__) && defined(LOW_SPEC_MEMORY_MANAGEMENT_ENABLED)
58 if(!forceUseMemoryPool)
60 mMemoryPoolEnabled = false;
69 ~MemoryPoolObjectAllocator() = default;
72 * @brief Allocate a block of memory from the memory pool of the appropriate size to
73 * store an object of type T. This is usually so the memory can be used in a
74 * placement new for an object of type T with a constructor that takes multiple
77 * @return Return the allocated memory block
81 return mPool->Allocate();
85 * @brief Thread-safe version of AllocateRaw()
87 * @return Return the allocated memory block
89 void* AllocateRawThreadSafe()
91 return mPool->AllocateThreadSafe();
95 * @brief Return the object to the memory pool
96 * Note: This performs a deallocation only, if the object has a destructor and is not
97 * freed from within an overloaded delete operator, Destroy() must be used instead.
99 * @param object Pointer to the object to delete
107 * @brief Thread-safe version of Free()
108 * Note: This performs a deallocation only, if the object has a destructor and is not
109 * freed from within an overloaded delete operator, DestroyThreadSafe() must be used instead.
111 * @param object Pointer to the object to delete
113 void FreeThreadSafe(T* object)
115 mPool->FreeThreadSafe(object);
119 * @brief Return the object to the memory pool after destructing it.
120 * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
122 * @param object Pointer to the object to delete
124 void Destroy(T* object)
131 * @brief Thread-safe version of Destroy()
132 * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
134 * @param object Pointer to the object to delete
136 void DestroyThreadSafe(T* object)
139 mPool->FreeThreadSafe(object);
143 * @brief Reset the memory pool, unloading all block memory previously allocated
145 void ResetMemoryPool()
147 if(DALI_LIKELY(mPool))
149 mPool->ResetMemoryPool();
153 if(mMemoryPoolEnabled)
155 mPool.reset(new FixedSizeMemoryPool(TypeSizeWithAlignment<T>::size, 32, 1024 * 1024, POOL_MAX_BLOCK_COUNT));
159 mPool.reset(new DummyMemoryPool(TypeSizeWithAlignment<T>::size));
165 * Get a pointer to the keyed item.
168 * @param[in] key 32 bit value indexing block/entry
169 * @return ptr to the memory of item, or nullptr if key is invalid
171 * @note on 32 bit systems, there is zero overhead, key is a raw ptr,
172 * and this method will return it's argument.
174 T* GetPtrFromKey(MemoryPoolInterface::KeyType key)
176 return static_cast<T*>(mPool->GetPtrFromKey(key));
180 * Get a key to the pointed at item
181 * @param[in] ptr Pointer to an item in the memory pool
182 * @return key of the item, or -1 if not found.
184 * @note on 32 bit systems, there is zero overhead, key is a raw ptr,
185 * and this method will return it's argument.
187 MemoryPoolInterface::KeyType GetKeyFromPtr(T* ptr)
189 return mPool->GetKeyFromPtr(ptr);
193 * @brief Get the capacity of the memory pool
195 uint32_t GetCapacity() const
197 return mPool->GetCapacity();
202 MemoryPoolObjectAllocator(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
205 MemoryPoolObjectAllocator& operator=(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
208 std::unique_ptr<MemoryPoolInterface> mPool; ///< Memory pool from which allocations are made
210 bool mMemoryPoolEnabled : 1; ///< True if we can use memory pool feature. False otherwise
213 } // namespace Internal
217 #endif // DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H