Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libevent / dist / evutil.h
blob7107499f88ac77713100adcb3a866281da6b9c9a
1 /* $NetBSD$ */
2 /*
3 * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _EVUTIL_H_
29 #define _EVUTIL_H_
31 /** @file evutil.h
33 Common convenience functions for cross-platform portability and
34 related socket manipulations.
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 #include <event-config.h>
43 #ifdef _EVENT_HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46 #ifdef _EVENT_HAVE_STDINT_H
47 #include <stdint.h>
48 #elif defined(_EVENT_HAVE_INTTYPES_H)
49 #include <inttypes.h>
50 #endif
51 #ifdef _EVENT_HAVE_SYS_TYPES_H
52 #include <sys/types.h>
53 #endif
54 #include <stdarg.h>
56 #ifdef _EVENT_HAVE_UINT64_T
57 #define ev_uint64_t uint64_t
58 #define ev_int64_t int64_t
59 #elif defined(WIN32)
60 #define ev_uint64_t unsigned __int64
61 #define ev_int64_t signed __int64
62 #elif _EVENT_SIZEOF_LONG_LONG == 8
63 #define ev_uint64_t unsigned long long
64 #define ev_int64_t long long
65 #elif _EVENT_SIZEOF_LONG == 8
66 #define ev_uint64_t unsigned long
67 #define ev_int64_t long
68 #else
69 #error "No way to define ev_uint64_t"
70 #endif
72 #ifdef _EVENT_HAVE_UINT32_T
73 #define ev_uint32_t uint32_t
74 #elif defined(WIN32)
75 #define ev_uint32_t unsigned int
76 #elif _EVENT_SIZEOF_LONG == 4
77 #define ev_uint32_t unsigned long
78 #elif _EVENT_SIZEOF_INT == 4
79 #define ev_uint32_t unsigned int
80 #else
81 #error "No way to define ev_uint32_t"
82 #endif
84 #ifdef _EVENT_HAVE_UINT16_T
85 #define ev_uint16_t uint16_t
86 #elif defined(WIN32)
87 #define ev_uint16_t unsigned short
88 #elif _EVENT_SIZEOF_INT == 2
89 #define ev_uint16_t unsigned int
90 #elif _EVENT_SIZEOF_SHORT == 2
91 #define ev_uint16_t unsigned short
92 #else
93 #error "No way to define ev_uint16_t"
94 #endif
96 #ifdef _EVENT_HAVE_UINT8_T
97 #define ev_uint8_t uint8_t
98 #else
99 #define ev_uint8_t unsigned char
100 #endif
102 int evutil_socketpair(int d, int type, int protocol, int sv[2]);
103 int evutil_make_socket_nonblocking(int sock);
104 #ifdef WIN32
105 #define EVUTIL_CLOSESOCKET(s) closesocket(s)
106 #else
107 #define EVUTIL_CLOSESOCKET(s) close(s)
108 #endif
110 #ifdef WIN32
111 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
112 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
113 do { WSASetLastError(errcode); } while (0)
114 #else
115 #define EVUTIL_SOCKET_ERROR() (errno)
116 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
117 do { errno = (errcode); } while (0)
118 #endif
121 * Manipulation functions for struct timeval
123 #ifdef _EVENT_HAVE_TIMERADD
124 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
125 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
126 #else
127 #define evutil_timeradd(tvp, uvp, vvp) \
128 do { \
129 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
130 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
131 if ((vvp)->tv_usec >= 1000000) { \
132 (vvp)->tv_sec++; \
133 (vvp)->tv_usec -= 1000000; \
135 } while (0)
136 #define evutil_timersub(tvp, uvp, vvp) \
137 do { \
138 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
139 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
140 if ((vvp)->tv_usec < 0) { \
141 (vvp)->tv_sec--; \
142 (vvp)->tv_usec += 1000000; \
144 } while (0)
145 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
147 #ifdef _EVENT_HAVE_TIMERCLEAR
148 #define evutil_timerclear(tvp) timerclear(tvp)
149 #else
150 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
151 #endif
153 #define evutil_timercmp(tvp, uvp, cmp) \
154 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
155 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
156 ((tvp)->tv_sec cmp (uvp)->tv_sec))
158 #ifdef _EVENT_HAVE_TIMERISSET
159 #define evutil_timerisset(tvp) timerisset(tvp)
160 #else
161 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
162 #endif
165 /* big-int related functions */
166 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
169 #ifdef _EVENT_HAVE_GETTIMEOFDAY
170 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
171 #else
172 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
173 #endif
175 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
176 #ifdef __GNUC__
177 __attribute__((format(printf, 3, 4)))
178 #endif
180 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
182 #ifdef __cplusplus
184 #endif
186 #endif /* _EVUTIL_H_ */