1 /* 2 * Copyright (c) 2022-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 #ifndef __HVB_UTIL_H_ 16 #define __HVB_UTIL_H_ 17 18 #include <stddef.h> 19 #include "hvb_ops.h" 20 #include "hvb.h" 21 #include "hvb_sysdeps.h" 22 23 #ifdef __cplusplus 24 extern "C" 25 { 26 #endif 27 28 #define MAX_STRING_LEN 17 29 #define HVB_MAX_DIGITS_UINT64 32 30 31 #ifndef NULL 32 #define NULL 0 33 #endif 34 35 #ifdef offsetof 36 #define hvb_offsetof(type, member) offsetof(type, member) 37 #else 38 #define hvb_offsetof(type, member) ((size_t)&(((type *)0)->member)) 39 #endif 40 41 #define hvb_return_hvb_err_if_null(__ptr) \ 42 do { \ 43 if ((__ptr) == NULL) { \ 44 hvb_printv("error, %s is NULL\n", #__ptr, NULL); \ 45 return HVB_ERROR_INVALID_ARGUMENT; \ 46 } \ 47 } while (0) 48 49 uint64_t hvb_be64toh(uint64_t data); 50 uint64_t hvb_htobe64(uint64_t data); 51 52 char *hvb_strdup(const char *str); 53 void *hvb_malloc(uint64_t size); 54 void *hvb_calloc(uint64_t size); 55 56 uint64_t hvb_uint64_to_base10(uint64_t value, char digits[HVB_MAX_DIGITS_UINT64]); 57 char *hvb_bin2hex(const uint8_t *value, size_t len); 58 59 enum hvb_errno check_hvb_ops(struct hvb_ops *ops); 60 61 #ifdef __cplusplus 62 } 63 #endif 64 65 #endif