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)
39 typedef unsigned uint;
42 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
43 (__a < __b) ? __a : __b; })
47 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
48 (__a > __b) ? __a : __b; })
52 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
56 * container_of - cast a member of a structure out to the containing structure
57 * @ptr: the pointer to the member.
58 * @type: the type of the container struct this is embedded in.
59 * @member: the name of the member within the struct.
63 #define container_of(ptr, type, member) ({ \
64 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
65 (type *)( (char *)__mptr - offsetof(type,member) );})
68 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
70 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
72 /* Used for buffering FILE IO in a few places */
73 #define BUFFER_SIZE 4096
75 #ifdef ENABLE_ERROR_MESSAGES
76 extern bool __wimlib_print_errors;
77 extern void wimlib_error(const char *format, ...)
78 FORMAT(printf, 1, 2) COLD;
79 extern void wimlib_error_with_errno(const char *format, ...)
80 FORMAT(printf, 1, 2) COLD;
81 extern void wimlib_warning(const char *format, ...)
82 FORMAT(printf, 1, 2) COLD;
83 # define ERROR wimlib_error
84 # define ERROR_WITH_ERRNO wimlib_error_with_errno
85 # define WARNING wimlib_warning
87 # define ERROR(format, ...)
88 # define ERROR_WITH_ERRNO(format, ...)
89 # define WARNING(format, ...)
90 #endif /* ENABLE_ERROR_MESSAGES */
92 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
94 # define DEBUG(format, ...) \
96 int __errno_save = errno; \
97 fprintf(stdout, "[%s %d] %s(): " format, \
98 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
101 errno = __errno_save; \
105 # define DEBUG(format, ...)
106 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
108 #ifdef ENABLE_MORE_DEBUG
109 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
111 # define DEBUG2(format, ...)
112 #endif /* ENABLE_DEBUG */
114 #ifdef ENABLE_ASSERTIONS
116 # define wimlib_assert(expr) assert(expr)
118 # define wimlib_assert(expr)
122 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
123 extern void *(*wimlib_malloc_func)(size_t);
124 extern void (*wimlib_free_func)(void *);
125 extern void *(*wimlib_realloc_func)(void *, size_t);
126 extern void *wimlib_calloc(size_t nmemb, size_t size);
127 extern char *wimlib_strdup(const char *str);
128 # define MALLOC wimlib_malloc_func
129 # define FREE wimlib_free_func
130 # define REALLOC wimlib_realloc_func
131 # define CALLOC wimlib_calloc
132 # define STRDUP wimlib_strdup
136 # define MALLOC malloc
138 # define REALLOC realloc
139 # define CALLOC calloc
140 # define STRDUP strdup
141 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
144 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
145 size_t *utf8_len_ret);
147 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
148 size_t *utf16_len_ret);
150 extern void randomize_byte_array(u8 *p, size_t n);
152 extern void randomize_char_array_with_alnum(char p[], size_t n);
154 extern const char *path_next_part(const char *path,
155 size_t *first_part_len_ret);
157 extern const char *path_basename(const char *path);
159 extern const char *path_stream_name(const char *path);
161 extern void to_parent_name(char buf[], size_t len);
163 extern void print_string(const void *string, size_t len);
165 extern int get_num_path_components(const char *path);
167 extern ssize_t full_write(int fd, const void *buf, size_t n);
169 static inline void print_byte_field(const u8 field[], size_t len)
172 printf("%02hhx", *field++);
176 #endif /* _WIMLIB_UTIL_H */