• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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/shared_objects/js_sendable_arraybuffer.h"
17 #include "ecmascript/mem/barriers-inl.h"
18 #include "ecmascript/platform/os.h"
19 #include "ecmascript/js_tagged_value-inl.h"
20 
21 namespace panda::ecmascript {
CopyDataBlockBytes(JSTaggedValue toBlock,JSTaggedValue fromBlock,int32_t fromIndex,int32_t count)22 void JSSendableArrayBuffer::CopyDataBlockBytes(
23     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 JSSendableArrayBuffer::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     ASSERT(from != nullptr && to != nullptr);
35     // LCOV_EXCL_START
36     if (memcpy_s(to, count, from + fromIndex, count) != EOK) {  // NOLINT
37         LOG_FULL(FATAL) << "memcpy_s failed";
38         UNREACHABLE();
39     } // LCOV_EXCL_STOP
40 }
41 
Attach(JSThread * thread,uint32_t arrayBufferByteLength,JSTaggedValue arrayBufferData,bool transferWithNativeAreaAllocator)42 void JSSendableArrayBuffer::Attach(JSThread *thread, uint32_t arrayBufferByteLength,
43                                    JSTaggedValue arrayBufferData, bool transferWithNativeAreaAllocator)
44 {
45     ASSERT(arrayBufferData.IsJSNativePointer());
46     // only in transition, should the JSSendableArrayBuffer with NativeAreaAllocator increase mem usage
47     if (transferWithNativeAreaAllocator) {
48         LOG_FULL(DEBUG) << "attaching for transfer";
49         JSHandle<JSNativePointer> np(thread, arrayBufferData.GetTaggedObject());
50         NativeAreaAllocator *allocator = SharedHeap::GetInstance()->GetNativeAreaAllocator();
51         if (allocator == nullptr) {
52             LOG_ECMA(FATAL) << "JSSendableArrayBuffer::Attach:allocator is nullptr";
53         }
54         allocator->IncreaseNativeMemoryUsage(MallocUsableSize(np->GetExternalPointer()));
55         np->SetData(allocator);
56     }
57     SetArrayBufferByteLength(arrayBufferByteLength);
58     SetArrayBufferData(thread, arrayBufferData);
59 }
60 
Detach(JSThread * thread,bool transferWithNativeAreaAllocator)61 void JSSendableArrayBuffer::Detach(JSThread *thread, bool transferWithNativeAreaAllocator)
62 {
63     JSTaggedValue arrayBufferData = GetArrayBufferData();
64     // already detached.
65     if (arrayBufferData.IsNull()) {
66         return;
67     }
68 
69     // only in transition, should the JSSendableArrayBuffer with NativeAreaAllocator decrease mem usage
70     if (transferWithNativeAreaAllocator) {
71         LOG_FULL(DEBUG) << "detaching for transfer";
72         JSHandle<JSNativePointer> np(thread, arrayBufferData.GetTaggedObject());
73         NativeAreaAllocator *allocator = SharedHeap::GetInstance()->GetNativeAreaAllocator();
74         allocator->DecreaseNativeMemoryUsage(MallocUsableSize(np->GetExternalPointer()));
75         np->SetData(nullptr);
76     }
77     SetArrayBufferData(thread, JSTaggedValue::Null());
78     SetArrayBufferByteLength(0);
79 }
80 }  // namespace panda::ecmascript