• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mem/region-inl.h"
23 #include "ecmascript/mem/heap.h"
24 #include "ecmascript/ecma_vm.h"
25 
26 namespace panda::ecmascript {
27 template<WriteBarrierType writeType = WriteBarrierType::NORMAL>
WriteBarrier(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)28 static ARK_INLINE void WriteBarrier(const JSThread *thread, void *obj, size_t offset, JSTaggedType value)
29 {
30     ASSERT(value != JSTaggedValue::VALUE_UNDEFINED);
31     Region *objectRegion = Region::ObjectAddressToRange(static_cast<TaggedObject *>(obj));
32     Region *valueRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(value));
33 #if ECMASCRIPT_ENABLE_BARRIER_CHECK
34     if (!thread->GetEcmaVM()->GetHeap()->IsAlive(JSTaggedValue(value).GetHeapObject())) {
35         LOG_FULL(FATAL) << "WriteBarrier checked value:" << value << " is invalid!";
36     }
37 #endif
38     uintptr_t slotAddr = ToUintPtr(obj) + offset;
39     if (!objectRegion->InYoungSpace() && valueRegion->InYoungSpace()) {
40         // Should align with '8' in 64 and 32 bit platform
41         ASSERT((slotAddr % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
42         objectRegion->InsertOldToNewRSet(slotAddr);
43     }
44 
45     if (thread->IsConcurrentMarkingOrFinished()) {
46         Barriers::Update(thread, slotAddr, objectRegion, reinterpret_cast<TaggedObject *>(value),
47                          valueRegion, writeType);
48     }
49 }
50 
51 /* static */
52 // CODECHECK-NOLINTNEXTLINE(C_RULE_ID_COMMENT_LOCATION)
53 // default value for need_write_barrier is true
54 template<bool need_write_barrier>
SetObject(const JSThread * thread,void * obj,size_t offset,JSTaggedType value)55 inline void Barriers::SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value)
56 {
57     // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
58     *reinterpret_cast<JSTaggedType *>(reinterpret_cast<uintptr_t>(obj) + offset) = value;
59     WriteBarrier(thread, obj, offset, value);
60 }
61 
SynchronizedSetClass(const JSThread * thread,void * obj,JSTaggedType value)62 inline void Barriers::SynchronizedSetClass(const JSThread *thread, void *obj, JSTaggedType value)
63 {
64     reinterpret_cast<volatile std::atomic<JSTaggedType> *>(obj)->store(value, std::memory_order_release);
65     WriteBarrier(thread, obj, 0, value);
66 }
67 
SynchronizedSetObject(const JSThread * thread,void * obj,size_t offset,JSTaggedType value,bool isPrimitive)68 inline void Barriers::SynchronizedSetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value,
69                                             bool isPrimitive)
70 {
71     reinterpret_cast<volatile std::atomic<JSTaggedType> *>(ToUintPtr(obj) + offset)
72     ->store(value, std::memory_order_release);
73     if (!isPrimitive) {
74         WriteBarrier(thread, obj, offset, value);
75     }
76 }
77 }  // namespace panda::ecmascript
78 
79 #endif  // ECMASCRIPT_MEM_BARRIERS_INL_H
80