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)
34 #define atomic_inc(ptr) \
35 __sync_fetch_and_add(ptr, 1)
37 #define atomic_dec(ptr) \
38 __sync_sub_and_fetch(ptr, 1)
47 typedef unsigned uint;
50 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
51 (__a < __b) ? __a : __b; })
55 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
56 (__a > __b) ? __a : __b; })
60 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
64 * container_of - cast a member of a structure out to the containing structure
65 * @ptr: the pointer to the member.
66 * @type: the type of the container struct this is embedded in.
67 * @member: the name of the member within the struct.
71 #define container_of(ptr, type, member) ({ \
72 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
73 (type *)( (char *)__mptr - offsetof(type,member) );})
76 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
78 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
80 /* Used for buffering FILE IO in a few places */
81 #define BUFFER_SIZE 4096
83 #ifdef ENABLE_ERROR_MESSAGES
84 extern bool __wimlib_print_errors;
85 extern void wimlib_error(const char *format, ...)
86 FORMAT(printf, 1, 2) COLD;
87 extern void wimlib_error_with_errno(const char *format, ...)
88 FORMAT(printf, 1, 2) COLD;
89 extern void wimlib_warning(const char *format, ...)
90 FORMAT(printf, 1, 2) COLD;
91 # define ERROR wimlib_error
92 # define ERROR_WITH_ERRNO wimlib_error_with_errno
93 # define WARNING wimlib_warning
95 # define ERROR(format, ...)
96 # define ERROR_WITH_ERRNO(format, ...)
97 # define WARNING(format, ...)
98 #endif /* ENABLE_ERROR_MESSAGES */
100 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
102 # define DEBUG(format, ...) \
104 int __errno_save = errno; \
105 fprintf(stdout, "[%s %d] %s(): " format, \
106 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
109 errno = __errno_save; \
113 # define DEBUG(format, ...)
114 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
116 #ifdef ENABLE_MORE_DEBUG
117 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
119 # define DEBUG2(format, ...)
120 #endif /* ENABLE_DEBUG */
122 #ifdef ENABLE_ASSERTIONS
124 # define wimlib_assert(expr) assert(expr)
126 # define wimlib_assert(expr)
130 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
131 extern void *(*wimlib_malloc_func)(size_t);
132 extern void (*wimlib_free_func)(void *);
133 extern void *(*wimlib_realloc_func)(void *, size_t);
134 extern void *wimlib_calloc(size_t nmemb, size_t size);
135 extern char *wimlib_strdup(const char *str);
136 # define MALLOC wimlib_malloc_func
137 # define FREE wimlib_free_func
138 # define REALLOC wimlib_realloc_func
139 # define CALLOC wimlib_calloc
140 # define STRDUP wimlib_strdup
144 # define MALLOC malloc
146 # define REALLOC realloc
147 # define CALLOC calloc
148 # define STRDUP strdup
149 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
152 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
153 size_t *utf8_len_ret);
155 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
156 size_t *utf16_len_ret);
158 extern void randomize_byte_array(u8 *p, size_t n);
160 extern void randomize_char_array_with_alnum(char p[], size_t n);
162 extern const char *path_next_part(const char *path,
163 size_t *first_part_len_ret);
165 extern const char *path_basename(const char *path);
167 extern const char *path_stream_name(const char *path);
169 extern void to_parent_name(char buf[], size_t len);
171 extern void print_string(const void *string, size_t len);
173 extern int get_num_path_components(const char *path);
175 extern ssize_t full_write(int fd, const void *buf, size_t n);
177 static inline void print_byte_field(const u8 field[], size_t len)
180 printf("%02hhx", *field++);
184 #endif /* _WIMLIB_UTIL_H */