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/js_arraybuffer.h"
17
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/mem/barriers-inl.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/platform/os.h"
24 #include "ecmascript/tagged_array.h"
25
26 #include "securec.h"
27
28 namespace panda::ecmascript {
CopyDataBlockBytes(JSTaggedValue toBlock,JSTaggedValue fromBlock,int32_t fromIndex,int32_t count)29 void JSArrayBuffer::CopyDataBlockBytes(JSTaggedValue toBlock, JSTaggedValue fromBlock, int32_t fromIndex, int32_t count)
30 {
31 void *fromBuf = JSNativePointer::Cast(fromBlock.GetTaggedObject())->GetExternalPointer();
32 void *toBuf = JSNativePointer::Cast(toBlock.GetTaggedObject())->GetExternalPointer();
33 CopyDataPointBytes(toBuf, fromBuf, fromIndex, count);
34 }
35
CopyDataPointBytes(void * toBuf,void * fromBuf,int32_t fromIndex,int32_t count)36 void JSArrayBuffer::CopyDataPointBytes(void *toBuf, void *fromBuf, int32_t fromIndex, int32_t count)
37 {
38 auto *from = static_cast<uint8_t *>(fromBuf);
39 auto *to = static_cast<uint8_t *>(toBuf);
40 if (memcpy_s(to, count, from + fromIndex, count) != EOK) { // NOLINT
41 LOG_FULL(FATAL) << "memcpy_s failed";
42 UNREACHABLE();
43 }
44 }
45
Attach(JSThread * thread,uint32_t arrayBufferByteLength,JSTaggedValue arrayBufferData,bool transferWithNativeAreaAllocator)46 void JSArrayBuffer::Attach(JSThread *thread, uint32_t arrayBufferByteLength,
47 JSTaggedValue arrayBufferData, bool transferWithNativeAreaAllocator)
48 {
49 ASSERT(arrayBufferData.IsJSNativePointer());
50 // only in transition, should the JSArrayBuffer with NativeAreaAllocator increase mem usage
51 if (transferWithNativeAreaAllocator) {
52 LOG_FULL(DEBUG) << "attaching for transfer";
53 JSHandle<JSNativePointer> np(thread, arrayBufferData.GetTaggedObject());
54 NativeAreaAllocator *allocator = thread->GetEcmaVM()->GetNativeAreaAllocator();
55 allocator->IncreaseNativeMemoryUsage(MallocUsableSize(np->GetExternalPointer()));
56 np->SetData(allocator);
57 }
58 SetArrayBufferByteLength(arrayBufferByteLength);
59 SetArrayBufferData(thread, arrayBufferData);
60 }
61
Detach(JSThread * thread,bool transferWithNativeAreaAllocator)62 void JSArrayBuffer::Detach(JSThread *thread, bool transferWithNativeAreaAllocator)
63 {
64 JSTaggedValue arrayBufferData = GetArrayBufferData();
65 // already detached.
66 if (arrayBufferData.IsNull()) {
67 return;
68 }
69
70 // only in transition, should the JSArrayBuffer with NativeAreaAllocator decrease mem usage
71 if (transferWithNativeAreaAllocator) {
72 LOG_FULL(DEBUG) << "detaching for transfer";
73 JSHandle<JSNativePointer> np(thread, arrayBufferData.GetTaggedObject());
74 NativeAreaAllocator *allocator = thread->GetEcmaVM()->GetNativeAreaAllocator();
75 allocator->DecreaseNativeMemoryUsage(MallocUsableSize(np->GetExternalPointer()));
76 np->SetData(nullptr);
77 }
78 SetArrayBufferData(thread, JSTaggedValue::Null());
79 SetArrayBufferByteLength(0);
80 }
81 } // namespace panda::ecmascript