• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "el5_memory_manager.h"
16 
17 #include "el5_filekey_manager_error.h"
18 #include "el5_filekey_manager_log.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 constexpr int32_t MAX_RUNNING_NUM = 256;
25 std::recursive_mutex g_instanceMutex;
26 }
27 
GetInstance()28 El5MemoryManager& El5MemoryManager::GetInstance()
29 {
30     static El5MemoryManager* instance = nullptr;
31     if (instance == nullptr) {
32         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
33         if (instance == nullptr) {
34             El5MemoryManager* tmp = new (std::nothrow) El5MemoryManager();
35             instance = std::move(tmp);
36         }
37     }
38     return *instance;
39 }
40 
AddFunctionRuningNum()41 void El5MemoryManager::AddFunctionRuningNum()
42 {
43     std::lock_guard<std::mutex> lock(callNumberMutex_);
44     callFuncRunningNum_++;
45     if (callFuncRunningNum_ > MAX_RUNNING_NUM) {
46         LOG_WARN("The num of working function (%{public}d) over %{public}d.", callFuncRunningNum_, MAX_RUNNING_NUM);
47     }
48 }
49 
DecreaseFunctionRuningNum()50 void El5MemoryManager::DecreaseFunctionRuningNum()
51 {
52     std::lock_guard<std::mutex> lock(callNumberMutex_);
53     callFuncRunningNum_--;
54 }
55 
IsFunctionFinished()56 bool El5MemoryManager::IsFunctionFinished()
57 {
58     std::lock_guard<std::mutex> lock(callNumberMutex_);
59     if (callFuncRunningNum_ == 0) {
60         return true;
61     }
62     LOG_WARN("Not allowed to unload service, callFuncRunningNum_ is %{public}d.", callFuncRunningNum_);
63     return false;
64 }
65 
IsAllowUnloadService()66 bool El5MemoryManager::IsAllowUnloadService()
67 {
68     std::lock_guard<std::mutex> lock(isAllowUnloadServiceMutex_);
69     return isAllowUnloadService_;
70 }
71 
SetIsAllowUnloadService(bool allow)72 void El5MemoryManager::SetIsAllowUnloadService(bool allow)
73 {
74     LOG_INFO("The allow flag is (%{public}d).", allow);
75     std::lock_guard<std::mutex> lock(isAllowUnloadServiceMutex_);
76     isAllowUnloadService_ = allow;
77 }
78 }  // namespace AccessToken
79 }  // namespace Security
80 }  // namespace OHOS
81