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)
53 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
54 (__a < __b) ? __a : __b; })
58 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
59 (__a > __b) ? __a : __b; })
63 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
67 * container_of - cast a member of a structure out to the containing structure
68 * @ptr: the pointer to the member.
69 * @type: the type of the container struct this is embedded in.
70 * @member: the name of the member within the struct.
74 #define container_of(ptr, type, member) ({ \
75 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
76 (type *)( (char *)__mptr - offsetof(type,member) );})
79 #define DIV_ROUND_UP(numerator, denominator) \
80 (((numerator) + (denominator) - 1) / (denominator))
82 #define MODULO_NONZERO(numerator, denominator) \
83 (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
85 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
87 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
89 /* Used for buffering FILE IO in a few places */
90 #define BUFFER_SIZE 4096
92 #ifdef ENABLE_ERROR_MESSAGES
93 extern void wimlib_error(const char *format, ...)
94 FORMAT(printf, 1, 2) COLD;
95 extern void wimlib_error_with_errno(const char *format, ...)
96 FORMAT(printf, 1, 2) COLD;
97 extern void wimlib_warning(const char *format, ...)
98 FORMAT(printf, 1, 2) COLD;
99 extern void wimlib_warning_with_errno(const char *format, ...)
100 FORMAT(printf, 1, 2) COLD;
101 # define ERROR wimlib_error
102 # define ERROR_WITH_ERRNO wimlib_error_with_errno
103 # define WARNING wimlib_warning
104 # define WARNING_WITH_ERRNO wimlib_warning
106 static inline FORMAT(printf, 1, 2) void
107 dummy_printf(const char *format, ...) { }
108 # define ERROR(format, ...) dummy_printf
109 # define ERROR_WITH_ERRNO(format, ...) dummy_printf
110 # define WARNING(format, ...) dummy_printf
111 # define WARNING_WITH_ERRNO(format, ...) dummy_printf
112 #endif /* ENABLE_ERROR_MESSAGES */
114 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
116 # define DEBUG(format, ...) \
118 int __errno_save = errno; \
119 fprintf(stdout, "[%s %d] %s(): " format, \
120 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
123 errno = __errno_save; \
127 # define DEBUG(format, ...)
128 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
130 #ifdef ENABLE_MORE_DEBUG
131 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
133 # define DEBUG2(format, ...)
134 #endif /* ENABLE_DEBUG */
136 #ifdef ENABLE_ASSERTIONS
138 # define wimlib_assert(expr) assert(expr)
140 # define wimlib_assert(expr)
143 #ifdef ENABLE_MORE_ASSERTIONS
144 #define wimlib_assert2(expr) wimlib_assert(expr)
146 #define wimlib_assert2(expr)
149 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
151 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
152 extern void *(*wimlib_malloc_func)(size_t);
153 extern void (*wimlib_free_func)(void *);
154 extern void *(*wimlib_realloc_func)(void *, size_t);
155 extern void *wimlib_calloc(size_t nmemb, size_t size);
156 extern char *wimlib_strdup(const char *str);
157 # define MALLOC wimlib_malloc_func
158 # define FREE wimlib_free_func
159 # define REALLOC wimlib_realloc_func
160 # define CALLOC wimlib_calloc
161 # define STRDUP wimlib_strdup
165 # define MALLOC malloc
167 # define REALLOC realloc
168 # define CALLOC calloc
169 # define STRDUP strdup
170 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
176 static inline int iconv_global_init()
181 static inline void iconv_global_cleanup() { }
183 extern int iconv_global_init();
184 extern void iconv_global_cleanup();
187 extern int utf16_to_utf8(const char *utf16_str, size_t utf16_nbytes,
188 char **utf8_str_ret, size_t *utf8_nbytes_ret);
190 extern int utf8_to_utf16(const char *utf8_str, size_t utf8_nbytes,
191 char **utf16_str_ret, size_t *utf16_nbytes_ret);
194 extern void randomize_byte_array(u8 *p, size_t n);
196 extern void randomize_char_array_with_alnum(char p[], size_t n);
198 extern const char *path_next_part(const char *path,
199 size_t *first_part_len_ret);
201 extern const char *path_basename(const char *path);
203 extern const char *path_stream_name(const char *path);
205 extern void to_parent_name(char buf[], size_t len);
207 extern void print_string(const void *string, size_t len);
209 extern int get_num_path_components(const char *path);
211 static inline void print_byte_field(const u8 field[], size_t len)
214 printf("%02hhx", *field++);
217 static inline u32 bsr32(u32 n)
219 #if defined(__x86__) || defined(__x86_64__)
226 while ((n >>= 1) != 0)
232 #endif /* _WIMLIB_UTIL_H */