Commit | Line | Data |
---|---|---|
70bd0a12 JD |
1 | #ifndef _BABELTRACE_INTERNAL_H |
2 | #define _BABELTRACE_INTERNAL_H | |
3 | ||
eb31c5e6 | 4 | /* |
eb31c5e6 MD |
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. | |
c462e188 MD |
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. | |
eb31c5e6 | 24 | */ |
70bd0a12 JD |
25 | #include <stdio.h> |
26 | #include <glib.h> | |
11ac6674 | 27 | #include <stdint.h> |
f3e4505b | 28 | #include <stdlib.h> |
5b9e151d | 29 | #include <stdbool.h> |
f3e4505b | 30 | #include <errno.h> |
3d9990ac | 31 | #include <babeltrace/compat/string-internal.h> |
c55a9f58 | 32 | #include <babeltrace/types.h> |
fd3c708f MD |
33 | |
34 | #define PERROR_BUFLEN 200 | |
70bd0a12 | 35 | |
196409fe EB |
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 | |
90bf3cef | 51 | |
40af9f9f JG |
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 | ||
f3e4505b JG |
60 | #ifndef max_t |
61 | #define max_t(type, a, b) \ | |
62 | ((type) (a) > (type) (b) ? (type) (a) : (type) (b)) | |
63 | #endif | |
64 | ||
5b9e151d PP |
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 | ||
f3e4505b JG |
97 | /* |
98 | * Memory allocation zeroed | |
99 | */ | |
100 | #define zmalloc(x) calloc(1, x) | |
101 | ||
5d6c80a5 JD |
102 | /* |
103 | * BT_HIDDEN: set the hidden attribute for internal functions | |
675860f4 MJ |
104 | * On Windows, symbols are local unless explicitly exported, |
105 | * see https://gcc.gnu.org/wiki/Visibility | |
5d6c80a5 | 106 | */ |
675860f4 MJ |
107 | #if defined(_WIN32) || defined(__CYGWIN__) |
108 | #define BT_HIDDEN | |
109 | #else | |
5d6c80a5 | 110 | #define BT_HIDDEN __attribute__((visibility("hidden"))) |
675860f4 | 111 | #endif |
5d6c80a5 | 112 | |
20ccd9e1 | 113 | #ifndef __STRINGIFY |
6fbd4105 | 114 | #define __STRINGIFY(x) #x |
20ccd9e1 MJ |
115 | #endif |
116 | ||
6fbd4105 PP |
117 | #define TOSTRING(x) __STRINGIFY(x) |
118 | ||
312c056a PP |
119 | #define BT_UNUSED __attribute__((unused)) |
120 | ||
70bd0a12 | 121 | #endif |