1 #ifndef DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_H
2 #define DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_H
5 * Copyright (c) 2024 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.
24 #include <memory> ///< for std::unique_ptr
27 #include <dali/internal/common/memory-pool-interface.h>
34 * @brief Memory pool for a given fixed size of memory.
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.
44 class FixedSizeMemoryPool : public MemoryPoolInterface
47 using SizeType = MemoryPoolInterface::SizeType;
48 using KeyType = MemoryPoolInterface::KeyType;
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
61 explicit FixedSizeMemoryPool(SizeType fixedSize, SizeType initialCapacity = 32, SizeType maximumBlockCapacity = 1048576, SizeType maximumBlockCount = 0xffffffff);
66 ~FixedSizeMemoryPool() override;
69 * @copydoc Dali::Internal::MemoryPoolInterface::Allocate
71 void* Allocate() override;
74 * @copydoc Dali::Internal::MemoryPoolInterface::AllocateThreadSafe
76 void* AllocateThreadSafe() override;
79 * @copydoc Dali::Internal::MemoryPoolInterface::Free
81 void Free(void* memory) override;
84 * @copydoc Dali::Internal::MemoryPoolInterface::FreeThreadSafe
86 void FreeThreadSafe(void* memory) override;
89 * @copydoc Dali::Internal::MemoryPoolInterface::GetPtrFromKey
91 void* GetPtrFromKey(KeyType key) override;
94 * @copydoc Dali::Internal::MemoryPoolInterface::GetKeyFromPtr
96 KeyType GetKeyFromPtr(void* ptr) override;
99 * @copydoc Dali::Internal::MemoryPoolInterface::GetCapacity
101 uint32_t GetCapacity() const override;
104 * @copydoc Dali::Internal::MemoryPoolInterface::ResetMemoryPool
106 void ResetMemoryPool() override;
110 FixedSizeMemoryPool(const FixedSizeMemoryPool& fixedSizeMemoryPool);
113 FixedSizeMemoryPool& operator=(const FixedSizeMemoryPool& fixedSizeMemoryPool);
117 std::unique_ptr<Impl> mImpl{nullptr};
120 } // namespace Internal
124 #endif // DALI_INTERNAL_FIXED_SIZE_MEMORY_POOL_H