14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_BARRIERS_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_BARRIERS_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/common.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
214514f5e3Sopenharmony_ci#include "ecmascript/mem/mark_word.h"
224514f5e3Sopenharmony_ci#include "ecmascript/mem/mem_common.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_ciclass Region;
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cienum class WriteBarrierType : size_t { NORMAL, DESERIALIZE };
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ciclass Barriers {
304514f5e3Sopenharmony_cipublic:
314514f5e3Sopenharmony_ci    template<class T>
324514f5e3Sopenharmony_ci    static inline void SetPrimitive(void *obj, size_t offset, T value)
334514f5e3Sopenharmony_ci    {
344514f5e3Sopenharmony_ci        auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset);
354514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
364514f5e3Sopenharmony_ci        *addr = value;
374514f5e3Sopenharmony_ci    }
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ci    template<class T>
404514f5e3Sopenharmony_ci    static inline T AtomicSetPrimitive(volatile void *obj, size_t offset, T oldValue, T value)
414514f5e3Sopenharmony_ci    {
424514f5e3Sopenharmony_ci        volatile auto atomicField = reinterpret_cast<volatile std::atomic<T> *>(ToUintPtr(obj) + offset);
434514f5e3Sopenharmony_ci        std::atomic_compare_exchange_strong_explicit(atomicField, &oldValue, value, std::memory_order_release,
444514f5e3Sopenharmony_ci                                                     std::memory_order_relaxed);
454514f5e3Sopenharmony_ci        return oldValue;
464514f5e3Sopenharmony_ci    }
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci    template<bool needWriteBarrier = true>
494514f5e3Sopenharmony_ci    static void SetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value);
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    template<bool needWriteBarrier, bool maybeOverlap>
524514f5e3Sopenharmony_ci    static void CopyObject(const JSThread *thread, JSTaggedValue* dst, JSTaggedValue* src, size_t count);
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    template<bool maybeOverlap>
554514f5e3Sopenharmony_ci    static void CopyObjectPrimitive(JSTaggedValue* dst, JSTaggedValue* src, size_t count);
564514f5e3Sopenharmony_ci    static void SynchronizedSetClass(const JSThread *thread, void *obj, JSTaggedType value);
574514f5e3Sopenharmony_ci    static void SynchronizedSetObject(const JSThread *thread, void *obj, size_t offset, JSTaggedType value,
584514f5e3Sopenharmony_ci                                      bool isPrimitive = false);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    template<class T>
614514f5e3Sopenharmony_ci    static inline T GetValue(const void *obj, size_t offset)
624514f5e3Sopenharmony_ci    {
634514f5e3Sopenharmony_ci        auto *addr = reinterpret_cast<T *>(ToUintPtr(obj) + offset);
644514f5e3Sopenharmony_ci        return *addr;
654514f5e3Sopenharmony_ci    }
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    static void PUBLIC_API Update(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion,
684514f5e3Sopenharmony_ci                                  TaggedObject *value, Region *valueRegion,
694514f5e3Sopenharmony_ci                                  WriteBarrierType writeType = WriteBarrierType::NORMAL);
704514f5e3Sopenharmony_ci    static void PUBLIC_API UpdateWithoutEden(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion,
714514f5e3Sopenharmony_ci                                             TaggedObject *value, Region *valueRegion,
724514f5e3Sopenharmony_ci                                             WriteBarrierType writeType = WriteBarrierType::NORMAL);
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    static void PUBLIC_API UpdateShared(const JSThread *thread, TaggedObject *value, Region *valueRegion);
754514f5e3Sopenharmony_ci};
764514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_BARRIERS_H
79