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