1 /**
2 * Copyright (c) 2021-2024 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 #include "plugins/ets/runtime/types/ets_object.h"
17 #include "plugins/ets/runtime/ets_coroutine.h"
18 #include "runtime/handle_base-inl.h"
19
20 namespace ark::ets {
21
22 /* static */
Create(EtsCoroutine * etsCoroutine,EtsClass * klass)23 EtsObject *EtsObject::Create(EtsCoroutine *etsCoroutine, EtsClass *klass)
24 {
25 ASSERT_HAVE_ACCESS_TO_MANAGED_OBJECTS();
26 return static_cast<EtsObject *>(ObjectHeader::Create(etsCoroutine, klass->GetRuntimeClass()));
27 }
28
29 /* static */
Create(EtsClass * klass)30 EtsObject *EtsObject::Create(EtsClass *klass)
31 {
32 return Create(EtsCoroutine::GetCurrent(), klass);
33 }
34
35 /* static */
CreateNonMovable(EtsClass * klass)36 EtsObject *EtsObject::CreateNonMovable(EtsClass *klass)
37 {
38 return static_cast<EtsObject *>(ObjectHeader::CreateNonMovable(klass->GetRuntimeClass()));
39 }
40
GetHashCode()41 uint32_t EtsObject::GetHashCode()
42 {
43 while (true) {
44 MarkWord currentMarkWord = AtomicGetMark();
45 switch (currentMarkWord.GetState()) {
46 case MarkWord::STATE_HASHED: {
47 return currentMarkWord.GetHash();
48 }
49 case MarkWord::STATE_UNLOCKED: {
50 auto hashMark = currentMarkWord.DecodeFromHash(GenerateHashCode());
51 ASSERT(hashMark.GetState() == MarkWord::STATE_HASHED);
52 AtomicSetMark(currentMarkWord, hashMark);
53 break;
54 }
55 default: {
56 LOG(FATAL, RUNTIME) << "Error on GetHashCode(): invalid state " << currentMarkWord.GetState();
57 }
58 }
59 }
60 }
61
62 } // namespace ark::ets
63