]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
library:
[libucw.git] / lib / lib.h
1 /*
2  *      The UCW Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2005 Tomas Valla <tom@ucw.cz>
6  *      (c) 2006 Robert Spalek <robert@ucw.cz>
7  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
8  *
9  *      This software may be freely distributed and used according to the terms
10  *      of the GNU Lesser General Public License.
11  */
12
13 #ifndef _UCW_LIB_H
14 #define _UCW_LIB_H
15
16 #include "lib/config.h"
17 #include <stdarg.h>
18
19 /* Macros for handling structurues, offsets and alignment */
20
21 #define CHECK_PTR_TYPE(x, type) ((x)-(type)(x) + (type)(x))
22 #define PTR_TO(s, i) &((s*)0)->i
23 #define OFFSETOF(s, i) ((unsigned int) PTR_TO(s, i))
24 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
25 #define ALIGN_TO(s, a) (((s)+a-1)&~(a-1))
26 #define ALIGN_PTR(p, s) ((uintptr_t)(p) % (s) ? (typeof(p))((uintptr_t)(p) + (s) - (uintptr_t)(p) % (s)) : (p))
27 #define UNALIGNED_PART(ptr, type) (((uintptr_t) (ptr)) % sizeof(type))
28
29 /* Some other macros */
30
31 #define MIN(a,b) (((a)<(b))?(a):(b))
32 #define MAX(a,b) (((a)>(b))?(a):(b))
33 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
34 #define ABS(x) ((x) < 0 ? -(x) : (x))
35 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
36 #define STRINGIFY(x) #x
37 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
38 #define GLUE(x,y) x##y
39 #define GLUE_(x,y) x##_##y
40
41 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
42 #define REV_COMPARE(x,y) COMPARE(y,x)
43 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
44 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
45
46 #define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits))))      /* Bitwise rotation of an uns to the left */
47
48 /* GCC Extensions */
49
50 #ifdef __GNUC__
51
52 #undef inline
53 #define NONRET __attribute__((noreturn))
54 #define UNUSED __attribute__((unused))
55 #define CONSTRUCTOR __attribute__((constructor))
56 #define PACKED __attribute__((packed))
57 #define CONST __attribute__((const))
58 #define PURE __attribute__((pure))
59 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
60 #define likely(x) __builtin_expect((x),1)
61 #define unlikely(x) __builtin_expect((x),0)
62
63 #if __GNUC__ >= 4 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3
64 #define ALWAYS_INLINE inline __attribute__((always_inline))
65 #define NO_INLINE __attribute__((noinline))
66 #else
67 #define ALWAYS_INLINE inline
68 #endif
69
70 #if __GNUC__ >= 4
71 #define LIKE_MALLOC __attribute__((malloc))
72 #define SENTINEL_CHECK __attribute__((sentinel))
73 #else
74 #define LIKE_MALLOC
75 #define SENTINEL_CHECK
76 #endif
77
78 #else
79 #error This program requires the GNU C compiler.
80 #endif
81
82 /* Logging */
83
84 #define L_DEBUG         'D'             /* Debugging messages */
85 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
86 #define L_WARN          'W'
87 #define L_ERROR         'E'
88 #define L_INFO_R        'i'             /* Errors caused by external events */
89 #define L_WARN_R        'w'
90 #define L_ERROR_R       'e'
91 #define L_FATAL         '!'             /* die() */
92
93 extern char *log_title;                 /* NULL - print no title, default is log_progname */
94 extern char *log_filename;              /* Expanded name of the current log file */
95 extern volatile int log_switch_nest;    /* log_switch() nesting counter, increment to disable automatic switches */
96 extern int log_pid;                     /* 0 if shouldn't be logged */
97 extern int log_precise_timings;         /* Include microsecond timestamps in log messages */
98 extern void (*log_die_hook)(void);
99 struct tm;
100 extern void (*log_switch_hook)(struct tm *tm);
101
102 void msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3);
103 void vmsg(uns cat, const char *fmt, va_list args);
104 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
105 void log_init(const char *argv0);
106 void log_file(const char *name);
107 void log_fork(void);
108 int log_switch(void);
109
110 void assert_failed(const char *assertion, const char *file, int line) NONRET;
111 void assert_failed_noinfo(void) NONRET;
112
113 #ifdef DEBUG_ASSERTS
114 #define ASSERT(x) ({ if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); 1; })
115 #else
116 #define ASSERT(x) ({ if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); 1; })
117 #endif
118
119 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
120
121 #ifdef LOCAL_DEBUG
122 #define DBG(x,y...) msg(L_DEBUG, x,##y)
123 #else
124 #define DBG(x,y...) do { } while(0)
125 #endif
126
127 static inline void log_switch_disable(void) { log_switch_nest++; }
128 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
129
130 /* Memory allocation */
131
132 #define xmalloc sh_xmalloc
133 #define xrealloc sh_xrealloc
134 #define xfree sh_xfree
135
136 #ifdef DEBUG_DMALLOC
137 /*
138  * The standard dmalloc macros tend to produce lots of namespace
139  * conflicts and we use only xmalloc and xfree, so we can define
140  * the stubs ourselves.
141  */
142 #define DMALLOC_DISABLE
143 #include <dmalloc.h>
144 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
145 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
146 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
147 #else
148 /*
149  * Unfortunately, several libraries we might want to link to define
150  * their own xmalloc and we don't want to interfere with them, hence
151  * the renaming.
152  */
153 void *xmalloc(uns) LIKE_MALLOC;
154 void *xrealloc(void *, uns);
155 void xfree(void *);
156 #endif
157
158 void *xmalloc_zero(uns) LIKE_MALLOC;
159 char *xstrdup(const char *) LIKE_MALLOC;
160
161 /* Content-Type pattern matching and filters */
162
163 int match_ct_patt(const char *, const char *);
164
165 /* wordsplit.c */
166
167 int sepsplit(char *str, uns sep, char **rec, uns max);
168 int wordsplit(char *str, char **rec, uns max);
169
170 /* pat(i)match.c: Matching of shell patterns */
171
172 int match_pattern(const char *patt, const char *str);
173 int match_pattern_nocase(const char *patt, const char *str);
174
175 /* md5hex.c */
176
177 void md5_to_hex(const byte *s, char *d);
178 void hex_to_md5(const char *s, byte *d);
179
180 #define MD5_SIZE 16
181 #define MD5_HEX_SIZE 33
182
183 /* prime.c */
184
185 int isprime(uns x);
186 uns nextprime(uns x);
187
188 /* primetable.c */
189
190 uns next_table_prime(uns x);
191 uns prev_table_prime(uns x);
192
193 /* timer.c */
194
195 struct timeval;
196
197 void init_timer(void);
198 uns get_timer(void);
199 void get_last_timeval(struct timeval *tv);
200
201 /* regex.c */
202
203 typedef struct regex regex;
204
205 regex *rx_compile(const char *r, int icase);
206 void rx_free(regex *r);
207 int rx_match(regex *r, const char *s);
208 int rx_subst(regex *r, const char *by, const char *src, char *dest, uns destlen);
209
210 /* random.c */
211
212 uns random_u32(void);
213 uns random_max(uns max);
214 u64 random_u64(void);
215 u64 random_max_u64(u64 max);
216
217 /* mmap.c */
218
219 void *mmap_file(const char *name, unsigned *len, int writeable);
220 void munmap_file(void *start, unsigned len);
221
222 /* proctitle.c */
223
224 void setproctitle_init(int argc, char **argv);
225 void setproctitle(const char *msg, ...) FORMAT_CHECK(printf,1,2);
226 char *getproctitle(void);
227
228 /* randomkey.c */
229
230 void randomkey(byte *buf, uns size);
231
232 /* exitstatus.c */
233
234 #define EXIT_STATUS_MSG_SIZE 32
235 int format_exit_status(char *msg, int stat);
236
237 /* runcmd.c */
238
239 int run_command(const char *cmd, ...);
240 void NONRET exec_command(const char *cmd, ...);
241 void echo_command(char *buf, int size, const char *cmd, ...);
242 int run_command_v(const char *cmd, va_list args);
243 void NONRET exec_command_v(const char *cmd, va_list args);
244 void echo_command_v(char *buf, int size, const char *cmd, va_list args);
245
246 /* carefulio.c */
247
248 int careful_read(int fd, void *buf, int len);
249 int careful_write(int fd, const void *buf, int len);
250
251 /* sync.c */
252
253 void sync_dir(const char *name);
254
255 /* sighandler.c */
256
257 typedef int (*sh_sighandler_t)(int);    // gets signum, returns nonzero if abort() should be called
258
259 void handle_signal(int signum);
260 void unhandle_signal(int signum);
261 sh_sighandler_t set_signal_handler(int signum, sh_sighandler_t new);
262
263 /* string.c */
264
265 char *str_unesc(char *dest, const char *src);
266 char *str_format_flags(char *dest, const char *fmt, uns flags);
267
268 /* bigalloc.c */
269
270 void *page_alloc(unsigned int len) LIKE_MALLOC; // allocates a multiple of CPU_PAGE_SIZE bytes with mmap
271 void page_free(void *start, unsigned int len);
272 void *page_realloc(void *start, unsigned int old_len, unsigned int new_len);
273
274 void *big_alloc(unsigned int len) LIKE_MALLOC; // allocate a large memory block in the most efficient way available
275 void big_free(void *start, unsigned int len);
276
277 #endif