4 #include "wimlib/types.h"
5 #include "wimlib/compiler.h"
11 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
12 (__a < __b) ? __a : __b; })
16 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
17 (__a > __b) ? __a : __b; })
21 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
25 * container_of - cast a member of a structure out to the containing structure
26 * @ptr: the pointer to the member.
27 * @type: the type of the container struct this is embedded in.
28 * @member: the name of the member within the struct.
32 #define container_of(ptr, type, member) ({ \
33 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
34 (type *)( (char *)__mptr - offsetof(type,member) );})
37 #define DIV_ROUND_UP(numerator, denominator) \
38 (((numerator) + (denominator) - 1) / (denominator))
40 #define MODULO_NONZERO(numerator, denominator) \
41 (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
43 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
45 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
47 /* Used for buffering FILE IO in a few places */
48 #define BUFFER_SIZE 32768
50 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
52 /* Maximum number of array elements to allocate on the stack (used in various
53 * places when large temporary buffers are needed). */
54 #define STACK_MAX 32768
57 wimlib_malloc(size_t) _malloc_attribute;
60 wimlib_free_memory(void *p);
63 wimlib_realloc(void *, size_t) _warn_unused_result_attribute;
66 wimlib_calloc(size_t nmemb, size_t size) _malloc_attribute;
70 wimlib_wcsdup(const wchar_t *str) _malloc_attribute;
74 wimlib_strdup(const char *str) _malloc_attribute;
76 #define MALLOC wimlib_malloc
77 #define FREE wimlib_free_memory
78 #define REALLOC wimlib_realloc
79 #define CALLOC wimlib_calloc
80 #define STRDUP wimlib_strdup
81 #define WCSDUP wimlib_wcsdup
84 memdup(const void *mem, size_t size) _malloc_attribute;
88 mempcpy(void *dst, const void *src, size_t n);
93 randomize_byte_array(u8 *p, size_t n);
96 randomize_char_array_with_alnum(tchar p[], size_t n);
99 print_byte_field(const u8 field[], size_t len, FILE *out);
104 #if defined(__x86__) || defined(__x86_64__)
111 while ((n >>= 1) != 0)
118 is_power_of_2(unsigned long n)
120 return (n != 0 && (n & (n - 1)) == 0);
127 return n * 0x9e37fffffffc0001ULL;
131 cmp_u64(u64 n1, u64 n2)
141 /* is_any_path_separator() - characters treated as path separators in WIM path
142 * specifications and capture configuration files (the former will be translated
143 * to WIM_PATH_SEPARATOR; the latter will be translated to
144 * OS_PREFERRED_PATH_SEPARATOR)
146 * OS_PREFERRED_PATH_SEPARATOR - preferred (or only) path separator on the
147 * operating system. Used when constructing filesystem paths to extract or
150 * WIM_PATH_SEPARATOR - character treated as path separator for WIM paths.
151 * Currently needs to be '/' on UNIX for the WIM mounting code to work properly.
155 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
156 # define is_any_path_separator(c) ((c) == L'/' || (c) == L'\\')
158 # define OS_PREFERRED_PATH_SEPARATOR '/'
159 # define is_any_path_separator(c) ((c) == '/' || (c) == '\\')
162 #define WIM_PATH_SEPARATOR WIMLIB_WIM_PATH_SEPARATOR
164 #endif /* _WIMLIB_UTIL_H */