1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MEM_ALLOCATOR_H 17 #define MEM_ALLOCATOR_H 18 19 #include <memory> 20 #include <mutex> 21 22 namespace OHOS { 23 namespace Rosen { 24 namespace Drawing { 25 class MemAllocator { 26 public: 27 static constexpr uint32_t MEMORY_EXPANSION_FACTOR = 2; 28 29 MemAllocator(); 30 ~MemAllocator(); 31 32 /* 33 * @brief Creates a read-only memory allocator form a read-only buffer that will not be freed when destroyed. 34 * @param data A read-only buffer. 35 * @param size The size of a read-only buffer. 36 */ 37 bool BuildFromData(const void* data, size_t size); 38 39 /* 40 * @brief Creates a memory allocator by copying the specified data. 41 * @param data A read-only buffer. 42 * @param size The size of a read-only buffer. 43 */ 44 bool BuildFromDataWithCopy(const void* data, size_t size); 45 46 /* 47 * @brief Creates an object of T from a contiguous buffer in the memory allocator. 48 * @return The pointer to an object of T. 49 */ 50 template<typename T, typename... Args> Allocate(Args &&...args)51 T* Allocate(Args&&... args) 52 { 53 if (isReadOnly_) { 54 return nullptr; 55 } 56 57 if (capacity_ - size_ < sizeof(T)) { 58 // The capacity is not enough, expand the capacity 59 if (Resize((capacity_ + sizeof(T)) * MEMORY_EXPANSION_FACTOR) == false) { 60 return nullptr; 61 } 62 } 63 T* obj = nullptr; 64 void* addr = static_cast<void*>(startPtr_ + size_); 65 obj = new (addr) T{std::forward<Args>(args)...}; 66 if (obj) { 67 size_ += sizeof(T); 68 } 69 return obj; 70 } 71 72 /* 73 * @brief Copies a read-only buffer into contiguous memory held by the memory allocator. 74 * @param data A ready-only buffer. 75 * @param size The size of ready-only buffer. 76 */ 77 void* Add(const void* data, size_t size); 78 79 /* 80 * @brief Gets the size of the contiguous memory buffer held by MemAllocator. 81 */ 82 size_t GetSize() const; 83 84 /* 85 * @brief Gets the address of the contiguous memory buffer held by MemAllocator. 86 */ 87 const void* GetData() const; 88 89 /* 90 * @brief Gets the offset from the contiguous memory buffer header pointer 91 held by the MemAllocator based on the addr. 92 * @param addr To get the offset from the header pointer. 93 */ 94 uint32_t AddrToOffset(const void* addr) const; 95 96 /* 97 * @brief Gets the address of the contiguous memory buffer held by the memory allocator from the offset. 98 * @param offset To get the address of the offset. 99 */ 100 void* OffsetToAddr(size_t offset) const; 101 102 MemAllocator(MemAllocator&&) = delete; 103 MemAllocator(const MemAllocator&) = default; 104 MemAllocator& operator=(MemAllocator&&) = delete; 105 MemAllocator& operator=(const MemAllocator&) = default; 106 private: 107 bool Resize(size_t size); 108 void Clear(); 109 110 bool isReadOnly_; 111 size_t capacity_; // The size of the memory block 112 size_t size_; // The size already used 113 char* startPtr_; // Points to the beginning of the memory block 114 }; 115 } // namespace Drawing 116 } // namespace Rosen 117 } // namespace OHOS 118 119 #endif // MEM_ALLOCATOR_H 120