• 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_TABLE_H
17 #define PLUGINS_ETS_RUNTIME_ETS_OBJECT_STATE_TABLE_H
18 
19 #include <atomic>
20 #include <cstdint>
21 
22 #include "libpandabase/os/mutex.h"
23 
24 #include "runtime/include/mem/panda_containers.h"
25 
26 #include "plugins/ets/runtime/ets_coroutine.h"
27 #include "plugins/ets/runtime/ets_mark_word.h"
28 #include "plugins/ets/runtime/ets_object_state_info.h"
29 
30 namespace ark::ets {
31 
32 class EtsObject;
33 
34 /// @brief This class is used to contain and interact with information about each EtsObject that is USE_INFO state.
35 class EtsObjectStateTable {
36     // NOTE(molotkovmikhail): this table can be implemented with lock-free guaranty to reduse overhead of lock usage.
37     NO_COPY_SEMANTIC(EtsObjectStateTable);
38     NO_MOVE_SEMANTIC(EtsObjectStateTable);
39 
40 public:
41     static constexpr EtsObjectStateInfo::Id MAX_TABLE_ID = EtsMarkWord::INFO_TABLE_POINTER_MAX_COUNT;
42 
43     explicit EtsObjectStateTable(mem::InternalAllocatorPtr allocator);
44     ~EtsObjectStateTable();
45 
46     EtsObjectStateInfo *CreateInfo(EtsObject *obj);
47     EtsObjectStateInfo *LookupInfo(EtsObjectStateInfo::Id id);
48     void FreeInfo(EtsObjectStateInfo::Id id);
49 
50     void EnumerateObjectStates(const std::function<void(EtsObjectStateInfo *)> &cb);
51 
52     void DeflateInfo();
53 
54 private:
55     mem::InternalAllocatorPtr allocator_;
56     os::memory::RWLock lock_;
57     PandaUnorderedMap<EtsObjectStateInfo::Id, EtsObjectStateInfo *> table_;
58     EtsObjectStateInfo::Id lastId_ {0};
59 };
60 
61 }  // namespace ark::ets
62 
63 #endif  // PLUGINS_ETS_RUNTIME_ETS_OBJECT_STATE_TABLE_H