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 #include "recording/mem_allocator.h"
17
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace Drawing {
23
24 static constexpr size_t MEM_SIZE_MAX = SIZE_MAX;
25
MemAllocator()26 MemAllocator::MemAllocator() : isReadOnly_(false), capacity_(0), size_(0), startPtr_(nullptr) {}
27
~MemAllocator()28 MemAllocator::~MemAllocator()
29 {
30 Clear();
31 }
32
BuildFromData(const void * data,size_t size)33 bool MemAllocator::BuildFromData(const void* data, size_t size)
34 {
35 if (!data || size == 0 || size > MEM_SIZE_MAX) {
36 return false;
37 }
38
39 Clear();
40 isReadOnly_ = true;
41 startPtr_ = const_cast<char*>(static_cast<const char*>(data));
42 capacity_ = size;
43 size_ = size;
44
45 return true;
46 }
47
BuildFromDataWithCopy(const void * data,size_t size)48 bool MemAllocator::BuildFromDataWithCopy(const void* data, size_t size)
49 {
50 if (!data || size == 0 || size > MEM_SIZE_MAX) {
51 return false;
52 }
53
54 Clear();
55 isReadOnly_ = false;
56 Add(data, size);
57
58 return true;
59 }
60
Clear()61 void MemAllocator::Clear()
62 {
63 if (!isReadOnly_ && startPtr_) {
64 delete[] startPtr_;
65 }
66 isReadOnly_ = true;
67 startPtr_ = nullptr;
68 capacity_ = 0;
69 size_ = 0;
70 }
71
Resize(size_t size)72 bool MemAllocator::Resize(size_t size)
73 {
74 if (isReadOnly_ || size == 0 || size > MEM_SIZE_MAX || size < size_) {
75 return false;
76 }
77
78 char* newData = new char[size];
79 if (!newData) {
80 return false;
81 }
82
83 if (startPtr_) {
84 if (!memcpy_s(newData, size, startPtr_, size_)) {
85 delete[] startPtr_;
86 } else {
87 delete[] newData;
88 return false;
89 }
90 }
91 startPtr_ = newData;
92 capacity_ = size;
93 return true;
94 }
95
Add(const void * data,size_t size)96 void* MemAllocator::Add(const void* data, size_t size)
97 {
98 if (isReadOnly_ || !data || size == 0 || size > MEM_SIZE_MAX) {
99 return nullptr;
100 }
101
102 if (capacity_ - size_ < size) {
103 // The capacity is not enough, expand the capacity
104 if (Resize((capacity_ + size) * MEMORY_EXPANSION_FACTOR) == false) {
105 return nullptr;
106 }
107 }
108 if (!memcpy_s(startPtr_ + size_, capacity_ - size_, data, size)) {
109 size_ += size;
110 return startPtr_ + size_ - size;
111 } else {
112 return nullptr;
113 }
114 }
115
GetSize() const116 size_t MemAllocator::GetSize() const
117 {
118 return size_;
119 }
120
GetData() const121 const void* MemAllocator::GetData() const
122 {
123 return startPtr_;
124 }
125
AddrToOffset(const void * addr) const126 uint32_t MemAllocator::AddrToOffset(const void* addr) const
127 {
128 if (!addr) {
129 return 0;
130 }
131
132 auto offset = static_cast<uint32_t>(static_cast<const char*>(addr) - startPtr_);
133 if (offset > size_) {
134 return 0;
135 }
136 return offset;
137 }
138
OffsetToAddr(size_t offset) const139 void* MemAllocator::OffsetToAddr(size_t offset) const
140 {
141 if (offset >= size_) {
142 return nullptr;
143 }
144
145 return static_cast<void*>(startPtr_ + offset);
146 }
147 } // namespace Drawing
148 } // namespace Rosen
149 } // namespace OHOS
150