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/mem/mark_word.h" 20 21 namespace panda::ecmascript { 22 class Barriers { 23 public: 24 template<class T> SetDynPrimitive(void * obj,size_t offset,T value)25 static inline void SetDynPrimitive(void *obj, size_t offset, T value) 26 { 27 auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset); 28 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference) 29 *addr = value; 30 } 31 32 template<class T> AtomicSetDynPrimitive(volatile void * obj,size_t offset,T oldValue,T value)33 static inline bool AtomicSetDynPrimitive(volatile void *obj, size_t offset, T oldValue, T value) 34 { 35 volatile auto atomicField = reinterpret_cast<volatile std::atomic<T> *>(ToUintPtr(obj) + offset); 36 return std::atomic_compare_exchange_strong_explicit(atomicField, &oldValue, value, std::memory_order_release, 37 std::memory_order_relaxed); 38 } 39 40 template<bool need_write_barrier = true> 41 static inline void SetDynObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value); 42 43 template<class T> GetDynValue(const void * obj,size_t offset)44 static inline T GetDynValue(const void *obj, size_t offset) 45 { 46 auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset); 47 return *addr; 48 } 49 }; 50 } // namespace panda::ecmascript 51 52 #endif // ECMASCRIPT_MEM_BARRIERS_H 53