Added memory pool logging
[platform/core/uifw/dali-core.git] / dali / internal / common / fixed-size-memory-pool.h
1 #ifndef DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_H
2 #define DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_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 // EXTERNAL INCLUDES
22 #include <stdint.h>
23 #include <cstddef>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 /**
30  * @brief Calculate the size of a type taking alignment into account
31  */
32 template<typename T>
33 struct TypeSizeWithAlignment
34 {
35   ///< The size of the type with alignment taken into account
36   static const size_t size = ((sizeof(T) + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*);
37 };
38
39 /**
40  * @brief Memory pool for a given fixed size of memory.
41  *
42  * The pool will allocate and reclaim blocks of memory without concern for what is
43  * stored in them. This means it is up to the client to construct/destruct objects
44  * and hence determine what data type is stored in the memory block. See FixedSizeObjectAllocator
45  * below for an example client for creating objects of a given type. It is also up to the client
46  * to ensure that the size of the block takes memory alignment into account for the
47  * type of data they wish to store in the block. The TypeSizeWithAlignment<T> template
48  * can be useful for determining the size of memory aligned blocks for a given type.
49  */
50 class FixedSizeMemoryPool
51 {
52 public:
53   using SizeType = uint32_t;
54
55 public:
56   /**
57    * @brief Constructor.
58    *
59    * @param fixedSize The fixed size of each memory allocation. Use TypeSizeWithAlignment if aligned memory is required.
60    * @param initialCapacity The initial size of the memory pool. Defaults to a small value (32) after
61    *                        which the capacity will double as needed.
62    * @param maximumBlockCapacity The maximum size that a new block of memory can be allocated. Defaults to
63    *                             a large value (1024 * 1024 = 1048576).
64    */
65   explicit FixedSizeMemoryPool(SizeType fixedSize, SizeType initialCapacity = 32, SizeType maximumBlockCapacity = 1048576);
66
67   /**
68    * @brief Destructor.
69    */
70   ~FixedSizeMemoryPool();
71
72   /**
73    * @brief Allocate a new fixed size block of memory
74    *
75    * @return Return the newly allocated memory
76    */
77   void* Allocate();
78
79   /**
80    * @brief Thread-safe version of Allocate()
81    *
82    * @return Return the newly allocated memory
83    */
84   void* AllocateThreadSafe();
85
86   /**
87    * @brief Delete a block of memory for the allocation that has been allocated by this memory pool
88    *
89    * @param memory The memory to be deleted. Must have been allocated by this memory pool
90    */
91   void Free(void* memory);
92
93   /**
94    * @brief Thread-safe version of Free()
95    *
96    * @param memory The memory to be deleted. Must have been allocated by this memory pool
97    */
98   void FreeThreadSafe(void* memory);
99
100   /**
101    * Get the current capacity of the memory pool
102    *
103    * @note in release mode, this returns 0, as the block size isn't defined.
104    */
105   uint32_t GetCapacity() const;
106
107 private:
108   // Undefined
109   FixedSizeMemoryPool(const FixedSizeMemoryPool& fixedSizeMemoryPool);
110
111   // Undefined
112   FixedSizeMemoryPool& operator=(const FixedSizeMemoryPool& fixedSizeMemoryPool);
113
114 private:
115   struct Impl;
116   Impl* mImpl;
117 };
118
119 } // namespace Internal
120
121 } // namespace Dali
122
123 #endif // DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_H