]> wimlib.net Git - wimlib/blob - include/wimlib/util.h
Remove quotes in do_load_text_file()
[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 #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
81
82 extern void *
83 memdup(const void *mem, size_t size) _malloc_attribute;
84
85 #ifndef HAVE_MEMPCPY
86 extern void *
87 mempcpy(void *dst, const void *src, size_t n);
88 #endif
89
90 /* util.c */
91 extern void
92 randomize_byte_array(u8 *p, size_t n);
93
94 extern void
95 randomize_char_array_with_alnum(tchar p[], size_t n);
96
97 extern void
98 print_byte_field(const u8 field[], size_t len, FILE *out);
99
100 static inline u32
101 bsr32(u32 n)
102 {
103 #if defined(__x86__) || defined(__x86_64__)
104         asm("bsrl %0, %0;"
105                         : "=r"(n)
106                         : "0" (n));
107         return n;
108 #else
109         u32 pow = 0;
110         while ((n >>= 1) != 0)
111                 pow++;
112         return pow;
113 #endif
114 }
115
116 static inline bool
117 is_power_of_2(unsigned long n)
118 {
119         return (n != 0 && (n & (n - 1)) == 0);
120
121 }
122
123 static inline u64
124 hash_u64(u64 n)
125 {
126         return n * 0x9e37fffffffc0001ULL;
127 }
128
129 static inline int
130 cmp_u64(u64 n1, u64 n2)
131 {
132         if (n1 < n2)
133                 return -1;
134         else if (n1 > n2)
135                 return 1;
136         else
137                 return 0;
138 }
139
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)
144  *
145  * OS_PREFERRED_PATH_SEPARATOR - preferred (or only) path separator on the
146  * operating system.  Used when constructing filesystem paths to extract or
147  * archive.
148  *
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.
151  */
152
153 #ifdef __WIN32__
154 #  define OS_PREFERRED_PATH_SEPARATOR L'\\'
155 #  define is_any_path_separator(c) ((c) == L'/' || (c) == L'\\')
156 #else
157 #  define OS_PREFERRED_PATH_SEPARATOR '/'
158 #  define is_any_path_separator(c) ((c) == '/' || (c) == '\\')
159 #endif
160
161 #define WIM_PATH_SEPARATOR WIMLIB_WIM_PATH_SEPARATOR
162
163 #endif /* _WIMLIB_UTIL_H */