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