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/mem/mark_word.h" 21 #include "ecmascript/mem/mem_common.h" 22 23 namespace panda::ecmascript { 24 class Region; 25 26 enum class WriteBarrierType : size_t { NORMAL, DESERIALIZE }; 27 28 class Barriers { 29 public: 30 template<class T> SetPrimitive(void * obj,size_t offset,T value)31 static inline void SetPrimitive(void *obj, size_t offset, T value) 32 { 33 auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset); 34 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 35 *addr = value; 36 } 37 38 template<class T> AtomicSetPrimitive(volatile void * obj,size_t offset,T oldValue,T value)39 static inline T AtomicSetPrimitive(volatile void *obj, size_t offset, T oldValue, T value) 40 { 41 volatile auto atomicField = reinterpret_cast<volatile std::atomic<T> *>(ToUintPtr(obj) + offset); 42 std::atomic_compare_exchange_strong_explicit(atomicField, &oldValue, value, std::memory_order_release, 43 std::memory_order_relaxed); 44 return oldValue; 45 } 46 47 template<bool need_write_barrier = true> 48 static void SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value); 49 50 static void SynchronizedSetClass(const JSThread *thread, void *obj, JSTaggedType value); 51 static void SynchronizedSetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value, 52 bool isPrimitive = false); 53 54 template<class T> GetValue(const void * obj,size_t offset)55 static inline T GetValue(const void *obj, size_t offset) 56 { 57 auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset); 58 return *addr; 59 } 60 61 static void PUBLIC_API Update(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion, 62 TaggedObject *value, Region *valueRegion, 63 WriteBarrierType writeType = WriteBarrierType::NORMAL); 64 // For work deserialize, push deserialize result to mark stack if thread IsConcurrentMarkingOrFinished 65 static void MarkAndPushForDeserialize(const JSThread *thread, TaggedObject *object); 66 }; 67 } // namespace panda::ecmascript 68 69 #endif // ECMASCRIPT_MEM_BARRIERS_H 70