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 #include "utils/drawing_macros.h" 22 23 namespace OHOS { 24 namespace Rosen { 25 namespace Drawing { 26 class DRAWING_API MemAllocator { 27 public: 28 static constexpr uint32_t MEMORY_EXPANSION_FACTOR = 2; 29 30 MemAllocator(); 31 ~MemAllocator(); 32 33 /* 34 * @brief Creates a read-only memory allocator form a read-only buffer that will not be freed when destroyed. 35 * @param data A read-only buffer. 36 * @param size The size of a read-only buffer. 37 */ 38 bool BuildFromData(const void* data, size_t size); 39 40 /* 41 * @brief Creates a memory allocator by copying the specified data. 42 * @param data A read-only buffer. 43 * @param size The size of a read-only buffer. 44 */ 45 bool BuildFromDataWithCopy(const void* data, size_t size); 46 47 /* 48 * @brief Creates an object of T from a contiguous buffer in the memory allocator. 49 * @return The pointer to an object of T. 50 */ 51 template<typename T, typename... Args> Allocate(Args &&...args)52 T* Allocate(Args&&... args) 53 { 54 if (isReadOnly_) { 55 return nullptr; 56 } 57 58 if (capacity_ - size_ < sizeof(T)) { 59 // The capacity is not enough, expand the capacity 60 if (Resize((capacity_ + sizeof(T)) * MEMORY_EXPANSION_FACTOR) == false) { 61 return nullptr; 62 } 63 } 64 T* obj = nullptr; 65 void* addr = static_cast<void*>(startPtr_ + size_); 66 obj = new (addr) T{std::forward<Args>(args)...}; 67 if (obj) { 68 size_ += sizeof(T); 69 } 70 return obj; 71 } 72 73 /* 74 * @brief Copies a read-only buffer into contiguous memory held by the memory allocator. 75 * @param data A ready-only buffer. 76 * @param size The size of ready-only buffer. 77 */ 78 void* Add(const void* data, size_t size); 79 80 /* 81 * @brief Gets the size of the contiguous memory buffer held by MemAllocator. 82 */ 83 size_t GetSize() const; 84 85 /* 86 * @brief Gets the address of the contiguous memory buffer held by MemAllocator. 87 */ 88 const void* GetData() const; 89 90 /* 91 * @brief Gets the offset from the contiguous memory buffer header pointer 92 held by the MemAllocator based on the addr. 93 * @param addr To get the offset from the header pointer. 94 */ 95 uint32_t AddrToOffset(const void* addr) const; 96 97 /* 98 * @brief Gets the address of the contiguous memory buffer held by the memory allocator from the offset. 99 * @param offset To get the address of the offset. 100 */ 101 void* OffsetToAddr(size_t offset) const; 102 103 void ClearData(); 104 105 MemAllocator(MemAllocator&&) = delete; 106 MemAllocator(const MemAllocator&) = default; 107 MemAllocator& operator=(MemAllocator&&) = delete; 108 MemAllocator& operator=(const MemAllocator&) = default; 109 private: 110 bool Resize(size_t size); 111 void Clear(); 112 113 bool isReadOnly_; 114 size_t capacity_; // The size of the memory block 115 size_t size_; // The size already used 116 char* startPtr_; // Points to the beginning of the memory block 117 }; 118 } // namespace Drawing 119 } // namespace Rosen 120 } // namespace OHOS 121 122 #endif // MEM_ALLOCATOR_H 123