]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
lib: added {big,page}_alloc_zero routines
[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 log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3);
103 #define log log_msg
104 void vlog_msg(unsigned int cat, const char *msg, va_list args);
105 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
106 void log_init(byte *argv0);
107 void log_file(byte *name);
108 void log_fork(void);
109 int log_switch(void);
110
111 void assert_failed(char *assertion, char *file, int line) NONRET;
112 void assert_failed_noinfo(void) NONRET;
113
114 #ifdef DEBUG_ASSERTS
115 #define ASSERT(x) ({ if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); 1; })
116 #else
117 #define ASSERT(x) ({ if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); 1; })
118 #endif
119
120 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
121
122 #ifdef LOCAL_DEBUG
123 #define DBG(x,y...) log(L_DEBUG, x,##y)
124 #else
125 #define DBG(x,y...) do { } while(0)
126 #endif
127
128 static inline void log_switch_disable(void) { log_switch_nest++; }
129 static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch_nest--; }
130
131 /* Memory allocation */
132
133 #define xmalloc sh_xmalloc
134 #define xrealloc sh_xrealloc
135 #define xfree sh_xfree
136
137 #ifdef DEBUG_DMALLOC
138 /*
139  * The standard dmalloc macros tend to produce lots of namespace
140  * conflicts and we use only xmalloc and xfree, so we can define
141  * the stubs ourselves.
142  */
143 #define DMALLOC_DISABLE
144 #include <dmalloc.h>
145 #define sh_xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
146 #define sh_xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
147 #define sh_xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
148 #else
149 /*
150  * Unfortunately, several libraries we might want to link to define
151  * their own xmalloc and we don't want to interfere with them, hence
152  * the renaming.
153  */
154 void *xmalloc(unsigned) LIKE_MALLOC;
155 void *xrealloc(void *, unsigned);
156 void xfree(void *);
157 #endif
158
159 void *xmalloc_zero(unsigned) LIKE_MALLOC;
160 byte *xstrdup(byte *) LIKE_MALLOC;
161
162 /* Content-Type pattern matching and filters */
163
164 int match_ct_patt(byte *, byte *);
165
166 /* wordsplit.c */
167
168 int sepsplit(byte *str, byte sep, byte **rec, uns max);
169 int wordsplit(byte *, byte **, uns);
170
171 /* pat(i)match.c: Matching of shell patterns */
172
173 int match_pattern(byte *, byte *);
174 int match_pattern_nocase(byte *, byte *);
175
176 /* md5hex.c */
177
178 void md5_to_hex(byte *, byte *);
179 void hex_to_md5(byte *, byte *);
180
181 #define MD5_SIZE 16
182 #define MD5_HEX_SIZE 33
183
184 /* prime.c */
185
186 int isprime(uns);
187 uns nextprime(uns);
188
189 /* primetable.c */
190
191 uns next_table_prime(uns x);
192 uns prev_table_prime(uns x);
193
194 /* timer.c */
195
196 timestamp_t get_timestamp(void);
197
198 void init_timer(timestamp_t *timer);
199 uns get_timer(timestamp_t *timer);
200 uns switch_timer(timestamp_t *old, timestamp_t *new);
201
202 /* regex.c */
203
204 typedef struct regex regex;
205
206 regex *rx_compile(byte *r, int icase);
207 void rx_free(regex *r);
208 int rx_match(regex *r, byte *s);
209 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
210
211 /* random.c */
212
213 uns random_u32(void);
214 uns random_max(uns max);
215 u64 random_u64(void);
216 u64 random_max_u64(u64 max);
217
218 /* mmap.c */
219
220 void *mmap_file(byte *name, unsigned *len, int writeable);
221 void munmap_file(void *start, unsigned len);
222
223 /* proctitle.c */
224
225 void setproctitle_init(int argc, char **argv);
226 void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2);
227 char *getproctitle(void);
228
229 /* randomkey.c */
230
231 void randomkey(byte *buf, uns size);
232
233 /* exitstatus.c */
234
235 #define EXIT_STATUS_MSG_SIZE 32
236 int format_exit_status(byte *msg, int stat);
237
238 /* runcmd.c */
239
240 int run_command(byte *cmd, ...);
241 void NONRET exec_command(byte *cmd, ...);
242 void echo_command(byte *buf, int size, byte *cmd, ...);
243 int run_command_v(byte *cmd, va_list args);
244 void NONRET exec_command_v(byte *cmd, va_list args);
245 void echo_command_v(byte *buf, int size, byte *cmd, va_list args);
246
247 /* carefulio.c */
248
249 int careful_read(int fd, void *buf, int len);
250 int careful_write(int fd, void *buf, int len);
251
252 /* sync.c */
253
254 void sync_dir(byte *name);
255
256 /* sighandler.c */
257
258 typedef int (*sh_sighandler_t)(int);    // gets signum, returns nonzero if abort() should be called
259
260 void handle_signal(int signum);
261 void unhandle_signal(int signum);
262 sh_sighandler_t set_signal_handler(int signum, sh_sighandler_t new);
263
264 /* string.c */
265
266 byte *str_unesc(byte *dest, byte *src);
267 byte *str_format_flags(byte *dest, const byte *fmt, uns flags);
268
269 /* bigalloc.c */
270
271 void *page_alloc(u64 len) LIKE_MALLOC; // allocates a multiple of CPU_PAGE_SIZE bytes with mmap
272 void *page_alloc_zero(u64 len) LIKE_MALLOC;
273 void page_free(void *start, u64 len);
274 void *page_realloc(void *start, u64 old_len, u64 new_len);
275
276 void *big_alloc(u64 len) LIKE_MALLOC; // allocate a large memory block in the most efficient way available
277 void *big_alloc_zero(u64 len) LIKE_MALLOC;
278 void big_free(void *start, u64 len);
279
280 #endif