| 1 |
/*---------------------------------------------------------------------------*/ |
| 2 |
/* */ |
| 3 |
/* Host.h - Basic header file to provide cross-platform solutions via */ |
| 4 |
/* macros, conditional compilation, etc. */ |
| 5 |
/* */ |
| 6 |
/* Author : Mark Carrier ([email protected]) */ |
| 7 |
/* */ |
| 8 |
/*---------------------------------------------------------------------------*/ |
| 9 |
/* Copyright (c) 2007 CarrierLabs, LLC. All rights reserved. |
| 10 |
* |
| 11 |
* Redistribution and use in source and binary forms, with or without |
| 12 |
* modification, are permitted provided that the following conditions |
| 13 |
* are met: |
| 14 |
* |
| 15 |
* 1. Redistributions of source code must retain the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer. |
| 17 |
* |
| 18 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 19 |
* notice, this list of conditions and the following disclaimer in |
| 20 |
* the documentation and/or other materials provided with the |
| 21 |
* distribution. |
| 22 |
* |
| 23 |
* 3. The name of the author may not be used to endorse or promote products |
| 24 |
* derived from this software without specific prior written permission. |
| 25 |
* |
| 26 |
* 4. The name "CarrierLabs" must not be used to |
| 27 |
* endorse or promote products derived from this software without |
| 28 |
* prior written permission. For written permission, please contact |
| 29 |
* [email protected]. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY |
| 32 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 33 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 34 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR |
| 35 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 36 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 37 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 38 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 39 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 40 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 41 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 42 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 |
*----------------------------------------------------------------------------*/ |
| 44 |
#ifndef __HOST_H__ |
| 45 |
#define __HOST_H__ |
| 46 |
|
| 47 |
#include <cstdint> // this will define UINT8_MAX, so we don't need the re-defines below |
| 48 |
#include <limits.h> |
| 49 |
|
| 50 |
#ifdef __cplusplus |
| 51 |
extern "C" |
| 52 |
{ |
| 53 |
#endif |
| 54 |
|
| 55 |
/*---------------------------------------------------------------------------*/ |
| 56 |
/* */ |
| 57 |
/* Type Definition Macros */ |
| 58 |
/* */ |
| 59 |
/*---------------------------------------------------------------------------*/ |
| 60 |
#ifndef __WORDSIZE |
| 61 |
/* Assume 32 */ |
| 62 |
#define __WORDSIZE 32 |
| 63 |
#endif |
| 64 |
|
| 65 |
#if defined(__linux__) || defined(_DARWIN) |
| 66 |
typedef unsigned char uint8; |
| 67 |
typedef char int8; |
| 68 |
typedef unsigned short uint16; |
| 69 |
typedef short int16; |
| 70 |
typedef unsigned int uint32; |
| 71 |
typedef int int32; |
| 72 |
typedef int SOCKET; |
| 73 |
#endif |
| 74 |
|
| 75 |
#ifdef WIN32 |
| 76 |
struct iovec { |
| 77 |
void *iov_base; |
| 78 |
size_t iov_len; |
| 79 |
}; |
| 80 |
|
| 81 |
typedef unsigned char uint8; |
| 82 |
typedef char int8; |
| 83 |
typedef unsigned short uint16; |
| 84 |
typedef short int16; |
| 85 |
typedef unsigned int uint32; |
| 86 |
typedef int int32; |
| 87 |
#endif |
| 88 |
|
| 89 |
#ifdef WIN32 |
| 90 |
typedef int socklen_t; |
| 91 |
#endif |
| 92 |
|
| 93 |
#if defined(WIN32) |
| 94 |
typedef unsigned long long int uint64; |
| 95 |
typedef long long int int64; |
| 96 |
#elif (__WORDSIZE == 32) |
| 97 |
__extension__ |
| 98 |
typedef long long int int64; |
| 99 |
__extension__ |
| 100 |
typedef unsigned long long int uint64; |
| 101 |
#elif (__WORDSIZE == 64) |
| 102 |
typedef unsigned long int uint64; |
| 103 |
typedef long int int64; |
| 104 |
#endif |
| 105 |
|
| 106 |
#ifdef WIN32 |
| 107 |
|
| 108 |
#ifndef UINT8_MAX |
| 109 |
#define UINT8_MAX (UCHAR_MAX) |
| 110 |
#endif |
| 111 |
#ifndef UINT16_MAX |
| 112 |
#define UINT16_MAX (USHRT_MAX) |
| 113 |
#endif |
| 114 |
#ifndef UINT32_MAX |
| 115 |
#define UINT32_MAX (ULONG_MAX) |
| 116 |
#endif |
| 117 |
|
| 118 |
#if __WORDSIZE == 64 |
| 119 |
#define SIZE_MAX (18446744073709551615UL) |
| 120 |
#else |
| 121 |
#ifndef SIZE_MAX |
| 122 |
#define SIZE_MAX (4294967295U) |
| 123 |
#endif |
| 124 |
#endif |
| 125 |
#endif |
| 126 |
|
| 127 |
#if defined(WIN32) |
| 128 |
#define ssize_t size_t |
| 129 |
#endif |
| 130 |
|
| 131 |
#ifndef TRUE |
| 132 |
#define TRUE 1 |
| 133 |
#endif |
| 134 |
|
| 135 |
#ifndef FALSE |
| 136 |
#define FALSE 0 |
| 137 |
#endif |
| 138 |
|
| 139 |
#ifndef htonll |
| 140 |
#ifdef _BIG_ENDIAN |
| 141 |
#define htonll(x) (x) |
| 142 |
#define ntohll(x) (x) |
| 143 |
#else |
| 144 |
#define htonll(x) ((((uint64)htonl(x)) << 32) + htonl(x >> 32)) |
| 145 |
#define ntohll(x) ((((uint64)ntohl(x)) << 32) + ntohl(x >> 32)) |
| 146 |
#endif |
| 147 |
#endif |
| 148 |
|
| 149 |
/*---------------------------------------------------------------------------*/ |
| 150 |
/* */ |
| 151 |
/* Socket Macros */ |
| 152 |
/* */ |
| 153 |
/*---------------------------------------------------------------------------*/ |
| 154 |
#ifdef WIN32 |
| 155 |
#define SHUT_RD 0 |
| 156 |
#define SHUT_WR 1 |
| 157 |
#define SHUT_RDWR 2 |
| 158 |
#define ACCEPT(a,b,c) accept(a,b,c) |
| 159 |
#define CONNECT(a,b,c) connect(a,b,c) |
| 160 |
#define CLOSE(a) closesocket(a) |
| 161 |
#define READ(a,b,c) read(a,b,c) |
| 162 |
#define RECV(a,b,c,d) recv(a, (char *)b, c, d) |
| 163 |
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f) |
| 164 |
#define RECV_FLAGS MSG_WAITALL |
| 165 |
#define SELECT(a,b,c,d,e) select((int32)a,b,c,d,e) |
| 166 |
#define SEND(a,b,c,d) send(a, (const char *)b, (int)c, d) |
| 167 |
#define SENDTO(a,b,c,d,e,f) sendto(a, (const char *)b, (int)c, d, e, f) |
| 168 |
#define SEND_FLAGS 0 |
| 169 |
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d) |
| 170 |
#define SET_SOCKET_ERROR(x,y) errno=y |
| 171 |
#define SOCKET_ERROR_INTERUPT EINTR |
| 172 |
#define SOCKET_ERROR_TIMEDOUT EAGAIN |
| 173 |
#define WRITE(a,b,c) write(a,b,c) |
| 174 |
#define WRITEV(a,b,c) Writev(b, c) |
| 175 |
#define GETSOCKOPT(a,b,c,d,e) getsockopt(a,b,c,(char *)d, (int *)e) |
| 176 |
#define SETSOCKOPT(a,b,c,d,e) setsockopt(a,b,c,(char *)d, (int)e) |
| 177 |
#define GETHOSTBYNAME(a) gethostbyname(a) |
| 178 |
#endif |
| 179 |
|
| 180 |
#if defined(__linux__) || defined(_DARWIN) |
| 181 |
#define ACCEPT(a,b,c) accept(a,b,c) |
| 182 |
#define CONNECT(a,b,c) connect(a,b,c) |
| 183 |
#define CLOSE(a) close(a) |
| 184 |
#define READ(a,b,c) read(a,b,c) |
| 185 |
#define RECV(a,b,c,d) recv(a, (void *)b, c, d) |
| 186 |
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f) |
| 187 |
#define RECV_FLAGS MSG_WAITALL |
| 188 |
#define SELECT(a,b,c,d,e) select(a,b,c,d,e) |
| 189 |
#define SEND(a,b,c,d) send(a, (const int8 *)b, c, d) |
| 190 |
#define SENDTO(a,b,c,d,e,f) sendto(a, (const int8 *)b, c, d, e, f) |
| 191 |
#define SEND_FLAGS 0 |
| 192 |
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d) |
| 193 |
#define SET_SOCKET_ERROR(x,y) errno=y |
| 194 |
#define SOCKET_ERROR_INTERUPT EINTR |
| 195 |
#define SOCKET_ERROR_TIMEDOUT EAGAIN |
| 196 |
#define WRITE(a,b,c) write(a,b,c) |
| 197 |
#define WRITEV(a,b,c) writev(a, b, c) |
| 198 |
#define GETSOCKOPT(a,b,c,d,e) getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e) |
| 199 |
#define SETSOCKOPT(a,b,c,d,e) setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e) |
| 200 |
#define GETHOSTBYNAME(a) gethostbyname(a) |
| 201 |
#endif |
| 202 |
|
| 203 |
|
| 204 |
/*---------------------------------------------------------------------------*/ |
| 205 |
/* */ |
| 206 |
/* File Macros */ |
| 207 |
/* */ |
| 208 |
/*---------------------------------------------------------------------------*/ |
| 209 |
#define STRUCT_STAT struct stat |
| 210 |
#define LSTAT(x,y) lstat(x,y) |
| 211 |
#define FILE_HANDLE FILE * |
| 212 |
#define CLEARERR(x) clearerr(x) |
| 213 |
#define FCLOSE(x) fclose(x) |
| 214 |
#define FEOF(x) feof(x) |
| 215 |
#define FERROR(x) ferror(x) |
| 216 |
#define FFLUSH(x) fflush(x) |
| 217 |
#define FILENO(s) fileno(s) |
| 218 |
#define FOPEN(x,y) fopen(x, y) |
| 219 |
//#define FREAD(a,b,c,d) fread(a, b, c, d) |
| 220 |
#define FSTAT(s, st) fstat(FILENO(s), st) |
| 221 |
//#define FWRITE(a,b,c,d) fwrite(a, b, c, d) |
| 222 |
#define STAT_BLK_SIZE(x) ((x).st_blksize) |
| 223 |
|
| 224 |
// greebo: Use these defines to fix C4496 |
| 225 |
#ifdef WIN32 |
| 226 |
#define LSEEK(x, y, z) _lseek(x, y, z) |
| 227 |
#define FREAD(x, y, z) _read(x, y, z) |
| 228 |
#elif defined(__linux__) || defined(_DARWIN) |
| 229 |
#define LSEEK(x, y, z) lseek(x, y, z) |
| 230 |
#define FREAD(x, y, z) read(x, y, z) |
| 231 |
#endif |
| 232 |
|
| 233 |
/*---------------------------------------------------------------------------*/ |
| 234 |
/* */ |
| 235 |
/* Misc Macros */ |
| 236 |
/* */ |
| 237 |
/*---------------------------------------------------------------------------*/ |
| 238 |
#if defined(WIN32) |
| 239 |
#define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x) |
| 240 |
#else |
| 241 |
#define GET_CLOCK_COUNT(x) gettimeofday(x, NULL) |
| 242 |
#endif |
| 243 |
|
| 244 |
#if defined(WIN32) |
| 245 |
#define STRTOULL(x) _atoi64(x) |
| 246 |
#else |
| 247 |
#define STRTOULL(x) strtoull(x, NULL, 10) |
| 248 |
#endif |
| 249 |
|
| 250 |
#if defined(WIN32) |
| 251 |
#define SNPRINTF _snprintf |
| 252 |
#define PRINTF printf |
| 253 |
#define VPRINTF vprintf |
| 254 |
#define FPRINTF fprintf |
| 255 |
#else |
| 256 |
#define SNPRINTF snprintf |
| 257 |
#define PRINTF printf |
| 258 |
#define VPRINTF vprintf |
| 259 |
#define FPRINTF fprintf |
| 260 |
#endif |
| 261 |
|
| 262 |
#ifdef __cplusplus |
| 263 |
} |
| 264 |
#endif |
| 265 |
|
| 266 |
#endif /* __HOST_H__ */ |