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_MARK_WORD_H
17#define ECMASCRIPT_MEM_MARK_WORD_H
18
19#include <cstdint>
20#include <atomic>
21
22#include "libpandabase/utils/bit_field.h"
23
24namespace panda {
25namespace ecmascript {
26class TaggedObject;
27class JSHClass;
28
29using MarkWordType = uint64_t;
30
31class MarkWord {
32public:
33    // ForwardingAddress mark, this object has been moved and the address points to the newly allocated space.
34    static constexpr MarkWordType TAG_MARK_BIT = 0x02ULL;
35
36    explicit MarkWord(TaggedObject *header)
37    {
38        value_ = reinterpret_cast<volatile std::atomic<MarkWordType> *>(header)->load(std::memory_order_acquire);
39    }
40    ~MarkWord() = default;
41    NO_COPY_SEMANTIC(MarkWord);
42    NO_MOVE_SEMANTIC(MarkWord);
43
44    bool IsForwardingAddress()
45    {
46        return (value_ & TAG_MARK_BIT) != 0;
47    }
48
49    TaggedObject *ToForwardingAddress()
50    {
51        return reinterpret_cast<TaggedObject *>(value_ & (~TAG_MARK_BIT));
52    }
53
54    static MarkWordType FromForwardingAddress(MarkWordType forwardAddress)
55    {
56        return forwardAddress | TAG_MARK_BIT;
57    }
58
59    MarkWordType GetValue() const
60    {
61        return value_;
62    }
63
64    JSHClass *GetJSHClass() const
65    {
66        return reinterpret_cast<JSHClass *>(value_ & (~TAG_MARK_BIT));
67    }
68
69private:
70    MarkWordType value_ {0};
71};
72}  // namespace ecmascript
73}  // namespace panda
74
75#endif  // ECMASCRIPT_MEM_MARK_WORD_H
76