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 #ifndef ECMASCRIPT_MEM_BARRIERS_INL_H
17 #define ECMASCRIPT_MEM_BARRIERS_INL_H
18
19 #include "ecmascript/base/config.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/mem/barriers.h"
22 #include "ecmascript/runtime_api.h"
23
24 namespace panda::ecmascript {
WriteBarrier(void * obj,size_t offset,JSTaggedType value)25 static ARK_INLINE void WriteBarrier(void *obj, size_t offset, JSTaggedType value)
26 {
27 ASSERT(value != JSTaggedValue::VALUE_UNDEFINED);
28 Region *objectRegion = Region::ObjectAddressToRange(static_cast<TaggedObject *>(obj));
29 Region *valueRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(value));
30 uintptr_t slotAddr = ToUintPtr(obj) + offset;
31 if (!objectRegion->InYoungGeneration() && valueRegion->InYoungGeneration()) {
32 // Should align with '8' in 64 and 32 bit platform
33 ASSERT((slotAddr % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
34 objectRegion->InsertOldToNewRememberedSet(slotAddr);
35 }
36
37 if (valueRegion->IsMarking()) {
38 RuntimeApi::MarkObject(slotAddr, objectRegion, reinterpret_cast<TaggedObject *>(value), valueRegion);
39 }
40 }
41
42 /* static */
43 // CODECHECK-NOLINTNEXTLINE(C_RULE_ID_COMMENT_LOCATION)
44 // default value for need_write_barrier is true
45 template<bool need_write_barrier>
SetDynObject(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)46 inline void Barriers::SetDynObject([[maybe_unused]] const JSThread *thread, void *obj, size_t offset,
47 JSTaggedType value)
48 {
49 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
50 *reinterpret_cast<JSTaggedType *>(reinterpret_cast<uintptr_t>(obj) + offset) = value;
51 WriteBarrier(obj, offset, value);
52 }
53 } // namespace panda::ecmascript
54
55 #endif // ECMASCRIPT_MEM_BARRIERS_INL_H
56