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