Added memory pool logging
[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) 2022 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 namespace Internal
27 {
28 /**
29  * @brief Helper for allocating/deallocating objects using a memory pool.
30  *
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.
33  *
34  */
35 template<typename T>
36 class MemoryPoolObjectAllocator
37 {
38 public:
39   /**
40    * @brief Constructor
41    */
42   MemoryPoolObjectAllocator()
43   : mPool(nullptr)
44   {
45     ResetMemoryPool();
46   }
47
48   /**
49    * @brief Destructor
50    */
51   ~MemoryPoolObjectAllocator()
52   {
53     delete mPool;
54   }
55
56   /**
57    * @brief Allocate from the memory pool
58    *
59    * @return Return the allocated object
60    */
61   T* Allocate()
62   {
63     return new(mPool->Allocate()) T();
64   }
65
66   /**
67    * @brief Thread-safe version of Allocate()
68    *
69    * @return Return the allocated object
70    */
71   T* AllocateThreadSafe()
72   {
73     return new(mPool->AllocateThreadSafe()) T();
74   }
75
76   /**
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
80    *        parameters.
81    *
82    * @return Return the allocated memory block
83    */
84   void* AllocateRaw()
85   {
86     return mPool->Allocate();
87   }
88
89   /**
90    * @brief Thread-safe version of AllocateRaw()
91    *
92    * @return Return the allocated memory block
93    */
94   void* AllocateRawThreadSafe()
95   {
96     return mPool->AllocateThreadSafe();
97   }
98
99   /**
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.
103    *
104    * @param object Pointer to the object to delete
105    */
106   void Free(T* object)
107   {
108     mPool->Free(object);
109   }
110
111   /**
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.
115    *
116    * @param object Pointer to the object to delete
117    */
118   void FreeThreadSafe(T* object)
119   {
120     mPool->FreeThreadSafe(object);
121   }
122
123   /**
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.
126    *
127    * @param object Pointer to the object to delete
128    */
129   void Destroy(T* object)
130   {
131     object->~T();
132     mPool->Free(object);
133   }
134
135   /**
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.
138    *
139    * @param object Pointer to the object to delete
140    */
141   void DestroyThreadSafe(T* object)
142   {
143     object->~T();
144     mPool->FreeThreadSafe(object);
145   }
146
147   /**
148    * @brief Reset the memory pool, unloading all block memory previously allocated
149    */
150   void ResetMemoryPool()
151   {
152     delete mPool;
153
154     mPool = new FixedSizeMemoryPool(TypeSizeWithAlignment<T>::size);
155   }
156
157   /**
158    * @brief Get the capacity of the memory pool
159    */
160   uint32_t GetCapacity() const
161   {
162     return mPool->GetCapacity();
163   }
164
165 private:
166   // Undefined
167   MemoryPoolObjectAllocator(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
168
169   // Undefined
170   MemoryPoolObjectAllocator& operator=(const MemoryPoolObjectAllocator& memoryPoolObjectAllocator);
171
172 private:
173   FixedSizeMemoryPool* mPool; ///< Memory pool from which allocations are made
174 };
175
176 } // namespace Internal
177
178 } // namespace Dali
179
180 #endif // DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H