1 #ifndef DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
2 #define DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
5 * Copyright (c) 2021 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 <dali/internal/common/fixed-size-memory-pool.h>
29 * @brief Helper for allocating/deallocating objects using a memory pool.
31 * This is a helper class for creating and destroying objects of a single given type.
32 * The type may be a class or POD.
36 class MemoryPoolObjectAllocator
42 MemoryPoolObjectAllocator()
51 ~MemoryPoolObjectAllocator()
57 * @brief Allocate from the memory pool
59 * @return Return the allocated object
63 return new(mPool->Allocate()) T();
67 * @brief Thread-safe version of Allocate()
69 * @return Return the allocated object
71 T* AllocateThreadSafe()
73 return new(mPool->AllocateThreadSafe()) T();
77 * @brief Allocate a block of memory from the memory pool of the appropriate size to
78 * store an object of type T. This is usually so the memory can be used in a
79 * placement new for an object of type T with a constructor that takes multiple
82 * @return Return the allocated memory block
86 return mPool->Allocate();
90 * @brief Thread-safe version of AllocateRaw()
92 * @return Return the allocated memory block
94 void* AllocateRawThreadSafe()
96 return mPool->AllocateThreadSafe();
100 * @brief Return the object to the memory pool
101 * Note: This performs a deallocation only, if the object has a destructor and is not
102 * freed from within an overloaded delete operator, Destroy() must be used instead.
104 * @param object Pointer to the object to delete
112 * @brief Thread-safe version of Free()
113 * Note: This performs a deallocation only, if the object has a destructor and is not
114 * freed from within an overloaded delete operator, DestroyThreadSafe() must be used instead.
116 * @param object Pointer to the object to delete
118 void FreeThreadSafe(T* object)
120 mPool->FreeThreadSafe(object);
124 * @brief Return the object to the memory pool after destructing it.
125 * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
127 * @param object Pointer to the object to delete
129 void Destroy(T* object)
136 * @brief Thread-safe version of Destroy()
137 * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
139 * @param object Pointer to the object to delete
141 void DestroyThreadSafe(T* object)
144 mPool->FreeThreadSafe(object);
148 * @brief Reset the memory pool, unloading all block memory previously allocated
150 void ResetMemoryPool()
154 mPool = new FixedSizeMemoryPool(TypeSizeWithAlignment<T>::size);
159 MemoryPoolObjectAllocator(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
162 MemoryPoolObjectAllocator& operator=(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
165 FixedSizeMemoryPool* mPool; ///< Memory pool from which allocations are made
168 } // namespace Internal
172 #endif // DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H