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