]> mj.ucw.cz Git - libucw.git/blob - ucw/lib.h
99b848e348c4d1cd0ce3aa1cab85d2f20d3bf9ee
[libucw.git] / ucw / lib.h
1 /*
2  *      The UCW Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2008 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 "ucw/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) (uintptr_t) 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)) | ((uns)(x) >> (sizeof(uns)*8 - (bits)))) /* Bitwise rotation of an uns to the left */
47 #define ROR(x, bits) (((uns)(x) >> (bits)) | ((x) << (sizeof(uns)*8 - (bits))))
48
49 /* GCC Extensions */
50
51 #ifdef __GNUC__
52
53 #undef inline
54 #define NONRET __attribute__((noreturn))
55 #define UNUSED __attribute__((unused))
56 #define CONSTRUCTOR __attribute__((constructor))
57 #define PACKED __attribute__((packed))
58 #define CONST __attribute__((const))
59 #define PURE __attribute__((pure))
60 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
61 #define likely(x) __builtin_expect((x),1)
62 #define unlikely(x) __builtin_expect((x),0)
63
64 #if __GNUC__ >= 4 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3
65 #define ALWAYS_INLINE inline __attribute__((always_inline))
66 #define NO_INLINE __attribute__((noinline))
67 #else
68 #define ALWAYS_INLINE inline
69 #endif
70
71 #if __GNUC__ >= 4
72 #define LIKE_MALLOC __attribute__((malloc))
73 #define SENTINEL_CHECK __attribute__((sentinel))
74 #else
75 #define LIKE_MALLOC
76 #define SENTINEL_CHECK
77 #endif
78
79 #else
80 #error This program requires the GNU C compiler.
81 #endif
82
83 /* Logging */
84
85 #define L_DEBUG         'D'             /* Debugging messages */
86 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
87 #define L_WARN          'W'
88 #define L_ERROR         'E'
89 #define L_INFO_R        'i'             /* Errors caused by external events */
90 #define L_WARN_R        'w'
91 #define L_ERROR_R       'e'
92 #define L_FATAL         '!'             /* die() */
93
94 #define L_SIGHANDLER    0x10000         /* Avoid operations that are unsafe in signal handlers */
95
96 extern char *log_title;                 /* NULL - print no title, default is program name given to log_init() */
97 extern char *log_filename;              /* Expanded name of the current log file */
98 extern int log_pid;                     /* 0 if shouldn't be logged */
99 extern int log_precise_timings;         /* Include microsecond timestamps in log messages */
100 extern void (*log_die_hook)(void);
101 struct tm;
102 extern void (*log_switch_hook)(struct tm *tm);
103
104 void msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3);
105 void vmsg(uns cat, const char *fmt, va_list args);
106 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
107 void log_init(const char *argv0);
108 void log_file(const char *name);
109 void log_fork(void);                    /* Call after fork() to update log_pid */
110
111 /* If the log name contains metacharacters for date and time, we switch the logs
112  * automatically whenever the name changes. You can disable it and switch explicitly. */
113 int log_switch(void);
114 void log_switch_disable(void);
115 void log_switch_enable(void);
116
117 void assert_failed(const char *assertion, const char *file, int line) NONRET;
118 void assert_failed_noinfo(void) NONRET;
119
120 #ifdef DEBUG_ASSERTS
121 #define ASSERT(x) ({ if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); 1; })
122 #else
123 #define ASSERT(x) ({ if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); 1; })
124 #endif
125
126 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
127
128 #ifdef LOCAL_DEBUG
129 #define DBG(x,y...) msg(L_DEBUG, x,##y)
130 #else
131 #define DBG(x,y...) do { } while(0)
132 #endif
133
134 /* Memory allocation */
135
136 #define xmalloc ucw_xmalloc
137 #define xrealloc ucw_xrealloc
138 #define xfree ucw_xfree
139
140 /*
141  * Unfortunately, several libraries we might want to link to define
142  * their own xmalloc and we don't want to interfere with them, hence
143  * the renaming.
144  */
145 void *xmalloc(uns) LIKE_MALLOC;
146 void *xrealloc(void *, uns);
147 void xfree(void *);
148
149 void *xmalloc_zero(uns) LIKE_MALLOC;
150 char *xstrdup(const char *) LIKE_MALLOC;
151
152 /* timer.c */
153
154 timestamp_t get_timestamp(void);
155
156 void init_timer(timestamp_t *timer);
157 uns get_timer(timestamp_t *timer);
158 uns switch_timer(timestamp_t *oldt, timestamp_t *newt);
159
160 /* random.c */
161
162 uns random_u32(void);
163 uns random_max(uns max);
164 u64 random_u64(void);
165 u64 random_max_u64(u64 max);
166
167 /* mmap.c */
168
169 void *mmap_file(const char *name, unsigned *len, int writeable);
170 void munmap_file(void *start, unsigned len);
171
172 /* proctitle.c */
173
174 void setproctitle_init(int argc, char **argv);
175 void setproctitle(const char *msg, ...) FORMAT_CHECK(printf,1,2);
176 char *getproctitle(void);
177
178 /* randomkey.c */
179
180 void randomkey(byte *buf, uns size);
181
182 /* exitstatus.c */
183
184 #define EXIT_STATUS_MSG_SIZE 32
185 int format_exit_status(char *msg, int stat);
186
187 /* runcmd.c */
188
189 int run_command(const char *cmd, ...);
190 void NONRET exec_command(const char *cmd, ...);
191 void echo_command(char *buf, int size, const char *cmd, ...);
192 int run_command_v(const char *cmd, va_list args);
193 void NONRET exec_command_v(const char *cmd, va_list args);
194 void echo_command_v(char *buf, int size, const char *cmd, va_list args);
195
196 /* carefulio.c */
197
198 int careful_read(int fd, void *buf, int len);
199 int careful_write(int fd, const void *buf, int len);
200
201 /* sync.c */
202
203 void sync_dir(const char *name);
204
205 /* sighandler.c */
206
207 typedef int (*ucw_sighandler_t)(int);   // gets signum, returns nonzero if abort() should be called
208
209 void handle_signal(int signum);
210 void unhandle_signal(int signum);
211 ucw_sighandler_t set_signal_handler(int signum, ucw_sighandler_t newh);
212
213 /* bigalloc.c */
214
215 void *page_alloc(u64 len) LIKE_MALLOC; // allocates a multiple of CPU_PAGE_SIZE bytes with mmap
216 void *page_alloc_zero(u64 len) LIKE_MALLOC;
217 void page_free(void *start, u64 len);
218 void *page_realloc(void *start, u64 old_len, u64 new_len);
219
220 void *big_alloc(u64 len) LIKE_MALLOC; // allocate a large memory block in the most efficient way available
221 void *big_alloc_zero(u64 len) LIKE_MALLOC;
222 void big_free(void *start, u64 len);
223
224 #endif