]> wimlib.net Git - wimlib/blob - include/wimlib/util.h
v1.14.4
[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 /* Round 'v' up to the next 'alignment'-byte aligned boundary.  'alignment' must
28  * be a power of 2.  */
29 #undef ALIGN    /* NetBSD <sys/param.h> defines this already */
30 #define ALIGN(v, alignment)     (((v) + ((alignment) - 1)) & ~((alignment) - 1))
31
32 /* Maximum number of bytes that can be allocated on the stack.
33  *
34  * Note: this isn't a hard bound on the stack space used, since this is just for
35  * individual arrays.  The full call stack could use more than this.  */
36 #define STACK_MAX 32768
37
38 /* Default size of file I/O buffer.  Currently assumed to be <= STACK_MAX.  */
39 #define BUFFER_SIZE 32768
40
41 /*******************
42  * Memory allocation
43  *******************/
44
45 void *
46 wimlib_malloc(size_t size);
47
48 void
49 wimlib_free_memory(void *p);
50
51 void *
52 wimlib_realloc(void *ptr, size_t size);
53
54 void *
55 wimlib_calloc(size_t nmemb, size_t size);
56
57 char *
58 wimlib_strdup(const char *str);
59
60 #ifdef _WIN32
61 wchar_t *
62 wimlib_wcsdup(const wchar_t *str);
63 #endif
64
65 void *
66 wimlib_aligned_malloc(size_t size, size_t alignment);
67
68 void
69 wimlib_aligned_free(void *ptr);
70
71 void *
72 memdup(const void *mem, size_t size);
73
74 #define MALLOC          wimlib_malloc
75 #define FREE            wimlib_free_memory
76 #define REALLOC         wimlib_realloc
77 #define CALLOC          wimlib_calloc
78 #define STRDUP          wimlib_strdup
79 #define WCSDUP          wimlib_wcsdup
80 #define ALIGNED_MALLOC  wimlib_aligned_malloc
81 #define ALIGNED_FREE    wimlib_aligned_free
82
83 /*******************
84  * String utilities
85  *******************/
86
87 #ifndef HAVE_MEMPCPY
88 void *
89 mempcpy(void *dst, const void *src, size_t n);
90 #endif
91
92 /**************************
93  * Random number generation
94  **************************/
95
96 void
97 get_random_bytes(void *p, size_t n);
98
99 void
100 get_random_alnum_chars(tchar *p, size_t n);
101
102 /************************
103  * Hashing and comparison
104  ************************/
105
106 static inline bool
107 is_power_of_2(unsigned long n)
108 {
109         return (n != 0 && (n & (n - 1)) == 0);
110
111 }
112
113 static inline u64
114 hash_u64(u64 n)
115 {
116         return n * 0x9e37fffffffc0001ULL;
117 }
118
119 static inline int
120 cmp_u32(u32 n1, u32 n2)
121 {
122         if (n1 < n2)
123                 return -1;
124         if (n1 > n2)
125                 return 1;
126         return 0;
127 }
128
129 static inline int
130 cmp_u64(u64 n1, u64 n2)
131 {
132         if (n1 < n2)
133                 return -1;
134         if (n1 > n2)
135                 return 1;
136         return 0;
137 }
138
139 /************************
140  * System information
141  ************************/
142
143 unsigned
144 get_available_cpus(void);
145
146 u64
147 get_available_memory(void);
148
149 #endif /* _WIMLIB_UTIL_H */