]> jfr.im git - solanum.git/blame_incremental - librb/include/rb_lib.h
Remove ancient portability code (#361)
[solanum.git] / librb / include / rb_lib.h
... / ...
CommitLineData
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
37#ifdef rb_likely
38#undef rb_likely
39#endif
40#ifdef rb_unlikely
41#undef rb_unlikely
42#endif
43
44#if __GNUC__ == 2 && __GNUC_MINOR__ < 96
45# define __builtin_expect(x, expected_value) (x)
46#endif
47
48#define rb_likely(x) __builtin_expect(!!(x), 1)
49#define rb_unlikely(x) __builtin_expect(!!(x), 0)
50
51#else /* !__GNUC__ */
52
53#define UNUSED(x) x
54
55#ifdef rb_likely
56#undef rb_likely
57#endif
58#ifdef rb_unlikely
59#undef rb_unlikely
60#endif
61#define rb_likely(x) (x)
62#define rb_unlikely(x) (x)
63#endif
64
65#ifndef HOSTIPLEN
66#define HOSTIPLEN 53
67#endif
68
69
70/* For those unfamiliar with GNU format attributes, a is the 1 based
71 * argument number of the format string, and b is the 1 based argument
72 * number of the variadic ... */
73#ifdef __GNUC__
74#define AFP(a,b) __attribute__((format (printf, a, b)))
75#else
76#define AFP(a,b)
77#endif
78
79
80#ifdef __GNUC__
81#define slrb_assert(expr) ( \
82 rb_likely((expr)) || ( \
83 rb_lib_log( \
84 "file: %s line: %d (%s): Assertion failed: (%s)", \
85 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr), 0) \
86 )
87#else
88#define slrb_assert(expr) ( \
89 rb_likely((expr)) || ( \
90 rb_lib_log( \
91 "file: %s line: %d: Assertion failed: (%s)", \
92 __FILE__, __LINE__, #expr), 0) \
93 )
94#endif
95
96/* evaluates to true if assertion fails */
97#ifdef SOFT_ASSERT
98#define lrb_assert(expr) (!slrb_assert(expr))
99#else
100#define lrb_assert(expr) (assert(slrb_assert(expr)), 0)
101#endif
102
103#ifdef RB_SOCKADDR_HAS_SA_LEN
104#define ss_len sa_len
105#endif
106
107#define GET_SS_FAMILY(x) (((const struct sockaddr *)(x))->sa_family)
108#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
109#ifdef RB_SOCKADDR_HAS_SA_LEN
110#define SET_SS_LEN(x, y) do { \
111 struct sockaddr *_storage; \
112 _storage = ((struct sockaddr *)(x));\
113 _storage->sa_len = (y); \
114 } while (0)
115#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
116#else /* !RB_SOCKADDR_HAS_SA_LEN */
117#define SET_SS_LEN(x, y) (((struct sockaddr *)(x))->sa_family = ((struct sockaddr *)(x))->sa_family)
118#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))
119#endif
120
121#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
122#define SET_SS_PORT(x, y) do { \
123 if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
124 ((struct sockaddr_in *)(x))->sin_port = (y); \
125 } else { \
126 ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
127 } \
128 } while (0)
129
130#ifndef INADDRSZ
131#define INADDRSZ 4
132#endif
133
134#ifndef IN6ADDRSZ
135#define IN6ADDRSZ 16
136#endif
137
138#ifndef INT16SZ
139#define INT16SZ 2
140#endif
141
142#ifndef UINT16_MAX
143#define UINT16_MAX (65535U)
144#endif
145
146#ifndef UINT32_MAX
147#define UINT32_MAX (4294967295U)
148#endif
149
150
151typedef void log_cb(const char *buffer);
152typedef void restart_cb(const char *buffer);
153typedef void die_cb(const char *buffer);
154
155char *rb_ctime(const time_t, char *, size_t);
156char *rb_date(const time_t, char *, size_t);
157void rb_lib_log(const char *, ...);
158void rb_lib_restart(const char *, ...) __attribute__((noreturn));
159void rb_lib_die(const char *, ...);
160void rb_set_time(void);
161const char *rb_lib_version(void);
162
163void rb_lib_init(log_cb * xilog, restart_cb * irestart, die_cb * idie, int closeall, int maxfds,
164 size_t dh_size, size_t fd_heap_size);
165void rb_lib_loop(long delay) __attribute__((noreturn));
166
167time_t rb_current_time(void);
168const struct timeval *rb_current_time_tv(void);
169pid_t rb_spawn_process(const char *, const char **);
170
171char *rb_strtok_r(char *, const char *, char **);
172
173int rb_gettimeofday(struct timeval *, void *);
174
175void rb_sleep(unsigned int seconds, unsigned int useconds);
176char *rb_crypt(const char *, const char *);
177
178unsigned char *rb_base64_encode(const unsigned char *str, int length);
179unsigned char *rb_base64_decode(const unsigned char *str, int length, int *ret);
180int rb_kill(pid_t, int);
181char *rb_strerror(int);
182
183int rb_setenv(const char *, const char *, int);
184
185pid_t rb_waitpid(pid_t pid, int *status, int options);
186pid_t rb_getpid(void);
187//unsigned int rb_geteuid(void);
188
189
190#include <rb_tools.h>
191#include <rb_memory.h>
192#include <rb_commio.h>
193#include <rb_balloc.h>
194#include <rb_linebuf.h>
195#include <rb_event.h>
196#include <rb_helper.h>
197#include <rb_rawbuf.h>
198#include <rb_patricia.h>
199
200#endif