1 /*
2  * Copyright (c) 2021-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  /**
17  * @file pubdef.h
18  *
19  * @brief The file contains macro definitions for some basic functions
20  * in c_utils, for example, unit conversion and space freeup.
21  */
22 
23 #ifndef UTILS_BASE_PUBDEF_H
24 #define UTILS_BASE_PUBDEF_H
25 
26 namespace OHOS {
27 
28 // Used for unit conversion from byte to megabyte
29 #define B_TO_M(b) ((b) / 1024 / 1024)
30 // Used for unit conversion from megabyte to byte
31 #define M_TO_B(m) ((m) * 1024 * 1024)
32 // Used for unit conversion from byte to kilobyte
33 #define B_TO_K(b) ((b) / 1024)
34 // Used for unit conversion from kilobyte to byte
35 #define K_TO_B(m) ((m) * 1024)
36 
37 // Used for unit conversion from hour to second
38 #define HOUR_TO_SECOND(h) ((h) * 3600)
39 // Used for unit conversion from second to hour
40 #define SECOND_TO_HOUR(s) ((s) / 3600)
41 
42 // Used to return the array size
43 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a[0])))
44 
45 // Used to free the space pointed by pointer p
46 #define FREE_AND_NIL(p) do {if (p) { delete (p); (p) = nullptr;}} while (0)
47 // Used to free the array space pointed by pointer p
48 #define FREE_AND_NIL_ARRAY(p) do {if (p) {delete[] (p); (p) = nullptr;}} while (0)
49 
50 // Used to return the maximum of two numbers
51 #define MAX(x, y)  (((x) > (y)) ? (x) : (y))
52 // Used to return the minimum of two numbers
53 #define MIN(x, y)  (((x) < (y)) ? (x) : (y))
54 
55 // Used to determine whether float number 'x' is 0
56 #define EPS (1e-8)
57 #define EQUAL_TO_ZERO(x) (fabs(x) <= (EPS))
58 
59 // Used to attempt a retry when the syscall error code is EINTR
60 #ifndef TEMP_FAILURE_RETRY
61 #define TEMP_FAILURE_RETRY(exp)            \
62     ({                                     \
63     decltype(exp) _rc;                     \
64     do {                                   \
65         _rc = (exp);                       \
66     } while ((_rc == -1) && (errno == EINTR)); \
67     _rc;                                   \
68     })
69 #endif
70 }
71 
72 #endif
73 
74