• 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_H
17 #define ECMASCRIPT_MEM_BARRIERS_H
18 
19 #include "ecmascript/common.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/mem/mark_word.h"
22 #include "ecmascript/mem/mem_common.h"
23 
24 namespace panda::ecmascript {
25 class Region;
26 
27 enum class WriteBarrierType : size_t { NORMAL, DESERIALIZE, AOT_DESERIALIZE };
28 
29 class Barriers {
30 public:
31     template<class T>
SetPrimitive(void * obj,size_t offset,T value)32     static inline void SetPrimitive(void *obj, size_t offset, T value)
33     {
34         auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset);
35         // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
36         *addr = value;
37     }
38 
39     template<class T>
AtomicSetPrimitive(volatile void * obj,size_t offset,T oldValue,T value)40     static inline T AtomicSetPrimitive(volatile void *obj, size_t offset, T oldValue, T value)
41     {
42         volatile auto atomicField = reinterpret_cast<volatile std::atomic<T> *>(ToUintPtr(obj) + offset);
43         std::atomic_compare_exchange_strong_explicit(atomicField, &oldValue, value, std::memory_order_release,
44                                                      std::memory_order_relaxed);
45         return oldValue;
46     }
47 
48     template<bool needWriteBarrier = true>
49     static void SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value);
50 
51     // dstAddr/srcAddr is the address will be copied to/from.
52     // It can be a derived pointer point to the middle of an object.
53     //
54     // Note: dstObj is the object address for dstAddr, it must point to the head of an object.
55     template<bool needWriteBarrier, bool maybeOverlap>
56     static void CopyObject(const JSThread *thread, const TaggedObject *dstObj, JSTaggedValue *dstAddr,
57                            const JSTaggedValue *srcAddr, size_t count);
58 
59     // dstAddr/srcAddr is the address will be copied to/from.
60     // It can be a derived pointer point to the middle of an object.
61     //
62     // Note: dstObj is the object address for dstAddr, it must point to the head of an object.
63     template<bool maybeOverlap>
64     static void CopyObjectPrimitive(JSTaggedValue* dst, const JSTaggedValue* src, size_t count);
65     static void SynchronizedSetClass(const JSThread *thread, void *obj, JSTaggedType value);
66     static void SynchronizedSetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value,
67                                       bool isPrimitive = false);
68 
69     template<class T>
GetValue(const void * obj,size_t offset)70     static inline T GetValue(const void *obj, size_t offset)
71     {
72         auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset);
73         return *addr;
74     }
75 
76     static void PUBLIC_API Update(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion,
77                                   TaggedObject *value, Region *valueRegion,
78                                   WriteBarrierType writeType = WriteBarrierType::NORMAL);
79 
80     static void PUBLIC_API UpdateShared(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion,
81                                         TaggedObject *value, Region *valueRegion);
82 };
83 }  // namespace panda::ecmascript
84 
85 #endif  // ECMASCRIPT_MEM_BARRIERS_H
86