• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-2025 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 PLUGINS_ETS_RUNTIME_ETS_OBJECT_STATE_INFO_H
17 #define PLUGINS_ETS_RUNTIME_ETS_OBJECT_STATE_INFO_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <limits>
22 #include "libpandabase/macros.h"
23 #include "libpandabase/mem/mem.h"
24 
25 namespace ark::ets {
26 
27 class EtsObject;
28 class EtsCoroutine;
29 
30 class EtsObjectStateInfo {
31 public:
32     using Id = ObjectPointerType;
33     static constexpr uint32_t INVALID_INTEROP_INDEX = std::numeric_limits<uint32_t>::max();
34 
EtsObjectStateInfo(EtsObject * obj,Id id)35     EtsObjectStateInfo(EtsObject *obj, Id id) : obj_(obj), id_(id) {}
36     ~EtsObjectStateInfo() = default;
37 
38     NO_COPY_SEMANTIC(EtsObjectStateInfo);
39     NO_MOVE_SEMANTIC(EtsObjectStateInfo);
40 
GetEtsObject()41     EtsObject *GetEtsObject() const
42     {
43         return obj_;
44     }
45 
SetEtsObject(EtsObject * obj)46     void SetEtsObject(EtsObject *obj)
47     {
48         obj_ = obj;
49     }
50 
GetId()51     Id GetId() const
52     {
53         return id_;
54     }
55 
SetEtsHash(uint32_t hash)56     void SetEtsHash(uint32_t hash)
57     {
58         // Atomic with relaxed order reason: ordering constraints are not required
59         etsHash_.store(hash, std::memory_order_relaxed);
60     }
GetEtsHash()61     uint32_t GetEtsHash() const
62     {
63         // Atomic with relaxed order reason: ordering constraints are not required
64         return etsHash_.load(std::memory_order_relaxed);
65     }
66 
SetInteropIndex(uint32_t index)67     void SetInteropIndex(uint32_t index)
68     {
69         // Atomic with relaxed order reason: ordering constraints are not required
70         interopIndex_.store(index, std::memory_order_relaxed);
71     }
GetInteropIndex()72     uint32_t GetInteropIndex() const
73     {
74         // Atomic with relaxed order reason: ordering constraints are not required
75         return interopIndex_.load(std::memory_order_relaxed);
76     }
77 
78     bool DeflateInternal();
79 
80     static bool TryAddNewInfo(EtsObject *obj, uint32_t hash, uint32_t index);
81     static bool TryReadEtsHash(const EtsObject *obj, uint32_t *hash);
82     static bool TryReadInteropIndex(const EtsObject *obj, uint32_t *index);
83     static bool TryDropInteropIndex(EtsObject *obj);
84     static bool TryResetInteropIndex(EtsObject *obj, uint32_t index);
85     static bool TryCheckIfInteropIndexIsValid(const EtsObject *obj, bool *isValid);
86 
87 private:
88     std::atomic_uint32_t etsHash_ {0};
89     std::atomic_uint32_t interopIndex_ {0};
90     EtsObject *obj_ {nullptr};
91     Id id_ {0};
92 };
93 
94 }  // namespace ark::ets
95 
96 #endif  // PLUGINS_ETS_RUNTIME_ETS_OBJECT_STATE_INFO_H