1 /* 2 * Copyright (c) 2021 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 UTILS_BASE_PUBDEF_H 17 #define UTILS_BASE_PUBDEF_H 18 19 namespace OHOS { 20 21 #define B_TO_M(b) ((b) / 1024 / 1024) 22 #define M_TO_B(m) ((m) * 1024 * 1024) 23 #define B_TO_K(b) ((b) / 1024) 24 #define K_TO_B(m) ((m) * 1024) 25 26 #define HOUR_TO_SECOND(h) ((h) * 3600) 27 #define SECOND_TO_HOUR(s) ((s) / 3600) 28 29 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a[0]))) 30 31 #define FREE_AND_NIL(p) do { if (p) { delete (p); (p) = nullptr; } } while(0) 32 #define FREE_AND_NIL_ARRAY(p) do { if (p) { delete[] (p); (p) = nullptr; } } while(0) 33 34 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 35 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 36 37 #define EPS (1e-8) 38 #define EQUAL_TO_ZERO(x) (fabs(x) <= (EPS)) 39 40 // Used to retry syscalls that can return EINTR. 41 #ifndef TEMP_FAILURE_RETRY 42 #define TEMP_FAILURE_RETRY(exp) \ 43 ({ \ 44 decltype(exp) _rc; \ 45 do { \ 46 _rc = (exp); \ 47 } while ((_rc == -1) && (errno == EINTR)); \ 48 _rc; \ 49 }) 50 #endif 51 } 52 53 #endif 54 55