]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
Don't create large bit arrays on stack. (The default stack limit on Linux is 2MB.)
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock 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 /*
11  *  This file should be included as the very first include in all
12  *  source files, especially before all OS includes since it sets
13  *  up libc feature macros.
14  */
15
16 #ifndef _SHERLOCK_LIB_H
17 #define _SHERLOCK_LIB_H
18
19 #include "lib/config.h"
20
21 /* Tell libc we're going to use all extensions available */
22
23 #define _GNU_SOURCE
24
25 /* Ugly structure handling macros */
26
27 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
28 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
29 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
30 #define UNALIGNED_PART(ptr, type) (((long) (ptr)) % sizeof(type))
31
32 /* Some other macros */
33
34 #define MIN(a,b) (((a)<(b))?(a):(b))
35 #define MAX(a,b) (((a)>(b))?(a):(b))
36 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
37 #define ABS(x) ((x) < 0 ? -(x) : (x))
38 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
39 #define GLUE(x,y) x##y
40 #define GLUE_(x,y) x##_##y
41
42 /* Logging */
43
44 #define L_DEBUG         'D'             /* Debugging messages */
45 #define L_INFO          'I'             /* Informational msgs, warnings and errors */
46 #define L_WARN          'W'
47 #define L_ERROR         'E'
48 #define L_INFO_R        'i'             /* Errors caused by external events */
49 #define L_WARN_R        'w'
50 #define L_ERROR_R       'e'
51 #define L_FATAL         '!'             /* die() */
52
53 extern char *log_title;                 /* NULL - print no title, default is log_progname */
54
55 void log_msg(unsigned int cat, const char *msg, ...) __attribute__((format(printf,2,3)));
56 #define log log_msg
57 void die(byte *, ...) NONRET;
58 void log_init(byte *);
59 void log_file(byte *);
60 void log_fork(void);
61
62 #ifdef DEBUG
63 void assert_failed(char *assertion, char *file, int line) NONRET;
64 #define ASSERT(x) do { if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); } while(0)
65 #else
66 void assert_failed(void) NONRET;
67 #define ASSERT(x) do { if (__builtin_constant_p(x) && !(x)) assert_failed(); } while(0)
68 #endif
69
70 #ifdef LOCAL_DEBUG
71 #define DBG(x,y...) log(L_DEBUG, x,##y)
72 #else
73 #define DBG(x,y...) do { } while(0)
74 #endif
75
76 /* Memory allocation */
77
78 #ifdef DEBUG_DMALLOC
79 /*
80  * The standard dmalloc macros tend to produce lots of namespace
81  * conflicts and we use only xmalloc and xfree, so we can define
82  * the stubs ourselves.
83  */
84 #define DMALLOC_DISABLE
85 #include <dmalloc.h>
86 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
87 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
88 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
89 #else
90 /*
91  * Unfortunately, several libraries we might want to link to define
92  * their own xmalloc and we don't want to interfere with them, hence
93  * the renaming.
94  */
95 #define xmalloc sh_xmalloc
96 void *xmalloc(unsigned);
97 void *xrealloc(void *, unsigned);
98 #define xfree(x) free(x)
99 #endif
100
101 void *xmalloc_zero(unsigned);
102 byte *stralloc(byte *);
103
104 /* Content-Type pattern matching and filters */
105
106 int match_ct_patt(byte *, byte *);
107
108 /* log2.c */
109
110 int fls(u32);
111
112 /* wordsplit.c */
113
114 int wordsplit(byte *, byte **, uns);
115
116 /* pat(i)match.c: Matching of shell patterns */
117
118 int match_pattern(byte *, byte *);
119 int match_pattern_nocase(byte *, byte *);
120
121 /* md5hex.c */
122
123 void md5_to_hex(byte *, byte *);
124 void hex_to_md5(byte *, byte *);
125
126 #define MD5_SIZE 16
127 #define MD5_HEX_SIZE 33
128
129 /* prime.c */
130
131 int isprime(uns);
132 uns nextprime(uns);
133
134 /* timer.c */
135
136 void init_timer(void);
137 uns get_timer(void);
138
139 /* regex.c */
140
141 typedef struct regex regex;
142
143 regex *rx_compile(byte *r, int icase);
144 void rx_free(regex *r);
145 int rx_match(regex *r, byte *s);
146 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
147
148 /* random.c */
149
150 uns random_max(uns);
151
152 /* mmap.c */
153
154 void *mmap_file(byte *name, unsigned *len, int writeable);
155 void munmap_file(void *start, unsigned len);
156
157 /* partmap.c */
158
159 struct partmap;
160 struct partmap *partmap_open(byte *name, int writeable);
161 void *partmap_map(struct partmap *p, sh_off_t offset, uns size);
162 void partmap_close(struct partmap *p);
163 sh_off_t partmap_size(struct partmap *p);
164
165 /* proctitle.c */
166
167 void setproctitle_init(int argc, char **argv);
168 void setproctitle(char *msg, ...) __attribute__((format(printf,1,2)));
169
170 /* randomkey.c */
171
172 void randomkey(byte *buf, uns size);
173
174 #endif