13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci
163f4cbf05Sopenharmony_ci /**
173f4cbf05Sopenharmony_ci * @file pubdef.h
183f4cbf05Sopenharmony_ci *
193f4cbf05Sopenharmony_ci * @brief The file contains macro definitions for some basic functions
203f4cbf05Sopenharmony_ci * in c_utils, for example, unit conversion and space freeup.
213f4cbf05Sopenharmony_ci */
223f4cbf05Sopenharmony_ci
233f4cbf05Sopenharmony_ci#ifndef UTILS_BASE_PUBDEF_H
243f4cbf05Sopenharmony_ci#define UTILS_BASE_PUBDEF_H
253f4cbf05Sopenharmony_ci
263f4cbf05Sopenharmony_cinamespace OHOS {
273f4cbf05Sopenharmony_ci
283f4cbf05Sopenharmony_ci// Used for unit conversion from byte to megabyte
293f4cbf05Sopenharmony_ci#define B_TO_M(b) ((b) / 1024 / 1024)
303f4cbf05Sopenharmony_ci// Used for unit conversion from megabyte to byte
313f4cbf05Sopenharmony_ci#define M_TO_B(m) ((m) * 1024 * 1024)
323f4cbf05Sopenharmony_ci// Used for unit conversion from byte to kilobyte
333f4cbf05Sopenharmony_ci#define B_TO_K(b) ((b) / 1024)
343f4cbf05Sopenharmony_ci// Used for unit conversion from kilobyte to byte
353f4cbf05Sopenharmony_ci#define K_TO_B(m) ((m) * 1024)
363f4cbf05Sopenharmony_ci
373f4cbf05Sopenharmony_ci// Used for unit conversion from hour to second
383f4cbf05Sopenharmony_ci#define HOUR_TO_SECOND(h) ((h) * 3600)
393f4cbf05Sopenharmony_ci// Used for unit conversion from second to hour
403f4cbf05Sopenharmony_ci#define SECOND_TO_HOUR(s) ((s) / 3600)
413f4cbf05Sopenharmony_ci
423f4cbf05Sopenharmony_ci// Used to return the array size
433f4cbf05Sopenharmony_ci#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a[0])))
443f4cbf05Sopenharmony_ci
453f4cbf05Sopenharmony_ci// Used to free the space pointed by pointer p
463f4cbf05Sopenharmony_ci#define FREE_AND_NIL(p) do {if (p) { delete (p); (p) = nullptr;}} while (0)
473f4cbf05Sopenharmony_ci// Used to free the array space pointed by pointer p
483f4cbf05Sopenharmony_ci#define FREE_AND_NIL_ARRAY(p) do {if (p) {delete[] (p); (p) = nullptr;}} while (0)
493f4cbf05Sopenharmony_ci
503f4cbf05Sopenharmony_ci// Used to return the maximum of two numbers
513f4cbf05Sopenharmony_ci#define MAX(x, y)  (((x) > (y)) ? (x) : (y))
523f4cbf05Sopenharmony_ci// Used to return the minimum of two numbers
533f4cbf05Sopenharmony_ci#define MIN(x, y)  (((x) < (y)) ? (x) : (y))
543f4cbf05Sopenharmony_ci
553f4cbf05Sopenharmony_ci// Used to determine whether float number 'x' is 0
563f4cbf05Sopenharmony_ci#define EPS (1e-8)
573f4cbf05Sopenharmony_ci#define EQUAL_TO_ZERO(x) (fabs(x) <= (EPS))
583f4cbf05Sopenharmony_ci
593f4cbf05Sopenharmony_ci// Used to attempt a retry when the syscall error code is EINTR
603f4cbf05Sopenharmony_ci#ifndef TEMP_FAILURE_RETRY
613f4cbf05Sopenharmony_ci#define TEMP_FAILURE_RETRY(exp)            \
623f4cbf05Sopenharmony_ci    ({                                     \
633f4cbf05Sopenharmony_ci    decltype(exp) _rc;                     \
643f4cbf05Sopenharmony_ci    do {                                   \
653f4cbf05Sopenharmony_ci        _rc = (exp);                       \
663f4cbf05Sopenharmony_ci    } while ((_rc == -1) && (errno == EINTR)); \
673f4cbf05Sopenharmony_ci    _rc;                                   \
683f4cbf05Sopenharmony_ci    })
693f4cbf05Sopenharmony_ci#endif
703f4cbf05Sopenharmony_ci}
713f4cbf05Sopenharmony_ci
723f4cbf05Sopenharmony_ci#endif
733f4cbf05Sopenharmony_ci
74