]> jfr.im git - solanum.git/blame - librb/include/rb_lib.h
librb: linebuf: reduce the number of "put" implementations from 4 to 1
[solanum.git] / librb / include / rb_lib.h
CommitLineData
db137867
AC
1#ifndef RB_LIB_H
2#define RB_LIB_H 1
3
4#include <librb-config.h>
5#include <stdio.h>
6#include <limits.h>
7#include <stdarg.h>
8#include <assert.h>
9#include <fcntl.h>
10#include <signal.h>
11#include <ctype.h>
12
13#ifdef __GNUC__
14#undef alloca
15#define alloca __builtin_alloca
16#else
17# ifdef _MSC_VER
18# include <malloc.h>
19# define alloca _alloca
20# else
21# if RB_HAVE_ALLOCA_H
22# include <alloca.h>
23# else
24# ifdef _AIX
25#pragma alloca
26# else
27# ifndef alloca /* predefined by HP cc +Olibcalls */
28char *alloca();
29# endif
30# endif
31# endif
32# endif
33#endif /* __GNUC__ */
34
35#ifdef __GNUC__
36
c2ac22cc
VY
37#ifdef rb_likely
38#undef rb_likely
db137867 39#endif
c2ac22cc
VY
40#ifdef rb_unlikely
41#undef rb_unlikely
db137867
AC
42#endif
43
44#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
45# define __builtin_expect(x, expected_value) (x)
46#endif
47
c2ac22cc
VY
48#define rb_likely(x) __builtin_expect(!!(x), 1)
49#define rb_unlikely(x) __builtin_expect(!!(x), 0)
db137867
AC
50
51#else /* !__GNUC__ */
52
53#define UNUSED(x) x
54
c2ac22cc
VY
55#ifdef rb_likely
56#undef rb_likely
db137867 57#endif
c2ac22cc
VY
58#ifdef rb_unlikely
59#undef rb_unlikely
db137867 60#endif
c2ac22cc
VY
61#define rb_likely(x) (x)
62#define rb_unlikely(x) (x)
db137867
AC
63#endif
64
17e4e6af
AC
65#ifdef _WIN32
66#define rb_get_errno() do { errno = WSAGetLastError(); WSASetLastError(errno); } while(0)
67typedef SOCKET rb_platform_fd_t;
ac2f2189 68#define RB_PATH_SEPARATOR '\\'
17e4e6af
AC
69#else
70#define rb_get_errno()
71typedef int rb_platform_fd_t;
ac2f2189 72#define RB_PATH_SEPARATOR '/'
17e4e6af 73#endif
db137867 74
3202e249 75#ifdef _WIN32
db137867
AC
76#include <process.h>
77
d74fa5b5
JT
78#ifndef PATH_MAX
79#define PATH_MAX 128
db137867
AC
80#endif
81
db137867
AC
82#ifdef strerror
83#undef strerror
84#endif
85
3202e249
VY
86#define strerror(x) rb_strerror(x)
87char *rb_strerror(int error);
db137867 88
38b68802 89#undef ENOBUFS
db137867 90#define ENOBUFS WSAENOBUFS
b68dee9b 91
38b68802 92#undef EINPROGRESS
db137867 93#define EINPROGRESS WSAEINPROGRESS
b68dee9b 94
38b68802 95#undef EWOULDBLOCK
db137867 96#define EWOULDBLOCK WSAEWOULDBLOCK
b68dee9b 97
38b68802 98#undef EMSGSIZE
db137867 99#define EMSGSIZE WSAEMSGSIZE
b68dee9b 100
38b68802 101#undef EALREADY
db137867 102#define EALREADY WSAEALREADY
b68dee9b 103
38b68802 104#undef EISCONN
db137867 105#define EISCONN WSAEISCONN
b68dee9b 106
38b68802 107#undef EADDRINUSE
db137867 108#define EADDRINUSE WSAEADDRINUSE
b68dee9b 109
38b68802 110#undef EAFNOSUPPORT
db137867
AC
111#define EAFNOSUPPORT WSAEAFNOSUPPORT
112
113#define pipe(x) _pipe(x, 1024, O_BINARY)
491b3b39 114#define ioctl(x,y,z) ioctlsocket(x,y, (unsigned long *)z)
db137867 115
db137867 116#define WNOHANG 1
db137867
AC
117
118#ifndef SIGKILL
119#define SIGKILL SIGTERM
120#endif
121
3202e249 122#endif /* _WIN32 */
db137867
AC
123
124
125
126#ifndef HOSTIPLEN
127#define HOSTIPLEN 53
128#endif
129
7a06833f
SA
130
131/* For those unfamiliar with GNU format attributes, a is the 1 based
132 * argument number of the format string, and b is the 1 based argument
133 * number of the variadic ... */
134#ifdef __GNUC__
135#define AFP(a,b) __attribute__((format (printf, a, b)))
136#else
137#define AFP(a,b)
138#endif
139
140
db137867 141#ifdef __GNUC__
b5ad4cdf
SA
142#define slrb_assert(expr) ( \
143 rb_likely((expr)) || ( \
c2ac22cc 144 rb_lib_log( \
db137867 145 "file: %s line: %d (%s): Assertion failed: (%s)", \
b5ad4cdf
SA
146 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr), 0) \
147 )
db137867 148#else
b5ad4cdf
SA
149#define slrb_assert(expr) ( \
150 rb_likely((expr)) || ( \
29c92cf9 151 rb_lib_log( \
db137867 152 "file: %s line: %d: Assertion failed: (%s)", \
b5ad4cdf
SA
153 __FILE__, __LINE__, #expr), 0) \
154 )
db137867 155#endif
3202e249 156
b5ad4cdf 157/* evaluates to true if assertion fails */
3202e249 158#ifdef SOFT_ASSERT
b5ad4cdf 159#define lrb_assert(expr) (!slrb_assert(expr))
db137867 160#else
b5ad4cdf 161#define lrb_assert(expr) (assert(slrb_assert(expr)), 0)
db137867
AC
162#endif
163
9d9a4f60 164#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867
AC
165#define ss_len sa_len
166#endif
167
3202e249
VY
168#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
169#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
9d9a4f60 170#ifdef RB_SOCKADDR_HAS_SA_LEN
db137867 171#define SET_SS_LEN(x, y) do { \
d86692fa
EM
172 struct sockaddr *_storage; \
173 _storage = ((struct sockaddr *)(x));\
174 _storage->sa_len = (y); \
db137867
AC
175 } while (0)
176#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
9d9a4f60 177#else /* !RB_SOCKADDR_HAS_SA_LEN */
db137867
AC
178#define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
179#ifdef RB_IPV6
180#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
181#else
182#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : 0)
183#endif
184#endif
185
d86692fa
EM
186#ifdef RB_IPV6
187#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
188#define SET_SS_PORT(x, y) do { \
189 if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
190 ((struct sockaddr_in *)(x))->sin_port = (y); \
191 } else { \
192 ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
193 } \
194 } while (0)
195#else
196#define GET_SS_PORT(x) (((struct sockaddr_in *)(x))->sin_port)
197#define SET_SS_PORT(x, y) (((struct sockaddr_in *)(x))->sin_port = y)
198#endif
199
db137867
AC
200#ifndef INADDRSZ
201#define INADDRSZ 4
202#endif
203
204#ifndef IN6ADDRSZ
205#define IN6ADDRSZ 16
206#endif
207
208#ifndef INT16SZ
209#define INT16SZ 2
210#endif
211
41aed6bb
SA
212#ifndef UINT16_MAX
213#define UINT16_MAX (65535U)
214#endif
215
7c7cf006
SA
216#ifndef UINT32_MAX
217#define UINT32_MAX (4294967295U)
218#endif
219
db137867
AC
220
221typedef void log_cb(const char *buffer);
222typedef void restart_cb(const char *buffer);
223typedef void die_cb(const char *buffer);
224
225char *rb_ctime(const time_t, char *, size_t);
226char *rb_date(const time_t, char *, size_t);
227void rb_lib_log(const char *, ...);
92706fd5 228void rb_lib_restart(const char *, ...) __attribute__((noreturn));
db137867
AC
229void rb_lib_die(const char *, ...);
230void rb_set_time(void);
231const char *rb_lib_version(void);
232
233void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
234 size_t dh_size, size_t fd_heap_size);
92706fd5 235void rb_lib_loop(long delay) __attribute__((noreturn));
db137867
AC
236
237time_t rb_current_time(void);
238const struct timeval *rb_current_time_tv(void);
239pid_t rb_spawn_process(const char *, const char **);
240
241char *rb_strtok_r(char *, const char *, char **);
242
243int rb_gettimeofday(struct timeval *, void *);
244
245void rb_sleep(unsigned int seconds, unsigned int useconds);
246char *rb_crypt(const char *, const char *);
247
248unsigned char *rb_base64_encode(const unsigned char *str, int length);
249unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
3202e249
VY
250int rb_kill(pid_t, int);
251char *rb_strerror(int);
db137867 252
3202e249 253int rb_setenv(const char *, const char *, int);
db137867 254
3202e249
VY
255pid_t rb_waitpid(pid_t pid, int *status, int options);
256pid_t rb_getpid(void);
257//unsigned int rb_geteuid(void);
db137867 258
030272f3 259
db137867
AC
260#include <rb_tools.h>
261#include <rb_memory.h>
262#include <rb_commio.h>
263#include <rb_balloc.h>
264#include <rb_linebuf.h>
db137867
AC
265#include <rb_event.h>
266#include <rb_helper.h>
267#include <rb_rawbuf.h>
268#include <rb_patricia.h>
269
270#endif