12 # define WIMLIBAPI __attribute__((visibility("default")))
13 # define NOINLINE __attribute__((noinline))
14 # define ALWAYS_INLINE inline __attribute__((always_inline))
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 # define HOT __attribute__((hot))
27 # define ALWAYS_INLINE inline
28 # define FORMAT(type, format_str, args_start)
37 typedef unsigned uint;
39 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
40 (__a < __b) ? __a : __b; })
41 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
42 (__a > __b) ? __a : __b; })
43 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
45 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
47 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
49 /* Used for buffering FILE IO in a few places */
50 #define BUFFER_SIZE 4096
52 #ifdef ENABLE_ERROR_MESSAGES
53 extern bool __wimlib_print_errors;
54 extern void wimlib_error(const char *format, ...)
55 FORMAT(printf, 1, 2) COLD;
56 extern void wimlib_error_with_errno(const char *format, ...)
57 FORMAT(printf, 1, 2) COLD;
58 extern void wimlib_warning(const char *format, ...)
59 FORMAT(printf, 1, 2) COLD;
60 # define ERROR wimlib_error
61 # define ERROR_WITH_ERRNO wimlib_error_with_errno
62 # define WARNING wimlib_warning
64 # define ERROR(format, ...)
65 # define ERROR_WITH_ERRNO(format, ...)
66 # define WARNING(format, ...)
67 #endif /* ENABLE_ERROR_MESSAGES */
69 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
71 # define DEBUG(format, ...) \
73 int __errno_save = errno; \
74 fprintf(stdout, "[%s %d] %s(): " format, \
75 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
78 errno = __errno_save; \
82 # define DEBUG(format, ...)
83 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
85 #ifdef ENABLE_MORE_DEBUG
86 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
88 # define DEBUG2(format, ...)
89 #endif /* ENABLE_DEBUG */
91 #ifdef ENABLE_ASSERTIONS
93 # define wimlib_assert(expr) assert(expr)
95 # define wimlib_assert(expr)
99 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
100 extern void *(*wimlib_malloc_func)(size_t);
101 extern void (*wimlib_free_func)(void *);
102 extern void *(*wimlib_realloc_func)(void *, size_t);
103 extern void *wimlib_calloc(size_t nmemb, size_t size);
104 extern char *wimlib_strdup(const char *str);
105 # define MALLOC wimlib_malloc_func
106 # define FREE wimlib_free_func
107 # define REALLOC wimlib_realloc_func
108 # define CALLOC wimlib_calloc
109 # define STRDUP wimlib_strdup
113 # define MALLOC malloc
115 # define REALLOC realloc
116 # define CALLOC calloc
117 # define STRDUP strdup
118 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
121 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
122 size_t *utf8_len_ret);
124 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
125 size_t *utf16_len_ret);
127 extern void randomize_byte_array(u8 *p, size_t n);
129 extern void randomize_char_array_with_alnum(char p[], size_t n);
131 extern int sha1sum(const char *filename, void *buf);
133 extern const char *path_next_part(const char *path,
134 size_t *first_part_len_ret);
136 extern const char *path_basename(const char *path);
138 extern void to_parent_name(char buf[], size_t len);
140 extern void print_string(const void *string, size_t len);
142 extern int get_num_path_components(const char *path);
144 extern ssize_t full_write(int fd, const void *buf, size_t n);
146 static inline void print_byte_field(const u8 field[], size_t len)
149 printf("%02hhx", *field++);
153 #endif /* _WIMLIB_UTIL_H */