1 #ifndef __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__
2 #define __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__
5 * Copyright (c) 2015 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>
31 * @brief Helper for allocating/deallocating objects using a memory pool.
33 * This is a helper class for creating and destroying objects of a single given type.
34 * The type may be a class or POD.
37 template< typename T >
38 class MemoryPoolObjectAllocator
45 MemoryPoolObjectAllocator()
54 ~MemoryPoolObjectAllocator()
60 * @brief Allocate from the memory pool
62 * @return Return the allocated object
66 return new ( mPool->Allocate() ) T();
70 * @brief Thread-safe version of Allocate()
72 * @return Return the allocated object
74 T* AllocateThreadSafe()
76 return new ( mPool->AllocateThreadSafe() ) T();
80 * @brief Allocate a block of memory from the memory pool of the appropriate size to
81 * store an object of type T. This is usually so the memory can be used in a
82 * placement new for an object of type T with a constructor that takes multiple
85 * @return Return the allocated memory block
89 return mPool->Allocate();
93 * @brief Thread-safe version of AllocateRaw()
95 * @return Return the allocated memory block
97 void* AllocateRawThreadSafe()
99 return mPool->AllocateThreadSafe();
103 * @brief Return the object to the memory pool
105 * @param object Pointer to the object to delete
107 void Free( T* object )
111 mPool->Free( object );
115 * @brief Thread-safe version of Free()
117 * @param object Pointer to the object to delete
119 void FreeThreadSafe( T* object )
123 mPool->FreeThreadSafe( object );
127 * @brief Reset the memory pool, unloading all block memory previously allocated
129 void ResetMemoryPool()
136 mPool = new FixedSizeMemoryPool( TypeSizeWithAlignment< T >::size );
142 MemoryPoolObjectAllocator( const MemoryPoolObjectAllocator& memoryPoolObjectAllocator );
145 MemoryPoolObjectAllocator& operator=( const MemoryPoolObjectAllocator& memoryPoolObjectAllocator );
149 FixedSizeMemoryPool* mPool; ///< Memory pool from which allocations are made
153 } // namespace Internal
157 #endif /* __DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H__ */