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 class Barriers { 27 public: 28 template<class T> SetPrimitive(void * obj,size_t offset,T value)29 static inline void SetPrimitive(void *obj, size_t offset, T value) 30 { 31 auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset); 32 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 33 *addr = value; 34 } 35 36 template<class T> AtomicSetPrimitive(volatile void * obj,size_t offset,T oldValue,T value)37 static inline bool AtomicSetPrimitive(volatile void *obj, size_t offset, T oldValue, T value) 38 { 39 volatile auto atomicField = reinterpret_cast<volatile std::atomic<T> *>(ToUintPtr(obj) + offset); 40 return std::atomic_compare_exchange_strong_explicit(atomicField, &oldValue, value, std::memory_order_release, 41 std::memory_order_relaxed); 42 } 43 44 template<bool need_write_barrier = true> 45 static void SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value); 46 47 static void SynchronizedSetClass(void *obj, JSTaggedType value); 48 49 template<class T> GetValue(const void * obj,size_t offset)50 static inline T GetValue(const void *obj, size_t offset) 51 { 52 auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset); 53 return *addr; 54 } 55 56 static void PUBLIC_API Update(uintptr_t slotAddr, Region *objectRegion, TaggedObject *value, Region *valueRegion); 57 }; 58 } // namespace panda::ecmascript 59 60 #endif // ECMASCRIPT_MEM_BARRIERS_H 61