]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
useful macro to align pointer p to size s
[libucw.git] / lib / lib.h
1 /*
2  *      The UCW Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_LIB_H
11 #define _UCW_LIB_H
12
13 #include "lib/config.h"
14 #include <stdarg.h>
15
16 /* Tell libc we're going to use all extensions available */
17
18 #define _GNU_SOURCE
19
20 /* Ugly structure handling macros */
21
22 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
23 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
24 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
25 #define ALIGN_PTR(p, s) ((typeof(p)) ((addr_int_t)(p) + (s) - (addr_int_t)(p) % (s)))
26 #define UNALIGNED_PART(ptr, type) (((long) (ptr)) % sizeof(type))
27
28 /* Some other macros */
29
30 #define MIN(a,b) (((a)<(b))?(a):(b))
31 #define MAX(a,b) (((a)>(b))?(a):(b))
32 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
33 #define ABS(x) ((x) < 0 ? -(x) : (x))
34 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
35 #define STRINGIFY(x) #x
36 #define GLUE(x,y) x##y
37 #define GLUE_(x,y) x##_##y
38
39 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
40 #define REV_COMPARE(x,y) COMPARE(y,x)
41 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
42 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
43
44 /* GCC Extensions */
45
46 #ifdef __GNUC__
47
48 #undef inline
49 #define NONRET __attribute__((noreturn))
50 #define UNUSED __attribute__((unused))
51 #define CONSTRUCTOR __attribute__((constructor))
52 #define PACKED __attribute__((packed))
53 #define CONST __attribute__((const))
54 #define PURE __attribute__((const))
55 #define likely(x) __builtin_expect((x),1)
56 #define unlikely(x) __builtin_expect((x),0)
57
58 #else
59 #error This program requires the GNU C compiler.
60 #endif
61
62 /* Logging */
63
64 #define L_DEBUG         'D'             /* Debugging messages */
65 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
66 #define L_WARN          'W'
67 #define L_ERROR         'E'
68 #define L_INFO_R        'i'             /* Errors caused by external events */
69 #define L_WARN_R        'w'
70 #define L_ERROR_R       'e'
71 #define L_FATAL         '!'             /* die() */
72
73 extern char *log_title;                 /* NULL - print no title, default is log_progname */
74 extern char *log_filename;              /* Expanded name of the current log file */
75 extern int log_switch_nest;             /* log_switch() nesting counter, increment to disable automatic switches */
76 extern int log_pid;                     /* 0 if shouldn't be logged */
77 extern void (*log_die_hook)(void);
78 struct tm;
79 extern void (*log_switch_hook)(struct tm *tm);
80
81 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
82 #define log log_msg
83 void vlog_msg(unsigned int cat, const char *msg, va_list args);
84 void die(byte *, ...) NONRET;
85 void log_init(byte *argv0);
86 void log_file(byte *name);
87 void log_fork(void);
88 void log_switch(void);
89
90 void assert_failed(char *assertion, char *file, int line) NONRET;
91 void assert_failed_noinfo(void) NONRET;
92
93 #ifdef DEBUG_ASSERTS
94 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
95 #else
96 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); } while(0)
97 #endif
98
99 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
100
101 #ifdef LOCAL_DEBUG
102 #define DBG(x,y...) log(L_DEBUG, x,##y)
103 #else
104 #define DBG(x,y...) do { } while(0)
105 #endif
106
107 static inline void log_switch_disable(void) { log_switch_nest++; }
108 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
109
110 /* Memory allocation */
111
112 #define xmalloc sh_xmalloc
113 #define xrealloc sh_xrealloc
114 #define xfree sh_xfree
115
116 #ifdef DEBUG_DMALLOC
117 /*
118  * The standard dmalloc macros tend to produce lots of namespace
119  * conflicts and we use only xmalloc and xfree, so we can define
120  * the stubs ourselves.
121  */
122 #define DMALLOC_DISABLE
123 #include <dmalloc.h>
124 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
125 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
126 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
127 #else
128 /*
129  * Unfortunately, several libraries we might want to link to define
130  * their own xmalloc and we don't want to interfere with them, hence
131  * the renaming.
132  */
133 void *xmalloc(unsigned);
134 void *xrealloc(void *, unsigned);
135 #define sh_xfree(x) free(x)
136 #endif
137
138 void *xmalloc_zero(unsigned);
139 byte *xstrdup(byte *);
140
141 /* Content-Type pattern matching and filters */
142
143 int match_ct_patt(byte *, byte *);
144
145 /* log2.c */
146
147 int fls(u32);
148
149 /* wordsplit.c */
150
151 int sepsplit(byte *str, byte sep, byte **rec, uns max);
152 int wordsplit(byte *, byte **, uns);
153
154 /* pat(i)match.c: Matching of shell patterns */
155
156 int match_pattern(byte *, byte *);
157 int match_pattern_nocase(byte *, byte *);
158
159 /* md5hex.c */
160
161 void md5_to_hex(byte *, byte *);
162 void hex_to_md5(byte *, byte *);
163
164 #define MD5_SIZE 16
165 #define MD5_HEX_SIZE 33
166
167 /* prime.c */
168
169 int isprime(uns);
170 uns nextprime(uns);
171
172 /* timer.c */
173
174 void init_timer(void);
175 uns get_timer(void);
176
177 /* regex.c */
178
179 typedef struct regex regex;
180
181 regex *rx_compile(byte *r, int icase);
182 void rx_free(regex *r);
183 int rx_match(regex *r, byte *s);
184 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
185
186 /* random.c */
187
188 uns random_max(uns);
189
190 /* mmap.c */
191
192 void *mmap_file(byte *name, unsigned *len, int writeable);
193 void munmap_file(void *start, unsigned len);
194
195 /* partmap.c */
196
197 struct partmap;
198 struct partmap *partmap_open(byte *name, int writeable);
199 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
200 void partmap_close(struct partmap *p);
201 sh_off_t partmap_size(struct partmap *p);
202
203 /* proctitle.c */
204
205 void setproctitle_init(int argc, char **argv);
206 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
207
208 /* randomkey.c */
209
210 void randomkey(byte *buf, uns size);
211
212 /* exitstatus.c */
213
214 #define EXIT_STATUS_MSG_SIZE 32
215 int format_exit_status(byte *msg, int stat);
216
217 /* runcmd.c */
218
219 int run_command(byte *cmd, ...);
220 void NONRET exec_command(byte *cmd, ...);
221 void echo_command(byte *buf, int size, byte *cmd, ...);
222 int run_command_v(byte *cmd, va_list args);
223 void NONRET exec_command_v(byte *cmd, va_list args);
224 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
225
226 /* carefulio.c */
227
228 int careful_read(int fd, void *buf, int len);
229 int careful_write(int fd, void *buf, int len);
230
231 /* sync.c */
232
233 void sync_dir(byte *name);
234
235 /* sighandler.c */
236
237 typedef int (*sh_sighandler_t)(int);
238   /* obtains signum, returns nonzero if abort() should be called */
239 extern sh_sighandler_t signal_handler[];
240
241 struct sigaction;
242 void handle_signal(int signum, struct sigaction *oldact);
243 void unhandle_signal(int signum, struct sigaction *oldact);
244
245 #endif