lib: Added mblock support for kqueue.
[ashd.git] / lib / utils.h
CommitLineData
f0bbedf7
FT
1#ifndef _UTILS_H
2#define _UTILS_H
3
2b8eb6df 4#include <stdio.h>
90f6e953 5#include <stdarg.h>
04883ca4 6#include <sys/types.h>
90f6e953 7
b16800fa
FT
8#define max(a, b) (((b) > (a))?(b):(a))
9#define min(a, b) (((b) < (a))?(b):(a))
10
f0bbedf7
FT
11#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
12#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
13#define szmalloc(size) memset(smalloc(size), 0, size)
90f6e953 14#define sstrdup(str) ({const char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
f0bbedf7
FT
15#define omalloc(o) ((o) = szmalloc(sizeof(*(o))))
16
17#define bufinit(buf) memset(&(buf), 0, sizeof(buf))
5fc1bf9f 18#define buffree(buf) do { if((buf).b != NULL) {free((buf).b);} bufinit(buf); } while(0)
f0bbedf7 19#define sizebuf(buf, wanted) (_sizebuf((struct buffer *)&(buf), (wanted), sizeof(*((buf).b))))
dee4e642 20#define bufdel(buf, i) (memmove((buf).b + (i), (buf).b + (i) + 1, (--((buf).d) - (i)) * sizeof(*((buf).b))))
f0bbedf7
FT
21#define bufadd(buf, new) \
22do { \
23 _sizebuf((struct buffer *)&(buf), (buf).d + 1, sizeof(*((buf).b))); \
24 (buf).b[(buf).d++] = (new); \
25} while(0)
26#define bufcat(buf, new, size) \
27do { \
28 size_t __bufcat_size__; \
29 __bufcat_size__ = (size); \
30 _sizebuf((struct buffer *)&(buf), (buf).d + __bufcat_size__, sizeof((buf).b)); \
31 memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
32 (buf).d += __bufcat_size__; \
33} while(0)
90f6e953 34#define bufcatstr(buf, str) \
f0bbedf7
FT
35do { \
36 char *__buf__; \
37 __buf__ = (str); \
38 bufcat((buf), __buf__, strlen(__buf__)); \
39} while(0)
90f6e953
FT
40#define bufcatstr2(buf, str) \
41do { \
42 char *__buf__; \
43 __buf__ = (str); \
44 bufcat((buf), __buf__, strlen(__buf__) + 1); \
45} while(0)
46#define bufeat(buf, len) memmove((buf).b, (buf).b + (len), (buf).d -= (len))
f0bbedf7
FT
47
48struct buffer {
49 void *b;
50 size_t s, d;
51};
52
53#define typedbuf(type) struct {type *b; size_t s, d;}
54
55struct charbuf {
56 char *b;
57 size_t s, d;
58};
59
60struct charvbuf {
61 char **b;
62 size_t s, d;
63};
64
59c94590
FT
65struct btree {
66 struct btree *l, *r;
67 int h;
68 void *d;
69};
70
f0bbedf7 71void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
90f6e953
FT
72char *decstr(char **p, size_t *len);
73char *vsprintf2(char *format, va_list al);
74char *sprintf2(char *format, ...);
75char *sprintf3(char *format, ...);
9aee4cbc 76off_t atoo(char *n);
f2b26d44
FT
77char **tokenize(char *src);
78void freeca(char **ca);
d1bd343c 79int calen(char **a);
d422fdfd
FT
80void bvprintf(struct charbuf *buf, char *format, va_list al);
81void bprintf(struct charbuf *buf, char *format, ...);
5fc1bf9f 82void replstr(char **p, char *n);
be6e50bf
FT
83char *base64encode(char *data, size_t datalen);
84char *base64decode(char *data, size_t *datalen);
4dc7f716 85int hexdigit(char c);
59c94590
FT
86int bbtreedel(struct btree **tree, void *item, int (*cmp)(void *, void *));
87void freebtree(struct btree **tree, void (*ffunc)(void *));
88int bbtreeput(struct btree **tree, void *item, int (*cmp)(void *, void *));
89void *btreeget(struct btree *tree, void *key, int (*cmp)(void *, void *));
2b8eb6df
FT
90FILE *funstdio(void *pdata,
91 ssize_t (*read)(void *pdata, void *buf, size_t len),
92 ssize_t (*write)(void *pdata, const void *buf, size_t len),
93 off_t (*seek)(void *pdata, off_t offset, int whence),
94 int (*close)(void *pdata));
f0bbedf7
FT
95
96#endif