]> wimlib.net Git - wimlib/blob - include/wimlib/util.h
Re-visit SHA-1 code
[wimlib] / include / wimlib / util.h
1 #ifndef _WIMLIB_UTIL_H
2 #define _WIMLIB_UTIL_H
3
4 #include "wimlib/types.h"
5 #include "wimlib/compiler.h"
6
7 #include <stdio.h>
8
9 #ifndef min
10 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
11                                         (__a < __b) ? __a : __b; })
12 #endif
13
14 #ifndef max
15 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
16                                         (__a > __b) ? __a : __b; })
17 #endif
18
19 #ifndef swap
20 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
21 #endif
22
23 /**
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.
28  *
29  */
30 #ifndef container_of
31 #define container_of(ptr, type, member) ({                      \
32         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
33         (type *)( (char *)__mptr - offsetof(type,member) );})
34 #endif
35
36 #define DIV_ROUND_UP(numerator, denominator) \
37         (((numerator) + (denominator) - 1) / (denominator))
38
39 #define MODULO_NONZERO(numerator, denominator) \
40         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
41
42 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
43
44 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
45
46 /* Used for buffering FILE IO in a few places */
47 #define BUFFER_SIZE 32768
48
49 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
50
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
54
55 extern void *
56 wimlib_malloc(size_t) _malloc_attribute;
57
58 extern void
59 wimlib_free_memory(void *p);
60
61 extern void *
62 wimlib_realloc(void *, size_t) _warn_unused_result_attribute;
63
64 extern void *
65 wimlib_calloc(size_t nmemb, size_t size) _malloc_attribute;
66
67 #ifdef __WIN32__
68 extern wchar_t *
69 wimlib_wcsdup(const wchar_t *str) _malloc_attribute;
70
71 #endif
72 extern char *
73 wimlib_strdup(const char *str) _malloc_attribute;
74
75 extern void *
76 wimlib_aligned_malloc(size_t size, size_t alignment) _malloc_attribute;
77
78 extern void
79 wimlib_aligned_free(void *ptr);
80
81 #define MALLOC  wimlib_malloc
82 #define FREE    wimlib_free_memory
83 #define REALLOC wimlib_realloc
84 #define CALLOC  wimlib_calloc
85 #define STRDUP  wimlib_strdup
86 #define WCSDUP  wimlib_wcsdup
87 #define ALIGNED_MALLOC  wimlib_aligned_malloc
88 #define ALIGNED_FREE    wimlib_aligned_free
89
90 extern void *
91 memdup(const void *mem, size_t size) _malloc_attribute;
92
93 #ifndef HAVE_MEMPCPY
94 extern void *
95 mempcpy(void *dst, const void *src, size_t n);
96 #endif
97
98 extern size_t
99 utf16le_strlen(const utf16lechar *s);
100
101 extern void
102 randomize_byte_array(u8 *p, size_t n);
103
104 extern void
105 randomize_char_array_with_alnum(tchar p[], size_t n);
106
107 extern void
108 print_byte_field(const u8 field[], size_t len, FILE *out);
109
110 static inline u32
111 bsr32(u32 n)
112 {
113 #if defined(__x86__) || defined(__x86_64__)
114         asm("bsrl %0, %0;"
115                         : "=r"(n)
116                         : "0" (n));
117         return n;
118 #else
119         u32 pow = 0;
120         while ((n >>= 1) != 0)
121                 pow++;
122         return pow;
123 #endif
124 }
125
126 static inline bool
127 is_power_of_2(unsigned long n)
128 {
129         return (n != 0 && (n & (n - 1)) == 0);
130
131 }
132
133 static inline u64
134 hash_u64(u64 n)
135 {
136         return n * 0x9e37fffffffc0001ULL;
137 }
138
139 static inline int
140 cmp_u64(u64 n1, u64 n2)
141 {
142         if (n1 < n2)
143                 return -1;
144         else if (n1 > n2)
145                 return 1;
146         else
147                 return 0;
148 }
149
150 /* is_any_path_separator() - characters treated as path separators in WIM path
151  * specifications and capture configuration files (the former will be translated
152  * to WIM_PATH_SEPARATOR; the latter will be translated to
153  * OS_PREFERRED_PATH_SEPARATOR)
154  *
155  * OS_PREFERRED_PATH_SEPARATOR - preferred (or only) path separator on the
156  * operating system.  Used when constructing filesystem paths to extract or
157  * archive.
158  *
159  * WIM_PATH_SEPARATOR - character treated as path separator for WIM paths.
160  * Currently needs to be '/' on UNIX for the WIM mounting code to work properly.
161  */
162
163 #ifdef __WIN32__
164 #  define OS_PREFERRED_PATH_SEPARATOR L'\\'
165 #  define is_any_path_separator(c) ((c) == L'/' || (c) == L'\\')
166 #else
167 #  define OS_PREFERRED_PATH_SEPARATOR '/'
168 #  define is_any_path_separator(c) ((c) == '/' || (c) == '\\')
169 #endif
170
171 #define WIM_PATH_SEPARATOR WIMLIB_WIM_PATH_SEPARATOR
172
173 #endif /* _WIMLIB_UTIL_H */