1 /*
2  * Copyright (c) 2023 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 "entity.h"
17 #include "version_ctx.h"
18 #include "util/slab.h"
19 
20 namespace ffrt {
VA2Ctx(const void * p,SCPUEUTask * task)21 VersionCtx* Entity::VA2Ctx(const void* p, SCPUEUTask* task __attribute__((unused)))
22 {
23     auto it = std::as_const(vaMap).find(p);
24     if (it != vaMap.end()) {
25         return it->second;
26     }
27     auto version = new (SimpleAllocator<VersionCtx>::AllocMem()) VersionCtx(p, nullptr, nullptr);
28     vaMap[p] = version;
29     return version;
30 }
31 
RecycleVersion()32 void Entity::RecycleVersion()
33 {
34     for (auto it = Entity::Instance()->versionTrashcan.cbegin(); it != Entity::Instance()->versionTrashcan.cend();) {
35         VersionCtx* cur = *it;
36         VersionCtx* next = cur->next;
37         // VersionCtx list delete
38         next->last = cur->last;
39         if (cur->last != nullptr) {
40             cur->last->next = next;
41         }
42         SimpleAllocator<VersionCtx>::FreeMem(cur);
43         if (next->next == nullptr) {
44             // Delete root version
45             auto data = std::as_const(Entity::Instance()->vaMap).find(next->signature);
46             if (data != Entity::Instance()->vaMap.end()) {
47                 Entity::Instance()->vaMap.erase(data);
48             }
49             SimpleAllocator<VersionCtx>::FreeMem(next);
50         }
51         Entity::Instance()->versionTrashcan.erase(it++);
52     }
53 }
54 } /* namespace ffrt */