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 UTIL_H
17 #define UTIL_H
18
19 #include <cinttypes>
20
21 namespace OHOS {
22 namespace Rosen {
23
24 int64_t GetSysClockTime();
25
26 int64_t GetMillisTime();
27
28 template<typename T>
AddInt(T op1,T op2,T minVal,T maxVal,T & res)29 bool AddInt(T op1, T op2, T minVal, T maxVal, T& res)
30 {
31 if (op1 >= 0) {
32 if (op2 > maxVal - op1) {
33 return false;
34 }
35 } else {
36 if (op2 < minVal - op1) {
37 return false;
38 }
39 }
40 res = op1 + op2;
41 return true;
42 }
43
AddInt32(int32_t op1,int32_t op2,int32_t & res)44 inline bool AddInt32(int32_t op1, int32_t op2, int32_t& res)
45 {
46 return AddInt(op1, op2, INT32_MIN, INT32_MAX, res);
47 }
48
AddInt64(int64_t op1,int64_t op2,int64_t & res)49 inline bool AddInt64(int64_t op1, int64_t op2, int64_t& res)
50 {
51 return AddInt(op1, op2, INT64_MIN, INT64_MAX, res);
52 }
53
54 } // namespace Rosen
55 } // namespace OHOS
56 #endif // UTIL_H