Host.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*---------------------------------------------------------------------------*/
  2. /* */
  3. /* Host.h - Basic header file to provide cross-platform solutions via */
  4. /* macros, conditional compilation, etc. */
  5. /* */
  6. /* Author : Mark Carrier (mark@carrierlabs.com) */
  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. * mark@carrierlabs.com.
  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. #include <limits.h>
  47. #ifdef __cplusplus
  48. extern "C"
  49. {
  50. #endif
  51. /*---------------------------------------------------------------------------*/
  52. /* */
  53. /* Type Definition Macros */
  54. /* */
  55. /*---------------------------------------------------------------------------*/
  56. #ifndef __WORDSIZE
  57. /* Assume 32 */
  58. #define __WORDSIZE 32
  59. #endif
  60. #if defined(_LINUX) || defined(_DARWIN)
  61. typedef unsigned char uint8;
  62. typedef char int8;
  63. typedef unsigned short uint16;
  64. typedef short int16;
  65. typedef unsigned int uint32;
  66. typedef int int32;
  67. typedef int SOCKET;
  68. #endif
  69. #ifdef WIN32
  70. struct iovec {
  71. void *iov_base;
  72. size_t iov_len;
  73. };
  74. typedef unsigned char uint8;
  75. typedef char int8;
  76. typedef unsigned short uint16;
  77. typedef short int16;
  78. typedef unsigned int uint32;
  79. typedef int int32;
  80. #endif
  81. #ifdef WIN32
  82. typedef int socklen_t;
  83. #endif
  84. #if defined(WIN32)
  85. typedef unsigned long long int uint64;
  86. typedef long long int int64;
  87. #elif (__WORDSIZE == 32)
  88. __extension__
  89. typedef long long int int64;
  90. __extension__
  91. typedef unsigned long long int uint64;
  92. #elif (__WORDSIZE == 64)
  93. typedef unsigned long int uint64;
  94. typedef long int int64;
  95. #endif
  96. #ifdef WIN32
  97. #ifndef UINT8_MAX
  98. #define UINT8_MAX (UCHAR_MAX)
  99. #endif
  100. #ifndef UINT16_MAX
  101. #define UINT16_MAX (USHRT_MAX)
  102. #endif
  103. #ifndef UINT32_MAX
  104. #define UINT32_MAX (ULONG_MAX)
  105. #endif
  106. #if __WORDSIZE == 64
  107. #define SIZE_MAX (18446744073709551615UL)
  108. #else
  109. #ifndef SIZE_MAX
  110. #define SIZE_MAX (4294967295U)
  111. #endif
  112. #endif
  113. #endif
  114. #if defined(WIN32)
  115. #define ssize_t size_t
  116. #endif
  117. #ifndef TRUE
  118. #define TRUE 1
  119. #endif
  120. #ifndef FALSE
  121. #define FALSE 0
  122. #endif
  123. #ifndef htonll
  124. #ifdef _BIG_ENDIAN
  125. #define htonll(x) (x)
  126. #define ntohll(x) (x)
  127. #else
  128. #define htonll(x) ((((uint64)htonl(x)) << 32) + htonl(x >> 32))
  129. #define ntohll(x) ((((uint64)ntohl(x)) << 32) + ntohl(x >> 32))
  130. #endif
  131. #endif
  132. /*---------------------------------------------------------------------------*/
  133. /* */
  134. /* Socket Macros */
  135. /* */
  136. /*---------------------------------------------------------------------------*/
  137. #ifdef WIN32
  138. #define SHUT_RD 0
  139. #define SHUT_WR 1
  140. #define SHUT_RDWR 2
  141. #define ACCEPT(a,b,c) accept(a,b,c)
  142. #define CONNECT(a,b,c) connect(a,b,c)
  143. #define CLOSE(a) closesocket(a)
  144. #define READ(a,b,c) read(a,b,c)
  145. #define RECV(a,b,c,d) recv(a, (char *)b, c, d)
  146. #define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f)
  147. #define RECV_FLAGS MSG_WAITALL
  148. #define SELECT(a,b,c,d,e) select((int32)a,b,c,d,e)
  149. #define SEND(a,b,c,d) send(a, (const char *)b, (int)c, d)
  150. #define SENDTO(a,b,c,d,e,f) sendto(a, (const char *)b, (int)c, d, e, f)
  151. #define SEND_FLAGS 0
  152. #define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
  153. #define SET_SOCKET_ERROR(x,y) errno=y
  154. #define SOCKET_ERROR_INTERUPT EINTR
  155. #define SOCKET_ERROR_TIMEDOUT EAGAIN
  156. #define WRITE(a,b,c) write(a,b,c)
  157. #define WRITEV(a,b,c) Writev(b, c)
  158. #define GETSOCKOPT(a,b,c,d,e) getsockopt(a,b,c,(char *)d, (int *)e)
  159. #define SETSOCKOPT(a,b,c,d,e) setsockopt(a,b,c,(char *)d, (int)e)
  160. #define GETHOSTBYNAME(a) gethostbyname(a)
  161. #endif
  162. #if defined(_LINUX) || defined(_DARWIN)
  163. #define ACCEPT(a,b,c) accept(a,b,c)
  164. #define CONNECT(a,b,c) connect(a,b,c)
  165. #define CLOSE(a) close(a)
  166. #define READ(a,b,c) read(a,b,c)
  167. #define RECV(a,b,c,d) recv(a, (void *)b, c, d)
  168. #define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f)
  169. #define RECV_FLAGS MSG_WAITALL
  170. #define SELECT(a,b,c,d,e) select(a,b,c,d,e)
  171. #define SEND(a,b,c,d) send(a, (const int8 *)b, c, d)
  172. #define SENDTO(a,b,c,d,e,f) sendto(a, (const int8 *)b, c, d, e, f)
  173. #define SEND_FLAGS 0
  174. #define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
  175. #define SET_SOCKET_ERROR(x,y) errno=y
  176. #define SOCKET_ERROR_INTERUPT EINTR
  177. #define SOCKET_ERROR_TIMEDOUT EAGAIN
  178. #define WRITE(a,b,c) write(a,b,c)
  179. #define WRITEV(a,b,c) writev(a, b, c)
  180. #define GETSOCKOPT(a,b,c,d,e) getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e)
  181. #define SETSOCKOPT(a,b,c,d,e) setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e)
  182. #define GETHOSTBYNAME(a) gethostbyname(a)
  183. #endif
  184. /*---------------------------------------------------------------------------*/
  185. /* */
  186. /* File Macros */
  187. /* */
  188. /*---------------------------------------------------------------------------*/
  189. #define STRUCT_STAT struct stat
  190. #define LSTAT(x,y) lstat(x,y)
  191. #define FILE_HANDLE FILE *
  192. #define CLEARERR(x) clearerr(x)
  193. #define FCLOSE(x) fclose(x)
  194. #define FEOF(x) feof(x)
  195. #define FERROR(x) ferror(x)
  196. #define FFLUSH(x) fflush(x)
  197. #define FILENO(s) fileno(s)
  198. #define FOPEN(x,y) fopen(x, y)
  199. //#define FREAD(a,b,c,d) fread(a, b, c, d)
  200. #define FSTAT(s, st) fstat(FILENO(s), st)
  201. //#define FWRITE(a,b,c,d) fwrite(a, b, c, d)
  202. #define STAT_BLK_SIZE(x) ((x).st_blksize)
  203. /*---------------------------------------------------------------------------*/
  204. /* */
  205. /* Misc Macros */
  206. /* */
  207. /*---------------------------------------------------------------------------*/
  208. #if defined(WIN32)
  209. #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x)
  210. #else
  211. #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)
  212. #endif
  213. #if defined(WIN32)
  214. #define STRTOULL(x) _atoi64(x)
  215. #else
  216. #define STRTOULL(x) strtoull(x, NULL, 10)
  217. #endif
  218. #if defined(WIN32)
  219. #define SNPRINTF _snprintf
  220. #define PRINTF printf
  221. #define VPRINTF vprintf
  222. #define FPRINTF fprintf
  223. #else
  224. #define SNPRINTF snprintf
  225. #define PRINTF printf
  226. #define VPRINTF vprintf
  227. #define FPRINTF fprintf
  228. #endif
  229. #ifdef _MSC_VER
  230. #define EXPORT __declspec(dllexport)
  231. #else
  232. #define EXPORT
  233. #endif
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* __HOST_H__ */