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 "sandbox_memory_manager.h"
16
17 #include "sandbox_manager_log.h"
18
19 namespace OHOS {
20 namespace AccessControl {
21 namespace SandboxManager {
22 namespace {
23 using namespace OHOS::HiviewDFX;
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
25 LOG_CORE, ACCESSCONTROL_DOMAIN_SANDBOXMANAGER, "SandboxManagerMemmgr"
26 };
27 constexpr int32_t MAX_RUNNING_NUM = 256;
28 std::recursive_mutex g_instanceMutex;
29 }
30
GetInstance()31 SandboxMemoryManager& SandboxMemoryManager::GetInstance()
32 {
33 static SandboxMemoryManager* instance = nullptr;
34 if (instance == nullptr) {
35 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
36 if (instance == nullptr) {
37 instance = new SandboxMemoryManager();
38 }
39 }
40 return *instance;
41 }
42
AddFunctionRuningNum()43 void SandboxMemoryManager::AddFunctionRuningNum()
44 {
45 std::lock_guard<std::mutex> lock(callNumberMutex_);
46 callFuncRunningNum_++;
47 if (callFuncRunningNum_ > MAX_RUNNING_NUM) {
48 SANDBOXMANAGER_LOG_WARN(LABEL,
49 "The num of working function (%{public}d) over %{public}d.", callFuncRunningNum_, MAX_RUNNING_NUM);
50 }
51 }
52
DecreaseFunctionRuningNum()53 void SandboxMemoryManager::DecreaseFunctionRuningNum()
54 {
55 std::lock_guard<std::mutex> lock(callNumberMutex_);
56 callFuncRunningNum_--;
57 }
58
IsAllowedUnloadService()59 bool SandboxMemoryManager::IsAllowedUnloadService()
60 {
61 std::lock_guard<std::mutex> lock(callNumberMutex_);
62 if (callFuncRunningNum_ == 0) {
63 return true;
64 }
65 SANDBOXMANAGER_LOG_WARN(LABEL,
66 "Not allowed to unload service, callFuncRunningNum_ is %{public}d.", callFuncRunningNum_);
67 return false;
68 }
69
SetIsDelayedToUnload(bool isUnload)70 void SandboxMemoryManager::SetIsDelayedToUnload(bool isUnload)
71 {
72 std::lock_guard<std::mutex> lock(isDelayedMutex_);
73 isDelayedToUnload_ = isUnload;
74 }
75
IsDelayedToUnload()76 bool SandboxMemoryManager::IsDelayedToUnload()
77 {
78 std::lock_guard<std::mutex> lock(isDelayedMutex_);
79 return isDelayedToUnload_;
80 }
81
82 } // namespace SandboxManager
83 } // namespace AccessControl
84 } // namespace OHOS
85