4 #include "wimlib/types.h"
5 #include "wimlib/compiler.h"
10 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
11 (__a < __b) ? __a : __b; })
15 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
16 (__a > __b) ? __a : __b; })
20 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
24 * container_of - cast a member of a structure out to the containing structure
25 * @ptr: the pointer to the member.
26 * @type: the type of the container struct this is embedded in.
27 * @member: the name of the member within the struct.
31 #define container_of(ptr, type, member) ({ \
32 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
33 (type *)( (char *)__mptr - offsetof(type,member) );})
36 #define DIV_ROUND_UP(numerator, denominator) \
37 (((numerator) + (denominator) - 1) / (denominator))
39 #define MODULO_NONZERO(numerator, denominator) \
40 (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
42 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
44 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
46 /* Used for buffering FILE IO in a few places */
47 #define BUFFER_SIZE 32768
49 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
51 /* Maximum number of array elements to allocate on the stack (used in various
52 * places when large temporary buffers are needed). */
53 #define STACK_MAX 32768
56 wimlib_malloc(size_t) _malloc_attribute;
59 wimlib_free_memory(void *p);
62 wimlib_realloc(void *, size_t) _warn_unused_result_attribute;
65 wimlib_calloc(size_t nmemb, size_t size) _malloc_attribute;
69 wimlib_wcsdup(const wchar_t *str) _malloc_attribute;
73 wimlib_strdup(const char *str) _malloc_attribute;
75 #define MALLOC wimlib_malloc
76 #define FREE wimlib_free_memory
77 #define REALLOC wimlib_realloc
78 #define CALLOC wimlib_calloc
79 #define STRDUP wimlib_strdup
80 #define WCSDUP wimlib_wcsdup
83 memdup(const void *mem, size_t size) _malloc_attribute;
87 mempcpy(void *dst, const void *src, size_t n);
92 randomize_byte_array(u8 *p, size_t n);
95 randomize_char_array_with_alnum(tchar p[], size_t n);
98 print_byte_field(const u8 field[], size_t len, FILE *out);
103 #if defined(__x86__) || defined(__x86_64__)
110 while ((n >>= 1) != 0)
117 is_power_of_2(unsigned long n)
119 return (n != 0 && (n & (n - 1)) == 0);
126 return n * 0x9e37fffffffc0001ULL;
130 cmp_u64(u64 n1, u64 n2)
140 /* is_any_path_separator() - characters treated as path separators in WIM path
141 * specifications and capture configuration files (the former will be translated
142 * to WIM_PATH_SEPARATOR; the latter will be translated to
143 * OS_PREFERRED_PATH_SEPARATOR)
145 * OS_PREFERRED_PATH_SEPARATOR - preferred (or only) path separator on the
146 * operating system. Used when constructing filesystem paths to extract or
149 * WIM_PATH_SEPARATOR - character treated as path separator for WIM paths.
150 * Currently needs to be '/' on UNIX for the WIM mounting code to work properly.
154 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
155 # define is_any_path_separator(c) ((c) == L'/' || (c) == L'\\')
157 # define OS_PREFERRED_PATH_SEPARATOR '/'
158 # define is_any_path_separator(c) ((c) == '/' || (c) == '\\')
161 #define WIM_PATH_SEPARATOR WIMLIB_WIM_PATH_SEPARATOR
163 #endif /* _WIMLIB_UTIL_H */