1 /*
2 * Copyright (c) 2021 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 "ecmascript/mem/dyn_chunk.h"
17
18 #include "securec.h"
19
20 namespace panda::ecmascript {
Expand(size_t newSize)21 int DynChunk::Expand(size_t newSize)
22 {
23 if (newSize > allocatedSize_) {
24 if (error_) {
25 return FAILURE;
26 }
27 ASSERT(allocatedSize_ <= std::numeric_limits<size_t>::max() / ALLOCATE_MULTIPLIER);
28 size_t size = allocatedSize_ * ALLOCATE_MULTIPLIER;
29 if (size > newSize) {
30 newSize = size;
31 }
32 newSize = std::max(newSize, ALLOCATE_MIN_SIZE);
33 auto *newBuf = chunk_->NewArray<uint8_t>(newSize);
34 if (newBuf == nullptr) {
35 error_ = true;
36 return FAILURE;
37 }
38 if (memset_s(newBuf, newSize, 0, newSize) != EOK) {
39 error_ = true;
40 return FAILURE;
41 }
42 if (buf_ != nullptr) {
43 if (memcpy_s(newBuf, size_, buf_, size_) != EOK) {
44 error_ = true;
45 return FAILURE;
46 }
47 }
48 buf_ = newBuf;
49 allocatedSize_ = newSize;
50 }
51 return SUCCESS;
52 }
53
Insert(uint32_t position,size_t len)54 int DynChunk::Insert(uint32_t position, size_t len)
55 {
56 if (size_ < position) {
57 return FAILURE;
58 }
59 if (Expand(size_ + len) != 0) {
60 return FAILURE;
61 }
62 size_t moveSize = size_ - position;
63 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
64 if (memmove_s(buf_ + position + len, moveSize, buf_ + position, moveSize) != EOK) {
65 return FAILURE;
66 }
67 size_ += len;
68 return SUCCESS;
69 }
70
Emit(const uint8_t * data,size_t length)71 int DynChunk::Emit(const uint8_t *data, size_t length)
72 {
73 if (UNLIKELY((size_ + length) > allocatedSize_)) {
74 if (Expand(size_ + length) != 0) {
75 return FAILURE;
76 }
77 }
78
79 if (memcpy_s(buf_ + size_, // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
80 length, data, length) != EOK) {
81 return FAILURE;
82 }
83 size_ += length;
84 return SUCCESS;
85 }
86
EmitChar(uint8_t c)87 int DynChunk::EmitChar(uint8_t c)
88 {
89 return Emit(&c, 1);
90 }
91
EmitSelf(size_t offset,size_t length)92 int DynChunk::EmitSelf(size_t offset, size_t length)
93 {
94 if (UNLIKELY((size_ + length) > allocatedSize_)) {
95 if (Expand(size_ + length) != 0) {
96 return FAILURE;
97 }
98 }
99
100 if (memcpy_s(buf_ + size_, // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
101 length,
102 buf_ + offset, // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
103 length) != EOK) {
104 return FAILURE;
105 }
106 size_ += length;
107 return SUCCESS;
108 }
109
EmitStr(const char * str)110 int DynChunk::EmitStr(const char *str)
111 {
112 return Emit(reinterpret_cast<const uint8_t *>(str), strlen(str) + 1);
113 }
114 } // namespace panda::ecmascript
115