| 1 | #ifndef _BABELTRACE_INTERNAL_H |
| 2 | #define _BABELTRACE_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | #include <stdio.h> |
| 26 | #include <glib.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <errno.h> |
| 31 | #include "compat/string.h" |
| 32 | #include <babeltrace2/types.h> |
| 33 | |
| 34 | #define PERROR_BUFLEN 200 |
| 35 | |
| 36 | #ifndef likely |
| 37 | # ifdef __GNUC__ |
| 38 | # define likely(x) __builtin_expect(!!(x), 1) |
| 39 | # else |
| 40 | # define likely(x) (!!(x)) |
| 41 | # endif |
| 42 | #endif |
| 43 | |
| 44 | #ifndef unlikely |
| 45 | # ifdef __GNUC__ |
| 46 | # define unlikely(x) __builtin_expect(!!(x), 0) |
| 47 | # else |
| 48 | # define unlikely(x) (!!(x)) |
| 49 | # endif |
| 50 | #endif |
| 51 | |
| 52 | #ifndef min |
| 53 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 54 | #endif |
| 55 | |
| 56 | #ifndef max |
| 57 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 58 | #endif |
| 59 | |
| 60 | #ifndef max_t |
| 61 | #define max_t(type, a, b) \ |
| 62 | ((type) (a) > (type) (b) ? (type) (a) : (type) (b)) |
| 63 | #endif |
| 64 | |
| 65 | static inline |
| 66 | bool bt_safe_to_mul_int64(int64_t a, int64_t b) |
| 67 | { |
| 68 | if (a == 0 || b == 0) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | return a < INT64_MAX / b; |
| 73 | } |
| 74 | |
| 75 | static inline |
| 76 | bool bt_safe_to_mul_uint64(uint64_t a, uint64_t b) |
| 77 | { |
| 78 | if (a == 0 || b == 0) { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return a < UINT64_MAX / b; |
| 83 | } |
| 84 | |
| 85 | static inline |
| 86 | bool bt_safe_to_add_int64(int64_t a, int64_t b) |
| 87 | { |
| 88 | return a <= INT64_MAX - b; |
| 89 | } |
| 90 | |
| 91 | static inline |
| 92 | bool bt_safe_to_add_uint64(uint64_t a, uint64_t b) |
| 93 | { |
| 94 | return a <= UINT64_MAX - b; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Memory allocation zeroed |
| 99 | */ |
| 100 | #define zmalloc(x) calloc(1, x) |
| 101 | |
| 102 | /* |
| 103 | * BT_HIDDEN: set the hidden attribute for internal functions |
| 104 | * On Windows, symbols are local unless explicitly exported, |
| 105 | * see https://gcc.gnu.org/wiki/Visibility |
| 106 | */ |
| 107 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 108 | #define BT_HIDDEN |
| 109 | #else |
| 110 | #define BT_HIDDEN __attribute__((visibility("hidden"))) |
| 111 | #endif |
| 112 | |
| 113 | #ifndef __STRINGIFY |
| 114 | #define __STRINGIFY(x) #x |
| 115 | #endif |
| 116 | |
| 117 | #define TOSTRING(x) __STRINGIFY(x) |
| 118 | |
| 119 | #define BT_UNUSED __attribute__((unused)) |
| 120 | |
| 121 | #endif |