12 # if defined(__CYGWIN__) || defined(__WIN32__)
13 # define WIMLIBAPI __declspec(dllexport)
15 # define WIMLIBAPI __attribute__((visibility("default")))
17 # define ALWAYS_INLINE inline __attribute__((always_inline))
18 # define PACKED __attribute__((packed))
19 # define FORMAT(type, format_str, args_start) \
20 __attribute__((format(type, format_str, args_start)))
21 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
22 # define COLD __attribute__((cold))
28 # define ALWAYS_INLINE inline
29 # define FORMAT(type, format_str, args_start)
37 #define atomic_inc(ptr) \
38 __sync_fetch_and_add(ptr, 1)
40 #define atomic_dec(ptr) \
41 __sync_sub_and_fetch(ptr, 1)
52 /* A pointer to 'mbchar' indicates a string of "multibyte characters" provided
53 * in the default encoding of the user's locale, which may be "UTF-8",
54 * "ISO-8859-1", "C", or any other ASCII-compatible encoding.
55 * "ASCII-compatible" here means any encoding where all ASCII-representable
56 * characters have the same representation as in ASCII itself, and any non-ASCII
57 * character is represented as a sequence of one or more bytes not already used
58 * by any ASCII character. */
61 /* A pointer to 'utf8char' indicates a UTF-8 encoded string */
62 typedef char utf8char;
64 /* Note: in some places in the code, strings of plain old 'char' are still used.
65 * This means that the string is being operated on in an ASCII-compatible way,
66 * and may be either a multibyte or UTF-8 string. */
68 /* A pointer to 'utf16lechar' indicates a UTF-16LE encoded string */
69 typedef u16 utf16lechar;
72 utf16le_strlen(const utf16lechar *s);
76 iconv_global_cleanup();
78 extern bool wimlib_mbs_is_utf8;
80 #define DECLARE_CHAR_CONVERSION_FUNCTIONS(varname1, varname2, \
81 chartype1, chartype2) \
84 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
85 size_t *out_nbytes_ret); \
88 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes, \
92 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes, \
93 chartype2 **out_ret, \
94 size_t *out_nbytes_ret); \
96 /* multi-byte string to UTF16-LE string */
97 DECLARE_CHAR_CONVERSION_FUNCTIONS(mbs, utf16le, mbchar, utf16lechar);
99 /* UTF16-LE string to multi-byte string */
100 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf16le, mbs, utf16lechar, mbchar);
102 /* UTF-8 string to multi-byte string */
103 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf8, mbs, utf8char, mbchar);
106 utf8_str_contains_nonascii_chars(const utf8char *utf8_str);
109 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
110 (__a < __b) ? __a : __b; })
114 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
115 (__a > __b) ? __a : __b; })
119 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
123 * container_of - cast a member of a structure out to the containing structure
124 * @ptr: the pointer to the member.
125 * @type: the type of the container struct this is embedded in.
126 * @member: the name of the member within the struct.
130 #define container_of(ptr, type, member) ({ \
131 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
132 (type *)( (char *)__mptr - offsetof(type,member) );})
135 #define DIV_ROUND_UP(numerator, denominator) \
136 (((numerator) + (denominator) - 1) / (denominator))
138 #define MODULO_NONZERO(numerator, denominator) \
139 (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
141 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
143 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
145 /* Used for buffering FILE IO in a few places */
146 #define BUFFER_SIZE 4096
148 static inline void FORMAT(printf, 1, 2)
149 dummy_printf(const char *format, ...)
153 #ifdef ENABLE_ERROR_MESSAGES
155 wimlib_error(const char *format, ...) FORMAT(printf, 1, 2) COLD;
158 wimlib_error_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD;
161 wimlib_warning(const char *format, ...) FORMAT(printf, 1, 2) COLD;
164 wimlib_warning_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD;
165 # define ERROR wimlib_error
166 # define ERROR_WITH_ERRNO wimlib_error_with_errno
167 # define WARNING wimlib_warning
168 # define WARNING_WITH_ERRNO wimlib_warning
169 #else /* ENABLE_ERROR_MESSAGES */
170 # define ERROR(format, ...) dummy_printf(format, ## __VA_ARGS__)
171 # define ERROR_WITH_ERRNO(format, ...) dummy_printf(format, ## __VA_ARGS__)
172 # define WARNING(format, ...) dummy_printf(format, ## __VA_ARGS__)
173 # define WARNING_WITH_ERRNO(format, ...) dummy_printf(format, ## __VA_ARGS__)
174 #endif /* !ENABLE_ERROR_MESSAGES */
176 #if defined(ENABLE_MORE_DEBUG) && !defined(ENABLE_DEBUG)
177 # define ENABLE_DEBUG 1
180 #if defined(ENABLE_MORE_ASSERTIONS) && !defined(ENABLE_ASSERTIONS)
181 # define ENABLE_ASSERTIONS 1
186 wimlib_debug(const char *file, int line, const char *func,
187 const char *format, ...);
188 # define DEBUG(format, ...) \
189 wimlib_debug(__FILE__, __LINE__, __func__, format, ## __VA_ARGS__);
192 # define DEBUG(format, ...) dummy_printf(format, ## __VA_ARGS__)
193 #endif /* !ENABLE_DEBUG */
195 #ifdef ENABLE_MORE_DEBUG
196 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
198 # define DEBUG2(format, ...) dummy_printf(format, ## __VA_ARGS__)
199 #endif /* !ENABLE_MORE_DEBUG */
201 #ifdef ENABLE_ASSERTIONS
203 # define wimlib_assert(expr) assert(expr)
205 # define wimlib_assert(expr)
206 #endif /* !ENABLE_ASSERTIONS */
208 #ifdef ENABLE_MORE_ASSERTIONS
209 # define wimlib_assert2(expr) wimlib_assert(expr)
211 # define wimlib_assert2(expr)
212 #endif /* !ENABLE_MORE_ASSERTIONS */
214 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
216 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
217 extern void *(*wimlib_malloc_func)(size_t);
218 extern void (*wimlib_free_func)(void *);
219 extern void *(*wimlib_realloc_func)(void *, size_t);
220 extern void *wimlib_calloc(size_t nmemb, size_t size);
221 extern char *wimlib_strdup(const char *str);
222 # define MALLOC wimlib_malloc_func
223 # define FREE wimlib_free_func
224 # define REALLOC wimlib_realloc_func
225 # define CALLOC wimlib_calloc
226 # define STRDUP wimlib_strdup
227 #else /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
230 # define MALLOC malloc
232 # define REALLOC realloc
233 # define CALLOC calloc
234 # define STRDUP strdup
235 #endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */
240 randomize_byte_array(u8 *p, size_t n);
243 randomize_char_array_with_alnum(char p[], size_t n);
246 path_next_part(const char *path, size_t *first_part_len_ret);
249 path_basename(const char *path);
252 path_stream_name(const char *path);
255 to_parent_name(char buf[], size_t len);
258 print_string(const void *string, size_t len);
261 get_num_path_components(const char *path);
264 print_byte_field(const u8 field[], size_t len)
267 printf("%02hhx", *field++);
273 #if defined(__x86__) || defined(__x86_64__)
280 while ((n >>= 1) != 0)
287 wimlib_fprintf(FILE *fp, const char *format, ...)
288 //FORMAT(printf, 2, 3)
292 wimlib_printf(const char *format, ...)
293 //FORMAT(printf, 1, 2)
296 #endif /* _WIMLIB_UTIL_H */