5 #include "wimlib_tchar.h"
11 #include <sys/types.h>
14 # if defined(__CYGWIN__) || defined(__WIN32__)
15 # define WIMLIBAPI __declspec(dllexport)
17 # define WIMLIBAPI __attribute__((visibility("default")))
19 # define ALWAYS_INLINE inline __attribute__((always_inline))
20 # define PACKED __attribute__((packed))
21 # define FORMAT(type, format_str, args_start) \
22 /*__attribute__((format(type, format_str, args_start))) */
23 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
24 # define COLD __attribute__((cold))
30 # define ALWAYS_INLINE inline
31 # define FORMAT(type, format_str, args_start)
39 #define atomic_inc(ptr) \
40 __sync_fetch_and_add(ptr, 1)
42 #define atomic_dec(ptr) \
43 __sync_sub_and_fetch(ptr, 1)
55 /* A pointer to 'utf16lechar' indicates a UTF-16LE encoded string */
56 typedef u16 utf16lechar;
58 #define TMALLOC(n) MALLOC((n) * sizeof(tchar))
62 iconv_global_cleanup();
64 extern bool wimlib_mbs_is_utf8;
66 #define DECLARE_CHAR_CONVERSION_FUNCTIONS(varname1, varname2, \
67 chartype1, chartype2) \
70 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes, \
71 chartype2 **out_ret, \
72 size_t *out_nbytes_ret); \
75 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
76 size_t *out_nbytes_ret); \
79 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes, \
84 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf16le, tstr, utf16lechar, tchar);
85 DECLARE_CHAR_CONVERSION_FUNCTIONS(tstr, utf16le, tchar, utf16lechar);
89 utf8_to_tstr_simple(const char *utf8str, tchar **out);
92 tstr_to_utf8_simple(const tchar *tstr, char **out);
95 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
96 (__a < __b) ? __a : __b; })
100 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
101 (__a > __b) ? __a : __b; })
105 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
109 * container_of - cast a member of a structure out to the containing structure
110 * @ptr: the pointer to the member.
111 * @type: the type of the container struct this is embedded in.
112 * @member: the name of the member within the struct.
116 #define container_of(ptr, type, member) ({ \
117 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
118 (type *)( (char *)__mptr - offsetof(type,member) );})
121 #define DIV_ROUND_UP(numerator, denominator) \
122 (((numerator) + (denominator) - 1) / (denominator))
124 #define MODULO_NONZERO(numerator, denominator) \
125 (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
127 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
129 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
131 /* Used for buffering FILE IO in a few places */
132 #define BUFFER_SIZE 4096
134 static inline void FORMAT(printf, 1, 2)
135 dummy_tprintf(const tchar *format, ...)
139 #ifdef ENABLE_ERROR_MESSAGES
141 wimlib_error(const tchar *format, ...) FORMAT(printf, 1, 2) COLD;
144 wimlib_error_with_errno(const tchar *format, ...) FORMAT(printf, 1, 2) COLD;
147 wimlib_warning(const tchar *format, ...) FORMAT(printf, 1, 2) COLD;
150 wimlib_warning_with_errno(const tchar *format, ...) FORMAT(printf, 1, 2) COLD;
151 # define ERROR(format, ...) wimlib_error(T(format), ## __VA_ARGS__)
152 # define ERROR_WITH_ERRNO(format, ...) wimlib_error_with_errno(T(format), ## __VA_ARGS__)
153 # define WARNING(format, ...) wimlib_warning(T(format), ## __VA_ARGS__)
154 # define WARNING_WITH_ERRNO(format, ...) wimlib_warning(T(format), ## __VA_ARGS__)
155 #else /* ENABLE_ERROR_MESSAGES */
156 # define ERROR(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
157 # define ERROR_WITH_ERRNO(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
158 # define WARNING(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
159 # define WARNING_WITH_ERRNO(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
160 #endif /* !ENABLE_ERROR_MESSAGES */
162 #if defined(ENABLE_MORE_DEBUG) && !defined(ENABLE_DEBUG)
163 # define ENABLE_DEBUG 1
166 #if defined(ENABLE_MORE_ASSERTIONS) && !defined(ENABLE_ASSERTIONS)
167 # define ENABLE_ASSERTIONS 1
172 wimlib_debug(const tchar *file, int line, const char *func,
173 const tchar *format, ...);
174 # define DEBUG(format, ...) \
175 wimlib_debug(T(__FILE__), __LINE__, __func__, T(format), ## __VA_ARGS__)
178 # define DEBUG(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
179 #endif /* !ENABLE_DEBUG */
181 #ifdef ENABLE_MORE_DEBUG
182 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
184 # define DEBUG2(format, ...) dummy_tprintf(T(format), ## __VA_ARGS__)
185 #endif /* !ENABLE_MORE_DEBUG */
187 #ifdef ENABLE_ASSERTIONS
189 # define wimlib_assert(expr) assert(expr)
191 # define wimlib_assert(expr)
192 #endif /* !ENABLE_ASSERTIONS */
194 #ifdef ENABLE_MORE_ASSERTIONS
195 # define wimlib_assert2(expr) wimlib_assert(expr)
197 # define wimlib_assert2(expr)
198 #endif /* !ENABLE_MORE_ASSERTIONS */
200 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
202 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
203 extern void *(*wimlib_malloc_func)(size_t);
204 extern void (*wimlib_free_func)(void *);
205 extern void *(*wimlib_realloc_func)(void *, size_t);
206 extern void *wimlib_calloc(size_t nmemb, size_t size);
208 extern wchar_t *wimlib_wcsdup(const wchar_t *str);
210 extern char *wimlib_strdup(const char *str);
211 # define MALLOC wimlib_malloc_func
212 # define FREE wimlib_free_func
213 # define REALLOC wimlib_realloc_func
214 # define CALLOC wimlib_calloc
215 # define STRDUP wimlib_strdup
216 # define WSTRDUP wimlib_wcsdup
217 #else /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
220 # define MALLOC malloc
222 # define REALLOC realloc
223 # define CALLOC calloc
224 # define STRDUP strdup
225 # define WSTRDUP wcsdup
226 #endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */
231 randomize_byte_array(u8 *p, size_t n);
234 randomize_char_array_with_alnum(tchar p[], size_t n);
237 path_basename_with_len(const tchar *path, size_t len);
240 path_basename(const tchar *path);
243 path_stream_name(const tchar *path);
246 print_byte_field(const u8 field[], size_t len, FILE *out)
249 tfprintf(out, T("%02hhx"), *field++);
255 #if defined(__x86__) || defined(__x86_64__)
262 while ((n >>= 1) != 0)
269 # define wimlib_fprintf fwprintf
270 # define wimlib_printf wprintf
271 #else /* __WIN32__ */
273 wimlib_fprintf(FILE *fp, const tchar *format, ...) FORMAT(printf, 2, 3);
276 wimlib_printf(const tchar *format, ...) FORMAT(printf, 1, 2);
277 #endif /* !__WIN32__ */
280 zap_backslashes(tchar *s);
285 return n * 0x9e37fffffffc0001ULL;
288 #endif /* _WIMLIB_UTIL_H */