]> wimlib.net Git - wimlib/blob - src/util.h
Version 1.2.1
[wimlib] / src / util.h
1 #ifndef _WIMLIB_UTIL_H
2 #define _WIMLIB_UTIL_H
3
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <inttypes.h>
8 #include <sys/types.h>
9 #include "config.h"
10
11 #ifdef __GNUC__
12 #       define WIMLIBAPI __attribute__((visibility("default")))
13 #       define ALWAYS_INLINE inline __attribute__((always_inline))
14 #       define PACKED __attribute__((packed))
15 #       define FORMAT(type, format_str, args_start) \
16                         __attribute__((format(type, format_str, args_start)))
17 #       if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
18 #               define COLD     __attribute__((cold))
19 #       else
20 #               define COLD
21 #       endif
22 #else
23 #       define WIMLIBAPI
24 #       define ALWAYS_INLINE inline
25 #       define FORMAT(type, format_str, args_start)
26 #       define COLD
27 #       define PACKED
28 #endif /* __GNUC__ */
29
30
31 #if 0
32 #ifdef WITH_FUSE
33 #define atomic_inc(ptr) \
34         __sync_fetch_and_add(ptr, 1)
35
36 #define atomic_dec(ptr) \
37         __sync_sub_and_fetch(ptr, 1)
38 #endif
39 #endif
40
41 #ifndef _NTFS_TYPES_H
42 typedef uint8_t  u8;
43 typedef uint16_t u16;
44 typedef uint32_t u32;
45 typedef uint64_t u64;
46 #endif
47 typedef unsigned uint;
48
49 #ifndef min
50 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
51                                         (__a < __b) ? __a : __b; })
52 #endif
53
54 #ifndef max
55 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
56                                         (__a > __b) ? __a : __b; })
57 #endif
58
59 #ifndef swap
60 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
61 #endif
62
63 /**
64  * container_of - cast a member of a structure out to the containing structure
65  * @ptr:        the pointer to the member.
66  * @type:       the type of the container struct this is embedded in.
67  * @member:     the name of the member within the struct.
68  *
69  */
70 #ifndef container_of
71 #define container_of(ptr, type, member) ({                      \
72         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
73         (type *)( (char *)__mptr - offsetof(type,member) );})
74 #endif
75
76 #define DIV_ROUND_UP(numerator, denominator) \
77         (((numerator) + (denominator) - 1) / (denominator))
78
79 #define MODULO_NONZERO(numerator, denominator) \
80         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
81
82 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
83
84 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
85
86 /* Used for buffering FILE IO in a few places */
87 #define BUFFER_SIZE 4096
88
89 #ifdef ENABLE_ERROR_MESSAGES
90 extern bool __wimlib_print_errors;
91 extern void wimlib_error(const char *format, ...)
92                 FORMAT(printf, 1, 2) COLD;
93 extern void wimlib_error_with_errno(const char *format, ...)
94                 FORMAT(printf, 1, 2) COLD;
95 extern void wimlib_warning(const char *format, ...)
96                 FORMAT(printf, 1, 2) COLD;
97 #       define ERROR            wimlib_error
98 #       define ERROR_WITH_ERRNO wimlib_error_with_errno
99 #       define WARNING          wimlib_warning
100 #else
101 #       define ERROR(format, ...)
102 #       define ERROR_WITH_ERRNO(format, ...)
103 #       define WARNING(format, ...)
104 #endif /* ENABLE_ERROR_MESSAGES */
105
106 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
107 #       include <errno.h>
108 #       define DEBUG(format, ...)                                       \
109         ({                                                              \
110                 int __errno_save = errno;                               \
111                 fprintf(stdout, "[%s %d] %s(): " format,                \
112                         __FILE__, __LINE__, __func__, ## __VA_ARGS__);  \
113                 putchar('\n');                                          \
114                 fflush(stdout);                                         \
115                 errno = __errno_save;                                   \
116         })
117
118 #else
119 #       define DEBUG(format, ...)
120 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
121
122 #ifdef ENABLE_MORE_DEBUG
123 #       define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
124 #else
125 #       define DEBUG2(format, ...)
126 #endif /* ENABLE_DEBUG */
127
128 #ifdef ENABLE_ASSERTIONS
129 #include <assert.h>
130 #       define wimlib_assert(expr) assert(expr)
131 #else
132 #       define wimlib_assert(expr)
133 #endif
134
135
136 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
137 extern void *(*wimlib_malloc_func)(size_t);
138 extern void (*wimlib_free_func)(void *);
139 extern void *(*wimlib_realloc_func)(void *, size_t);
140 extern void *wimlib_calloc(size_t nmemb, size_t size);
141 extern char *wimlib_strdup(const char *str);
142 #       define  MALLOC  wimlib_malloc_func
143 #       define  FREE    wimlib_free_func
144 #       define  REALLOC wimlib_realloc_func
145 #       define  CALLOC  wimlib_calloc
146 #       define  STRDUP  wimlib_strdup
147 #else
148 #       include <stdlib.h>
149 #       include <string.h>
150 #       define  MALLOC  malloc
151 #       define  FREE    free
152 #       define  REALLOC realloc
153 #       define  CALLOC  calloc
154 #       define  STRDUP  strdup
155 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
156
157
158 /* encoding.c */
159 extern int utf16_to_utf8(const char *utf16_str, size_t utf16_nbytes,
160                          char **utf8_str_ret, size_t *utf8_nbytes_ret);
161
162 extern int utf8_to_utf16(const char *utf8_str, size_t utf8_nbytes,
163                          char **utf16_str_ret, size_t *utf16_nbytes_ret);
164
165 /* util.c */
166 extern void randomize_byte_array(u8 *p, size_t n);
167
168 extern void randomize_char_array_with_alnum(char p[], size_t n);
169
170 extern const char *path_next_part(const char *path,
171                                   size_t *first_part_len_ret);
172
173 extern const char *path_basename(const char *path);
174
175 extern const char *path_stream_name(const char *path);
176
177 extern void to_parent_name(char buf[], size_t len);
178
179 extern void print_string(const void *string, size_t len);
180
181 extern int get_num_path_components(const char *path);
182
183 static inline void print_byte_field(const u8 field[], size_t len)
184 {
185         while (len--)
186                 printf("%02hhx", *field++);
187 }
188
189 static inline u32 bsr32(u32 n)
190 {
191 #if defined(__x86__) || defined(__x86_64__)
192         asm("bsrl %0, %0;"
193                         : "=r"(n)
194                         : "0" (n));
195         return n;
196 #else
197         u32 pow = 0;
198         while ((n >>= 1) != 0)
199                 pow++;
200         return pow;
201 #endif
202 }
203
204 #endif /* _WIMLIB_UTIL_H */