]> wimlib.net Git - wimlib/blob - include/wimlib/util.h
Adjust endianness detection
[wimlib] / include / wimlib / util.h
1 /*
2  * util.h - utility functions and macros
3  */
4 #ifndef _WIMLIB_UTIL_H
5 #define _WIMLIB_UTIL_H
6
7 #include "wimlib/compiler.h"
8 #include "wimlib/types.h"
9
10 /****************
11  * General macros
12  *****************/
13
14 /* Cast a pointer to a struct member to a pointer to the containing struct.  */
15 #define container_of(ptr, type, member) \
16         ((type *)((char *)(ptr) - offsetof(type, member)))
17
18 /* Calculate 'n / d', but round up instead of down.  */
19 #define DIV_ROUND_UP(n, d)      (((n) + (d) - 1) / (d))
20
21 /* Calculate 'n % d', but return 'd' if the result would be 0.  */
22 #define MODULO_NONZERO(n, d)    (((n) % (d)) ? ((n) % (d)) : (d))
23
24 /* Get the number of elements of an array type.  */
25 #define ARRAY_LEN(array)        (sizeof(array) / sizeof((array)[0]))
26
27 /* Maximum number of bytes that can be allocated on the stack.
28  *
29  * Note: this isn't a hard bound on the stack space used, since this is just for
30  * individual arrays.  The full call stack could use more than this.  */
31 #define STACK_MAX 32768
32
33 /* Default size of file I/O buffer.  Currently assumed to be <= STACK_MAX.  */
34 #define BUFFER_SIZE 32768
35
36 /*******************
37  * Memory allocation
38  *******************/
39
40 extern void *
41 wimlib_malloc(size_t size) _malloc_attribute;
42
43 extern void
44 wimlib_free_memory(void *p);
45
46 extern void *
47 wimlib_realloc(void *ptr, size_t size);
48
49 extern void *
50 wimlib_calloc(size_t nmemb, size_t size) _malloc_attribute;
51
52 extern char *
53 wimlib_strdup(const char *str) _malloc_attribute;
54
55 #ifdef __WIN32__
56 extern wchar_t *
57 wimlib_wcsdup(const wchar_t *str) _malloc_attribute;
58 #endif
59
60 extern void *
61 wimlib_aligned_malloc(size_t size, size_t alignment) _malloc_attribute;
62
63 extern void
64 wimlib_aligned_free(void *ptr);
65
66 extern void *
67 memdup(const void *mem, size_t size) _malloc_attribute;
68
69 #define MALLOC          wimlib_malloc
70 #define FREE            wimlib_free_memory
71 #define REALLOC         wimlib_realloc
72 #define CALLOC          wimlib_calloc
73 #define STRDUP          wimlib_strdup
74 #define WCSDUP          wimlib_wcsdup
75 #define ALIGNED_MALLOC  wimlib_aligned_malloc
76 #define ALIGNED_FREE    wimlib_aligned_free
77
78 /*******************
79  * String utilities
80  *******************/
81
82 #ifndef HAVE_MEMPCPY
83 extern void *
84 mempcpy(void *dst, const void *src, size_t n);
85 #endif
86
87 extern size_t
88 utf16le_strlen(const utf16lechar *s);
89
90 extern void
91 randomize_byte_array(u8 *p, size_t n);
92
93 extern void
94 randomize_char_array_with_alnum(tchar *p, size_t n);
95
96 /************************
97  * Hashing and comparison
98  ************************/
99
100 static inline bool
101 is_power_of_2(unsigned long n)
102 {
103         return (n != 0 && (n & (n - 1)) == 0);
104
105 }
106
107 static inline u64
108 hash_u64(u64 n)
109 {
110         return n * 0x9e37fffffffc0001ULL;
111 }
112
113 static inline int
114 cmp_u64(u64 n1, u64 n2)
115 {
116         if (n1 < n2)
117                 return -1;
118         if (n1 > n2)
119                 return 1;
120         return 0;
121 }
122
123 #endif /* _WIMLIB_UTIL_H */