ipc/chromium/src/third_party/libevent/util-internal.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/util-internal.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,327 @@
     1.4 +/*
     1.5 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + * 3. The name of the author may not be used to endorse or promote products
    1.16 + *    derived from this software without specific prior written permission.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + */
    1.29 +#ifndef _EVENT_UTIL_INTERNAL_H
    1.30 +#define _EVENT_UTIL_INTERNAL_H
    1.31 +
    1.32 +#include "event2/event-config.h"
    1.33 +#include <errno.h>
    1.34 +
    1.35 +/* For EVUTIL_ASSERT */
    1.36 +#include "log-internal.h"
    1.37 +#include <stdio.h>
    1.38 +#include <stdlib.h>
    1.39 +#ifdef _EVENT_HAVE_SYS_SOCKET_H
    1.40 +#include <sys/socket.h>
    1.41 +#endif
    1.42 +#include "event2/util.h"
    1.43 +
    1.44 +#include "ipv6-internal.h"
    1.45 +
    1.46 +#ifdef __cplusplus
    1.47 +extern "C" {
    1.48 +#endif
    1.49 +
    1.50 +/* If we need magic to say "inline", get it for free internally. */
    1.51 +#ifdef _EVENT_inline
    1.52 +#define inline _EVENT_inline
    1.53 +#endif
    1.54 +#ifdef _EVENT___func__
    1.55 +#define __func__ _EVENT___func__
    1.56 +#endif
    1.57 +
    1.58 +/* A good no-op to use in macro definitions. */
    1.59 +#define _EVUTIL_NIL_STMT ((void)0)
    1.60 +/* A no-op that tricks the compiler into thinking a condition is used while
    1.61 + * definitely not making any code for it.  Used to compile out asserts while
    1.62 + * avoiding "unused variable" warnings.  The "!" forces the compiler to
    1.63 + * do the sizeof() on an int, in case "condition" is a bitfield value.
    1.64 + */
    1.65 +#define _EVUTIL_NIL_CONDITION(condition) do { \
    1.66 +	(void)sizeof(!(condition));  \
    1.67 +} while(0)
    1.68 +
    1.69 +/* Internal use only: macros to match patterns of error codes in a
    1.70 +   cross-platform way.  We need these macros because of two historical
    1.71 +   reasons: first, nonblocking IO functions are generally written to give an
    1.72 +   error on the "blocked now, try later" case, so sometimes an error from a
    1.73 +   read, write, connect, or accept means "no error; just wait for more
    1.74 +   data," and we need to look at the error code.  Second, Windows defines
    1.75 +   a different set of error codes for sockets. */
    1.76 +
    1.77 +#ifndef WIN32
    1.78 +
    1.79 +/* True iff e is an error that means a read/write operation can be retried. */
    1.80 +#define EVUTIL_ERR_RW_RETRIABLE(e)				\
    1.81 +	((e) == EINTR || (e) == EAGAIN)
    1.82 +/* True iff e is an error that means an connect can be retried. */
    1.83 +#define EVUTIL_ERR_CONNECT_RETRIABLE(e)			\
    1.84 +	((e) == EINTR || (e) == EINPROGRESS)
    1.85 +/* True iff e is an error that means a accept can be retried. */
    1.86 +#define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
    1.87 +	((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED)
    1.88 +
    1.89 +/* True iff e is an error that means the connection was refused */
    1.90 +#define EVUTIL_ERR_CONNECT_REFUSED(e)					\
    1.91 +	((e) == ECONNREFUSED)
    1.92 +
    1.93 +#else
    1.94 +
    1.95 +#define EVUTIL_ERR_RW_RETRIABLE(e)					\
    1.96 +	((e) == WSAEWOULDBLOCK ||					\
    1.97 +	    (e) == WSAEINTR)
    1.98 +
    1.99 +#define EVUTIL_ERR_CONNECT_RETRIABLE(e)					\
   1.100 +	((e) == WSAEWOULDBLOCK ||					\
   1.101 +	    (e) == WSAEINTR ||						\
   1.102 +	    (e) == WSAEINPROGRESS ||					\
   1.103 +	    (e) == WSAEINVAL)
   1.104 +
   1.105 +#define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
   1.106 +	EVUTIL_ERR_RW_RETRIABLE(e)
   1.107 +
   1.108 +#define EVUTIL_ERR_CONNECT_REFUSED(e)					\
   1.109 +	((e) == WSAECONNREFUSED)
   1.110 +
   1.111 +#endif
   1.112 +
   1.113 +#ifdef _EVENT_socklen_t
   1.114 +#define socklen_t _EVENT_socklen_t
   1.115 +#endif
   1.116 +
   1.117 +/* Arguments for shutdown() */
   1.118 +#ifdef SHUT_RD
   1.119 +#define EVUTIL_SHUT_RD SHUT_RD
   1.120 +#else
   1.121 +#define EVUTIL_SHUT_RD 0
   1.122 +#endif
   1.123 +#ifdef SHUT_WR
   1.124 +#define EVUTIL_SHUT_WR SHUT_WR
   1.125 +#else
   1.126 +#define EVUTIL_SHUT_WR 1
   1.127 +#endif
   1.128 +#ifdef SHUT_BOTH
   1.129 +#define EVUTIL_SHUT_BOTH SHUT_BOTH
   1.130 +#else
   1.131 +#define EVUTIL_SHUT_BOTH 2
   1.132 +#endif
   1.133 +
   1.134 +/* Locale-independent replacements for some ctypes functions.  Use these
   1.135 + * when you care about ASCII's notion of character types, because you are about
   1.136 + * to send those types onto the wire.
   1.137 + */
   1.138 +int EVUTIL_ISALPHA(char c);
   1.139 +int EVUTIL_ISALNUM(char c);
   1.140 +int EVUTIL_ISSPACE(char c);
   1.141 +int EVUTIL_ISDIGIT(char c);
   1.142 +int EVUTIL_ISXDIGIT(char c);
   1.143 +int EVUTIL_ISPRINT(char c);
   1.144 +int EVUTIL_ISLOWER(char c);
   1.145 +int EVUTIL_ISUPPER(char c);
   1.146 +char EVUTIL_TOUPPER(char c);
   1.147 +char EVUTIL_TOLOWER(char c);
   1.148 +
   1.149 +/** Helper macro.  If we know that a given pointer points to a field in a
   1.150 +    structure, return a pointer to the structure itself.  Used to implement
   1.151 +    our half-baked C OO.  Example:
   1.152 +
   1.153 +    struct subtype {
   1.154 +	int x;
   1.155 +	struct supertype common;
   1.156 +	int y;
   1.157 +    };
   1.158 +    ...
   1.159 +    void fn(struct supertype *super) {
   1.160 +	struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
   1.161 +	...
   1.162 +    }
   1.163 + */
   1.164 +#define EVUTIL_UPCAST(ptr, type, field)				\
   1.165 +	((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
   1.166 +
   1.167 +/* As open(pathname, flags, mode), except that the file is always opened with
   1.168 + * the close-on-exec flag set. (And the mode argument is mandatory.)
   1.169 + */
   1.170 +int evutil_open_closeonexec(const char *pathname, int flags, unsigned mode);
   1.171 +
   1.172 +int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
   1.173 +    int is_binary);
   1.174 +
   1.175 +int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
   1.176 +
   1.177 +int evutil_socket_finished_connecting(evutil_socket_t fd);
   1.178 +
   1.179 +int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]);
   1.180 +
   1.181 +int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
   1.182 +    ev_socklen_t *socklen, int port);
   1.183 +
   1.184 +const char *evutil_getenv(const char *name);
   1.185 +
   1.186 +long _evutil_weakrand(void);
   1.187 +
   1.188 +/* Evaluates to the same boolean value as 'p', and hints to the compiler that
   1.189 + * we expect this value to be false. */
   1.190 +#if defined(__GNUC__) && __GNUC__ >= 3         /* gcc 3.0 or later */
   1.191 +#define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
   1.192 +#else
   1.193 +#define EVUTIL_UNLIKELY(p) (p)
   1.194 +#endif
   1.195 +
   1.196 +/* Replacement for assert() that calls event_errx on failure. */
   1.197 +#ifdef NDEBUG
   1.198 +#define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond)
   1.199 +#define EVUTIL_FAILURE_CHECK(cond) 0
   1.200 +#else
   1.201 +#define EVUTIL_ASSERT(cond)						\
   1.202 +	do {								\
   1.203 +		if (EVUTIL_UNLIKELY(!(cond))) {				\
   1.204 +			event_errx(_EVENT_ERR_ABORT,			\
   1.205 +			    "%s:%d: Assertion %s failed in %s",		\
   1.206 +			    __FILE__,__LINE__,#cond,__func__);		\
   1.207 +			/* In case a user-supplied handler tries to */	\
   1.208 +			/* return control to us, log and abort here. */	\
   1.209 +			(void)fprintf(stderr,				\
   1.210 +			    "%s:%d: Assertion %s failed in %s",		\
   1.211 +			    __FILE__,__LINE__,#cond,__func__);		\
   1.212 +			abort();					\
   1.213 +		}							\
   1.214 +	} while (0)
   1.215 +#define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
   1.216 +#endif
   1.217 +
   1.218 +#ifndef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE
   1.219 +/* Replacement for sockaddr storage that we can use internally on platforms
   1.220 + * that lack it.  It is not space-efficient, but neither is sockaddr_storage.
   1.221 + */
   1.222 +struct sockaddr_storage {
   1.223 +	union {
   1.224 +		struct sockaddr ss_sa;
   1.225 +		struct sockaddr_in ss_sin;
   1.226 +		struct sockaddr_in6 ss_sin6;
   1.227 +		char ss_padding[128];
   1.228 +	} ss_union;
   1.229 +};
   1.230 +#define ss_family ss_union.ss_sa.sa_family
   1.231 +#endif
   1.232 +
   1.233 +/* Internal addrinfo error code.  This one is returned from only from
   1.234 + * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS
   1.235 + * server. */
   1.236 +#define EVUTIL_EAI_NEED_RESOLVE      -90002
   1.237 +
   1.238 +struct evdns_base;
   1.239 +struct evdns_getaddrinfo_request;
   1.240 +typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
   1.241 +    struct evdns_base *base,
   1.242 +    const char *nodename, const char *servname,
   1.243 +    const struct evutil_addrinfo *hints_in,
   1.244 +    void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
   1.245 +
   1.246 +void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
   1.247 +
   1.248 +struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa,
   1.249 +    ev_socklen_t socklen, const struct evutil_addrinfo *hints);
   1.250 +struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first,
   1.251 +    struct evutil_addrinfo *append);
   1.252 +void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints);
   1.253 +int evutil_getaddrinfo_common(const char *nodename, const char *servname,
   1.254 +    struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
   1.255 +
   1.256 +int evutil_getaddrinfo_async(struct evdns_base *dns_base,
   1.257 +    const char *nodename, const char *servname,
   1.258 +    const struct evutil_addrinfo *hints_in,
   1.259 +    void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
   1.260 +
   1.261 +/** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
   1.262 + * ::1). */
   1.263 +int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
   1.264 +
   1.265 +
   1.266 +/**
   1.267 +    Formats a sockaddr sa into a string buffer of size outlen stored in out.
   1.268 +    Returns a pointer to out.  Always writes something into out, so it's safe
   1.269 +    to use the output of this function without checking it for NULL.
   1.270 + */
   1.271 +const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
   1.272 +
   1.273 +long evutil_tv_to_msec(const struct timeval *tv);
   1.274 +
   1.275 +int evutil_hex_char_to_int(char c);
   1.276 +
   1.277 +#ifdef WIN32
   1.278 +HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
   1.279 +#endif
   1.280 +
   1.281 +#ifndef EV_SIZE_FMT
   1.282 +#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
   1.283 +#define EV_U64_FMT "%I64u"
   1.284 +#define EV_I64_FMT "%I64d"
   1.285 +#define EV_I64_ARG(x) ((__int64)(x))
   1.286 +#define EV_U64_ARG(x) ((unsigned __int64)(x))
   1.287 +#else
   1.288 +#define EV_U64_FMT "%llu"
   1.289 +#define EV_I64_FMT "%lld"
   1.290 +#define EV_I64_ARG(x) ((long long)(x))
   1.291 +#define EV_U64_ARG(x) ((unsigned long long)(x))
   1.292 +#endif
   1.293 +#endif
   1.294 +
   1.295 +#ifdef _WIN32
   1.296 +#define EV_SOCK_FMT EV_I64_FMT
   1.297 +#define EV_SOCK_ARG(x) EV_I64_ARG((x))
   1.298 +#else
   1.299 +#define EV_SOCK_FMT "%d"
   1.300 +#define EV_SOCK_ARG(x) (x)
   1.301 +#endif
   1.302 +
   1.303 +#if defined(__STDC__) && defined(__STDC_VERSION__)
   1.304 +#if (__STDC_VERSION__ >= 199901L)
   1.305 +#define EV_SIZE_FMT "%zu"
   1.306 +#define EV_SSIZE_FMT "%zd"
   1.307 +#define EV_SIZE_ARG(x) (x)
   1.308 +#define EV_SSIZE_ARG(x) (x)
   1.309 +#endif
   1.310 +#endif
   1.311 +
   1.312 +#ifndef EV_SIZE_FMT
   1.313 +#if (_EVENT_SIZEOF_SIZE_T <= _EVENT_SIZEOF_LONG)
   1.314 +#define EV_SIZE_FMT "%lu"
   1.315 +#define EV_SSIZE_FMT "%ld"
   1.316 +#define EV_SIZE_ARG(x) ((unsigned long)(x))
   1.317 +#define EV_SSIZE_ARG(x) ((long)(x))
   1.318 +#else
   1.319 +#define EV_SIZE_FMT EV_U64_FMT
   1.320 +#define EV_SSIZE_FMT EV_I64_FMT
   1.321 +#define EV_SIZE_ARG(x) EV_U64_ARG(x)
   1.322 +#define EV_SSIZE_ARG(x) EV_I64_ARG(x)
   1.323 +#endif
   1.324 +#endif
   1.325 +
   1.326 +#ifdef __cplusplus
   1.327 +}
   1.328 +#endif
   1.329 +
   1.330 +#endif

mercurial