]> wimlib.net Git - wimlib/blob - src/util.h
Refactor error functions; make re-entrant
[wimlib] / src / util.h
1 #ifndef _WIMLIB_UTIL_H
2 #define _WIMLIB_UTIL_H
3
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <inttypes.h>
8 #include <sys/types.h>
9 #include "config.h"
10
11 #ifdef __GNUC__
12 #       define WIMLIBAPI __attribute__((visibility("default")))
13 #       define ALWAYS_INLINE inline __attribute__((always_inline))
14 #       define PACKED __attribute__((packed))
15 #       define FORMAT(type, format_str, args_start) \
16                         __attribute__((format(type, format_str, args_start)))
17 #       if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
18 #               define COLD     __attribute__((cold))
19 #       else
20 #               define COLD
21 #       endif
22 #else
23 #       define WIMLIBAPI
24 #       define ALWAYS_INLINE inline
25 #       define FORMAT(type, format_str, args_start)
26 #       define COLD
27 #       define PACKED
28 #endif /* __GNUC__ */
29
30
31 #if 0
32 #ifdef WITH_FUSE
33 #define atomic_inc(ptr) \
34         __sync_fetch_and_add(ptr, 1)
35
36 #define atomic_dec(ptr) \
37         __sync_sub_and_fetch(ptr, 1)
38 #endif
39 #endif
40
41 #ifndef _NTFS_TYPES_H
42 typedef uint8_t  u8;
43 typedef uint16_t u16;
44 typedef uint32_t u32;
45 typedef uint64_t u64;
46 #endif
47
48 #ifndef min
49 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
50                                         (__a < __b) ? __a : __b; })
51 #endif
52
53 #ifndef max
54 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
55                                         (__a > __b) ? __a : __b; })
56 #endif
57
58 #ifndef swap
59 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
60 #endif
61
62 /**
63  * container_of - cast a member of a structure out to the containing structure
64  * @ptr:        the pointer to the member.
65  * @type:       the type of the container struct this is embedded in.
66  * @member:     the name of the member within the struct.
67  *
68  */
69 #ifndef container_of
70 #define container_of(ptr, type, member) ({                      \
71         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
72         (type *)( (char *)__mptr - offsetof(type,member) );})
73 #endif
74
75 #define DIV_ROUND_UP(numerator, denominator) \
76         (((numerator) + (denominator) - 1) / (denominator))
77
78 #define MODULO_NONZERO(numerator, denominator) \
79         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
80
81 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
82
83 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
84
85 /* Used for buffering FILE IO in a few places */
86 #define BUFFER_SIZE 4096
87
88 #ifdef ENABLE_ERROR_MESSAGES
89 extern void wimlib_error(const char *format, ...)
90                 FORMAT(printf, 1, 2) COLD;
91 extern void wimlib_error_with_errno(const char *format, ...)
92                 FORMAT(printf, 1, 2) COLD;
93 extern void wimlib_warning(const char *format, ...)
94                 FORMAT(printf, 1, 2) COLD;
95 extern void wimlib_warning_with_errno(const char *format, ...)
96                 FORMAT(printf, 1, 2) COLD;
97 #       define ERROR                    wimlib_error
98 #       define ERROR_WITH_ERRNO         wimlib_error_with_errno
99 #       define WARNING                  wimlib_warning
100 #       define WARNING_WITH_ERRNO       wimlib_warning
101 #else
102 static inline FORMAT(printf, 1, 2) void
103 dummy_printf(const char *format, ...) { }
104 #       define ERROR(format, ...)               dummy_printf
105 #       define ERROR_WITH_ERRNO(format, ...)    dummy_printf
106 #       define WARNING(format, ...)             dummy_printf
107 #       define WARNING_WITH_ERRNO(format, ...)  dummy_printf
108 #endif /* ENABLE_ERROR_MESSAGES */
109
110 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
111 #       include <errno.h>
112 #       define DEBUG(format, ...)                                       \
113         ({                                                              \
114                 int __errno_save = errno;                               \
115                 fprintf(stdout, "[%s %d] %s(): " format,                \
116                         __FILE__, __LINE__, __func__, ## __VA_ARGS__);  \
117                 putchar('\n');                                          \
118                 fflush(stdout);                                         \
119                 errno = __errno_save;                                   \
120         })
121
122 #else
123 #       define DEBUG(format, ...)
124 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
125
126 #ifdef ENABLE_MORE_DEBUG
127 #       define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
128 #else
129 #       define DEBUG2(format, ...)
130 #endif /* ENABLE_DEBUG */
131
132 #ifdef ENABLE_ASSERTIONS
133 #include <assert.h>
134 #       define wimlib_assert(expr) assert(expr)
135 #else
136 #       define wimlib_assert(expr)
137 #endif
138
139 #ifdef ENABLE_MORE_ASSERTIONS
140 #define wimlib_assert2(expr) wimlib_assert(expr)
141 #else
142 #define wimlib_assert2(expr)
143 #endif
144
145 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
146
147 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
148 extern void *(*wimlib_malloc_func)(size_t);
149 extern void (*wimlib_free_func)(void *);
150 extern void *(*wimlib_realloc_func)(void *, size_t);
151 extern void *wimlib_calloc(size_t nmemb, size_t size);
152 extern char *wimlib_strdup(const char *str);
153 #       define  MALLOC  wimlib_malloc_func
154 #       define  FREE    wimlib_free_func
155 #       define  REALLOC wimlib_realloc_func
156 #       define  CALLOC  wimlib_calloc
157 #       define  STRDUP  wimlib_strdup
158 #else
159 #       include <stdlib.h>
160 #       include <string.h>
161 #       define  MALLOC  malloc
162 #       define  FREE    free
163 #       define  REALLOC realloc
164 #       define  CALLOC  calloc
165 #       define  STRDUP  strdup
166 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
167
168
169 /* encoding.c */
170
171 #ifdef WITH_NTFS_3G
172 static inline int iconv_global_init()
173 {
174         return 0;
175 }
176
177 static inline void iconv_global_cleanup() { }
178 #else
179 extern int iconv_global_init();
180 extern void iconv_global_cleanup();
181 #endif
182
183 extern int utf16_to_utf8(const char *utf16_str, size_t utf16_nbytes,
184                          char **utf8_str_ret, size_t *utf8_nbytes_ret);
185
186 extern int utf8_to_utf16(const char *utf8_str, size_t utf8_nbytes,
187                          char **utf16_str_ret, size_t *utf16_nbytes_ret);
188
189 /* util.c */
190 extern void randomize_byte_array(u8 *p, size_t n);
191
192 extern void randomize_char_array_with_alnum(char p[], size_t n);
193
194 extern const char *path_next_part(const char *path,
195                                   size_t *first_part_len_ret);
196
197 extern const char *path_basename(const char *path);
198
199 extern const char *path_stream_name(const char *path);
200
201 extern void to_parent_name(char buf[], size_t len);
202
203 extern void print_string(const void *string, size_t len);
204
205 extern int get_num_path_components(const char *path);
206
207 static inline void print_byte_field(const u8 field[], size_t len)
208 {
209         while (len--)
210                 printf("%02hhx", *field++);
211 }
212
213 static inline u32 bsr32(u32 n)
214 {
215 #if defined(__x86__) || defined(__x86_64__)
216         asm("bsrl %0, %0;"
217                         : "=r"(n)
218                         : "0" (n));
219         return n;
220 #else
221         u32 pow = 0;
222         while ((n >>= 1) != 0)
223                 pow++;
224         return pow;
225 #endif
226 }
227
228 #endif /* _WIMLIB_UTIL_H */