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