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 24 namespace panda { 25 namespace ecmascript { 26 class TaggedObject; 27 class JSHClass; 28 29 using MarkWordType = uint64_t; 30 31 class MarkWord { 32 public: 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 IsForwardingAddress(MarkWordType value)36 static bool IsForwardingAddress(MarkWordType value) 37 { 38 return (value & TAG_MARK_BIT) != 0; 39 } 40 ToForwardingAddress(MarkWordType value)41 static TaggedObject *ToForwardingAddress(MarkWordType value) 42 { 43 return reinterpret_cast<TaggedObject *>(value & (~TAG_MARK_BIT)); 44 } 45 MarkWord(TaggedObject * header)46 explicit MarkWord(TaggedObject *header) 47 { 48 value_ = reinterpret_cast<volatile std::atomic<MarkWordType> *>(header)->load(std::memory_order_acquire); 49 } 50 ~MarkWord() = default; 51 NO_COPY_SEMANTIC(MarkWord); 52 NO_MOVE_SEMANTIC(MarkWord); 53 IsForwardingAddress()54 bool IsForwardingAddress() 55 { 56 return (value_ & TAG_MARK_BIT) != 0; 57 } 58 ToForwardingAddress()59 TaggedObject *ToForwardingAddress() 60 { 61 return reinterpret_cast<TaggedObject *>(value_ & (~TAG_MARK_BIT)); 62 } 63 FromForwardingAddress(MarkWordType forwardAddress)64 static MarkWordType FromForwardingAddress(MarkWordType forwardAddress) 65 { 66 return forwardAddress | TAG_MARK_BIT; 67 } 68 GetValue()69 MarkWordType GetValue() const 70 { 71 return value_; 72 } 73 GetJSHClass()74 JSHClass *GetJSHClass() const 75 { 76 return reinterpret_cast<JSHClass *>(value_ & (~TAG_MARK_BIT)); 77 } 78 79 private: 80 MarkWordType value_ {0}; 81 }; 82 } // namespace ecmascript 83 } // namespace panda 84 85 #endif // ECMASCRIPT_MEM_MARK_WORD_H 86