]> wimlib.net Git - wimlib/blob - include/wimlib/util.h
177e1190f94a029df32a05e2eb4758a509b2a113
[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 #include <stddef.h>
9
10 #ifndef min
11 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
12                                         (__a < __b) ? __a : __b; })
13 #endif
14
15 #ifndef max
16 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
17                                         (__a > __b) ? __a : __b; })
18 #endif
19
20 #ifndef swap
21 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
22 #endif
23
24 /**
25  * container_of - cast a member of a structure out to the containing structure
26  * @ptr:        the pointer to the member.
27  * @type:       the type of the container struct this is embedded in.
28  * @member:     the name of the member within the struct.
29  *
30  */
31 #ifndef container_of
32 #define container_of(ptr, type, member) ({                      \
33         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
34         (type *)( (char *)__mptr - offsetof(type,member) );})
35 #endif
36
37 #define DIV_ROUND_UP(numerator, denominator) \
38         (((numerator) + (denominator) - 1) / (denominator))
39
40 #define MODULO_NONZERO(numerator, denominator) \
41         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
42
43 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
44
45 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
46
47 /* Used for buffering FILE IO in a few places */
48 #define BUFFER_SIZE 32768
49
50 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
51
52 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
53 extern void *(*wimlib_malloc_func)(size_t);
54 extern void (*wimlib_free_func)(void *);
55 extern void *(*wimlib_realloc_func)(void *, size_t);
56 extern void *wimlib_calloc(size_t nmemb, size_t size);
57 #ifdef __WIN32__
58 extern wchar_t *wimlib_wcsdup(const wchar_t *str);
59 #endif
60 extern char *wimlib_strdup(const char *str);
61 #  define       MALLOC  wimlib_malloc_func
62 #  define       FREE    wimlib_free_func
63 #  define       REALLOC wimlib_realloc_func
64 #  define       CALLOC  wimlib_calloc
65 #  define       STRDUP  wimlib_strdup
66 #  define       WSTRDUP wimlib_wcsdup
67 #else /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
68 #  include <stdlib.h>
69 #  include <string.h>
70 #  define       MALLOC  malloc
71 #  define       FREE    free
72 #  define       REALLOC realloc
73 #  define       CALLOC  calloc
74 #  define       STRDUP  strdup
75 #  define       WSTRDUP wcsdup
76 #endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */
77
78
79 /* util.c */
80 extern void
81 randomize_byte_array(u8 *p, size_t n);
82
83 extern void
84 randomize_char_array_with_alnum(tchar p[], size_t n);
85
86 extern void
87 print_byte_field(const u8 field[], size_t len, FILE *out);
88
89 static inline u32
90 bsr32(u32 n)
91 {
92 #if defined(__x86__) || defined(__x86_64__)
93         asm("bsrl %0, %0;"
94                         : "=r"(n)
95                         : "0" (n));
96         return n;
97 #else
98         u32 pow = 0;
99         while ((n >>= 1) != 0)
100                 pow++;
101         return pow;
102 #endif
103 }
104
105 static inline u64
106 hash_u64(u64 n)
107 {
108         return n * 0x9e37fffffffc0001ULL;
109 }
110
111 #endif /* _WIMLIB_UTIL_H */