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