[dali_2.3.30] Merge branch 'devel/master'
[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) 2024 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 #include <memory> ///< for std::unique_ptr
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/common/memory-pool-interface.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 /**
34  * @brief Memory pool for a given fixed size of memory.
35  *
36  * The pool will allocate and reclaim blocks of memory without concern for what is
37  * stored in them. This means it is up to the client to construct/destruct objects
38  * and hence determine what data type is stored in the memory block. See FixedSizeObjectAllocator
39  * below for an example client for creating objects of a given type. It is also up to the client
40  * to ensure that the size of the block takes memory alignment into account for the
41  * type of data they wish to store in the block. The TypeSizeWithAlignment<T> template
42  * can be useful for determining the size of memory aligned blocks for a given type.
43  */
44 class FixedSizeMemoryPool : public MemoryPoolInterface
45 {
46 public:
47   using SizeType = MemoryPoolInterface::SizeType;
48   using KeyType  = MemoryPoolInterface::KeyType;
49
50 public:
51   /**
52    * @brief Constructor.
53    *
54    * @param fixedSize The fixed size of each memory allocation. Use TypeSizeWithAlignment if aligned memory is required.
55    * @param initialCapacity The initial size of the memory pool. Defaults to a small value (32) after
56    *                        which the capacity will double as needed.
57    * @param maximumBlockCapacity The maximum size that a new block of memory can be allocated. Defaults to
58    *                             a large value (1024 * 1024 = 1048576).
59    * @param[in] maximumBlockCount The maximum number of blocks that can be allocated, or -1 for unlimited
60    */
61   explicit FixedSizeMemoryPool(SizeType fixedSize, SizeType initialCapacity = 32, SizeType maximumBlockCapacity = 1048576, SizeType maximumBlockCount = 0xffffffff);
62
63   /**
64    * @brief Destructor.
65    */
66   ~FixedSizeMemoryPool() override;
67
68   /**
69    * @copydoc Dali::Internal::MemoryPoolInterface::Allocate
70    */
71   void* Allocate() override;
72
73   /**
74    * @copydoc Dali::Internal::MemoryPoolInterface::AllocateThreadSafe
75    */
76   void* AllocateThreadSafe() override;
77
78   /**
79    * @copydoc Dali::Internal::MemoryPoolInterface::Free
80    */
81   void Free(void* memory) override;
82
83   /**
84    * @copydoc Dali::Internal::MemoryPoolInterface::FreeThreadSafe
85    */
86   void FreeThreadSafe(void* memory) override;
87
88   /**
89    * @copydoc Dali::Internal::MemoryPoolInterface::GetPtrFromKey
90    */
91   void* GetPtrFromKey(KeyType key) override;
92
93   /**
94    * @copydoc Dali::Internal::MemoryPoolInterface::GetKeyFromPtr
95    */
96   KeyType GetKeyFromPtr(void* ptr) override;
97
98   /**
99    * @copydoc Dali::Internal::MemoryPoolInterface::GetCapacity
100    */
101   uint32_t GetCapacity() const override;
102
103   /**
104    * @copydoc Dali::Internal::MemoryPoolInterface::ResetMemoryPool
105    */
106   void ResetMemoryPool() override;
107
108 private:
109   // Undefined
110   FixedSizeMemoryPool(const FixedSizeMemoryPool& fixedSizeMemoryPool);
111
112   // Undefined
113   FixedSizeMemoryPool& operator=(const FixedSizeMemoryPool& fixedSizeMemoryPool);
114
115 private:
116   struct Impl;
117   std::unique_ptr<Impl> mImpl{nullptr};
118 };
119
120 } // namespace Internal
121
122 } // namespace Dali
123
124 #endif // DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_H