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