Moved SingletonService into dali-core
[platform/core/uifw/dali-core.git] / dali / internal / common / memory-pool-object-allocator.h
1 #ifndef DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
2 #define DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/fixed-size-memory-pool.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 /**
31  * @brief Helper for allocating/deallocating objects using a memory pool.
32  *
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.
35  *
36  */
37 template< typename T >
38 class MemoryPoolObjectAllocator
39 {
40 public:
41
42   /**
43    * @brief Constructor
44    */
45   MemoryPoolObjectAllocator()
46   : mPool( NULL )
47   {
48     ResetMemoryPool();
49   }
50
51   /**
52    * @brief Destructor
53    */
54   ~MemoryPoolObjectAllocator()
55   {
56     delete mPool;
57   }
58
59   /**
60    * @brief Allocate from the memory pool
61    *
62    * @return Return the allocated object
63    */
64   T* Allocate()
65   {
66     return new ( mPool->Allocate() ) T();
67   }
68
69   /**
70    * @brief Thread-safe version of Allocate()
71    *
72    * @return Return the allocated object
73    */
74   T* AllocateThreadSafe()
75   {
76     return new ( mPool->AllocateThreadSafe() ) T();
77   }
78
79   /**
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
83    *        parameters.
84    *
85    * @return Return the allocated memory block
86    */
87   void* AllocateRaw()
88   {
89     return mPool->Allocate();
90   }
91
92   /**
93    * @brief Thread-safe version of AllocateRaw()
94    *
95    * @return Return the allocated memory block
96    */
97   void* AllocateRawThreadSafe()
98   {
99     return mPool->AllocateThreadSafe();
100   }
101
102   /**
103    * @brief Return the object to the memory pool
104    * Note: This performs a deallocation only, if the object has a destructor and is not
105    * freed from within an overloaded delete operator, Destroy() must be used instead.
106    *
107    * @param object Pointer to the object to delete
108    */
109   void Free( T* object )
110   {
111     mPool->Free( object );
112   }
113
114   /**
115    * @brief Thread-safe version of Free()
116    * Note: This performs a deallocation only, if the object has a destructor and is not
117    * freed from within an overloaded delete operator, DestroyThreadSafe() must be used instead.
118    *
119    * @param object Pointer to the object to delete
120    */
121   void FreeThreadSafe( T* object )
122   {
123     mPool->FreeThreadSafe( object );
124   }
125
126   /**
127    * @brief Return the object to the memory pool after destructing it.
128    * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
129    *
130    * @param object Pointer to the object to delete
131    */
132   void Destroy( T* object )
133   {
134     object->~T();
135     mPool->Free( object );
136   }
137
138   /**
139    * @brief Thread-safe version of Destroy()
140    * Note: Do not call this from an overloaded delete operator, as this will already have called the objects destructor.
141    *
142    * @param object Pointer to the object to delete
143    */
144   void DestroyThreadSafe( T* object )
145   {
146     object->~T();
147     mPool->FreeThreadSafe( object );
148   }
149
150   /**
151    * @brief Reset the memory pool, unloading all block memory previously allocated
152    */
153   void ResetMemoryPool()
154   {
155     delete mPool;
156
157     mPool = new FixedSizeMemoryPool( TypeSizeWithAlignment< T >::size );
158   }
159
160 private:
161
162   // Undefined
163   MemoryPoolObjectAllocator( const MemoryPoolObjectAllocator& memoryPoolObjectAllocator );
164
165   // Undefined
166   MemoryPoolObjectAllocator& operator=( const MemoryPoolObjectAllocator& memoryPoolObjectAllocator );
167
168 private:
169
170   FixedSizeMemoryPool* mPool;      ///< Memory pool from which allocations are made
171
172 };
173
174 } // namespace Internal
175
176 } // namespace Dali
177
178 #endif // DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H