]> wimlib.net Git - wimlib/blob - include/wimlib/util.h
Header fixes
[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 /**
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.
14  *
15  */
16 #ifndef container_of
17 #define container_of(ptr, type, member) ({                      \
18         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
19         (type *)( (char *)__mptr - offsetof(type,member) );})
20 #endif
21
22 #define DIV_ROUND_UP(numerator, denominator) \
23         (((numerator) + (denominator) - 1) / (denominator))
24
25 #define MODULO_NONZERO(numerator, denominator) \
26         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
27
28 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
29
30 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
31
32 /* Used for buffering FILE IO in a few places */
33 #define BUFFER_SIZE 32768
34
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
38
39 extern void *
40 wimlib_malloc(size_t) _malloc_attribute;
41
42 extern void
43 wimlib_free_memory(void *p);
44
45 extern void *
46 wimlib_realloc(void *, size_t);
47
48 extern void *
49 wimlib_calloc(size_t nmemb, size_t size) _malloc_attribute;
50
51 #ifdef __WIN32__
52 extern wchar_t *
53 wimlib_wcsdup(const wchar_t *str) _malloc_attribute;
54
55 #endif
56 extern char *
57 wimlib_strdup(const char *str) _malloc_attribute;
58
59 extern void *
60 wimlib_aligned_malloc(size_t size, size_t alignment) _malloc_attribute;
61
62 extern void
63 wimlib_aligned_free(void *ptr);
64
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
73
74 extern void *
75 memdup(const void *mem, size_t size) _malloc_attribute;
76
77 #ifndef HAVE_MEMPCPY
78 extern void *
79 mempcpy(void *dst, const void *src, size_t n);
80 #endif
81
82 extern size_t
83 utf16le_strlen(const utf16lechar *s);
84
85 extern void
86 randomize_byte_array(u8 *p, size_t n);
87
88 extern void
89 randomize_char_array_with_alnum(tchar p[], size_t n);
90
91 extern void
92 print_byte_field(const u8 field[], size_t len, FILE *out);
93
94 static inline bool
95 is_power_of_2(unsigned long n)
96 {
97         return (n != 0 && (n & (n - 1)) == 0);
98
99 }
100
101 static inline u64
102 hash_u64(u64 n)
103 {
104         return n * 0x9e37fffffffc0001ULL;
105 }
106
107 static inline int
108 cmp_u64(u64 n1, u64 n2)
109 {
110         if (n1 < n2)
111                 return -1;
112         else if (n1 > n2)
113                 return 1;
114         else
115                 return 0;
116 }
117
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)
122  *
123  * OS_PREFERRED_PATH_SEPARATOR - preferred (or only) path separator on the
124  * operating system.  Used when constructing filesystem paths to extract or
125  * archive.
126  *
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.
129  */
130
131 #ifdef __WIN32__
132 #  define OS_PREFERRED_PATH_SEPARATOR L'\\'
133 #  define is_any_path_separator(c) ((c) == L'/' || (c) == L'\\')
134 #else
135 #  define OS_PREFERRED_PATH_SEPARATOR '/'
136 #  define is_any_path_separator(c) ((c) == '/' || (c) == '\\')
137 #endif
138
139 #define WIM_PATH_SEPARATOR WIMLIB_WIM_PATH_SEPARATOR
140
141 #endif /* _WIMLIB_UTIL_H */