• 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 #include "plugins/ets/runtime/ets_object_state_table.h"
17 #include "libpandabase/macros.h"
18 #include "libpandabase/os/mutex.h"
19 
20 namespace ark::ets {
21 
EtsObjectStateTable(mem::InternalAllocatorPtr allocator)22 EtsObjectStateTable::EtsObjectStateTable(mem::InternalAllocatorPtr allocator)
23     : allocator_(allocator), table_(allocator->Adapter())
24 {
25 }
26 
~EtsObjectStateTable()27 EtsObjectStateTable::~EtsObjectStateTable()
28 {
29     for ([[maybe_unused]] auto &[id, info] : table_) {
30         if (info != nullptr) {
31             allocator_->Delete(info);
32         }
33     }
34 }
35 
CreateInfo(EtsObject * obj)36 EtsObjectStateInfo *EtsObjectStateTable::CreateInfo(EtsObject *obj)
37 {
38     os::memory::WriteLockHolder wlh(lock_);
39     for (EtsObjectStateInfo::Id i = 0; i < MAX_TABLE_ID; ++i) {
40         lastId_ = (lastId_ + 1) % MAX_TABLE_ID;
41         if (table_.count(lastId_) == 0) {
42             auto *info = allocator_->New<EtsObjectStateInfo>(obj, lastId_);
43             if (info == nullptr) {
44                 return nullptr;
45             }
46             table_[lastId_] = info;
47             return info;
48         }
49     }
50     return nullptr;
51 }
52 
LookupInfo(EtsObjectStateInfo::Id id)53 EtsObjectStateInfo *EtsObjectStateTable::LookupInfo(EtsObjectStateInfo::Id id)
54 {
55     os::memory::ReadLockHolder rlh(lock_);
56     auto it = table_.find(id);
57     if (it != table_.end()) {
58         return it->second;
59     }
60     return nullptr;
61 }
62 
EnumerateObjectStates(const std::function<void (EtsObjectStateInfo *)> & cb)63 void EtsObjectStateTable::EnumerateObjectStates(const std::function<void(EtsObjectStateInfo *)> &cb)
64 {
65     os::memory::WriteLockHolder wlh(lock_);
66     for (auto &objectState : table_) {
67         cb(objectState.second);
68     }
69 }
70 
FreeInfo(EtsObjectStateInfo::Id id)71 void EtsObjectStateTable::FreeInfo(EtsObjectStateInfo::Id id)
72 {
73     os::memory::WriteLockHolder wlh(lock_);
74     auto it = table_.find(id);
75     if (it != table_.end()) {
76         auto *info = it->second;
77         table_.erase(id);
78         allocator_->Delete(info);
79     }
80 }
81 
DeflateInfo()82 void EtsObjectStateTable::DeflateInfo()
83 {
84     os::memory::WriteLockHolder wlh(lock_);
85     for (auto infoI = table_.begin(); infoI != table_.end();) {
86         auto info = infoI->second;
87         if (info->DeflateInternal()) {
88             infoI = table_.erase(infoI);
89             allocator_->Delete(info);
90         } else {
91             infoI++;
92         }
93     }
94 }
95 
96 }  // namespace ark::ets
97