4 #include "wimlib/types.h"
5 #include "wimlib/compiler.h"
10 * container_of - cast a member of a structure out to the containing structure
11 * @ptr: the pointer to the member.
12 * @type: the type of the container struct this is embedded in.
13 * @member: the name of the member within the struct.
17 #define container_of(ptr, type, member) ({ \
18 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
19 (type *)( (char *)__mptr - offsetof(type,member) );})
22 #define DIV_ROUND_UP(numerator, denominator) \
23 (((numerator) + (denominator) - 1) / (denominator))
25 #define MODULO_NONZERO(numerator, denominator) \
26 (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
28 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
30 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
32 /* Used for buffering FILE IO in a few places */
33 #define BUFFER_SIZE 32768
35 /* Maximum number of array elements to allocate on the stack (used in various
36 * places when large temporary buffers are needed). */
37 #define STACK_MAX 32768
40 wimlib_malloc(size_t) _malloc_attribute;
43 wimlib_free_memory(void *p);
46 wimlib_realloc(void *, size_t);
49 wimlib_calloc(size_t nmemb, size_t size) _malloc_attribute;
53 wimlib_wcsdup(const wchar_t *str) _malloc_attribute;
57 wimlib_strdup(const char *str) _malloc_attribute;
60 wimlib_aligned_malloc(size_t size, size_t alignment) _malloc_attribute;
63 wimlib_aligned_free(void *ptr);
65 #define MALLOC wimlib_malloc
66 #define FREE wimlib_free_memory
67 #define REALLOC wimlib_realloc
68 #define CALLOC wimlib_calloc
69 #define STRDUP wimlib_strdup
70 #define WCSDUP wimlib_wcsdup
71 #define ALIGNED_MALLOC wimlib_aligned_malloc
72 #define ALIGNED_FREE wimlib_aligned_free
75 memdup(const void *mem, size_t size) _malloc_attribute;
79 mempcpy(void *dst, const void *src, size_t n);
83 utf16le_strlen(const utf16lechar *s);
86 randomize_byte_array(u8 *p, size_t n);
89 randomize_char_array_with_alnum(tchar p[], size_t n);
92 print_byte_field(const u8 field[], size_t len, FILE *out);
95 is_power_of_2(unsigned long n)
97 return (n != 0 && (n & (n - 1)) == 0);
104 return n * 0x9e37fffffffc0001ULL;
108 cmp_u64(u64 n1, u64 n2)
118 /* is_any_path_separator() - characters treated as path separators in WIM path
119 * specifications and capture configuration files (the former will be translated
120 * to WIM_PATH_SEPARATOR; the latter will be translated to
121 * OS_PREFERRED_PATH_SEPARATOR)
123 * OS_PREFERRED_PATH_SEPARATOR - preferred (or only) path separator on the
124 * operating system. Used when constructing filesystem paths to extract or
127 * WIM_PATH_SEPARATOR - character treated as path separator for WIM paths.
128 * Currently needs to be '/' on UNIX for the WIM mounting code to work properly.
132 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
133 # define is_any_path_separator(c) ((c) == L'/' || (c) == L'\\')
135 # define OS_PREFERRED_PATH_SEPARATOR '/'
136 # define is_any_path_separator(c) ((c) == '/' || (c) == '\\')
139 #define WIM_PATH_SEPARATOR WIMLIB_WIM_PATH_SEPARATOR
141 #endif /* _WIMLIB_UTIL_H */