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 #ifndef RESSCHED_COMMON_INCLUDE_RES_COMMON_UTIL_H
17 #define RESSCHED_COMMON_INCLUDE_RES_COMMON_UTIL_H
18 
19 #include <string>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <limits>
23 
24 namespace OHOS {
25 namespace ResourceSchedule {
26 namespace {
27     constexpr uint32_t MIN_BUNDLE_NAME_LEN = 7;
28     constexpr uint32_t MAX_BUNDLE_NAME_LEN = 127;
29 }
30 class ResCommonUtil {
31 public:
CheckBundleName(const std::string & bundleName)32     static bool CheckBundleName(const std::string &bundleName)
33     {
34         if (bundleName.empty()) {
35             return false;
36         }
37         if (bundleName.size() < MIN_BUNDLE_NAME_LEN || bundleName.size() > MAX_BUNDLE_NAME_LEN) {
38             return false;
39         }
40         if (!isalpha(bundleName.front())) {
41             return false;
42         }
43         for (const auto &ch : bundleName) {
44             if (!isalnum(ch) && ch != '_' && ch != '.') {
45                 return false;
46             }
47         }
48         return true;
49     }
50 
WriteFileReclaim(int32_t pid)51     static void WriteFileReclaim(int32_t pid)
52     {
53         std::string path = "/proc/" + std::to_string(pid) + "/reclaim";
54         std::string contentStr = "1";
55         int fd = open(path.c_str(), O_WRONLY);
56         if (fd < 0) {
57             return;
58         }
59         write(fd, contentStr.c_str(), contentStr.length());
60         close(fd);
61     }
62 
StrToFloat(const std::string & value,float & result)63     static bool StrToFloat(const std::string& value, float& result)
64     {
65         char* pEnd = nullptr;
66         errno = 0;
67         float res = std::strtof(value.c_str(), &pEnd);
68         if (errno == ERANGE || pEnd == value.c_str() || *pEnd != '\0' ||
69             (res < std::numeric_limits<float>::min()) ||
70             res > std::numeric_limits<float>::max()) {
71             return false;
72         }
73         result = res;
74         return true;
75     }
76 
GetNowMillTime()77     static int64_t GetNowMillTime()
78     {
79         auto now = std::chrono::steady_clock::now();
80         auto millSecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
81         return millSecs.count();
82     }
83 };
84 } // namespace ResourceSchedule
85 } // namespace OHOS
86 
87 #endif // RESSCHED_COMMON_INCLUDE_RES_COMMON_UTIL_H