torint.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Copyright 2003 Roger Dingledine
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. /**
  6. * \file torint.h
  7. * \brief Header file to define uint32_t and friends
  8. **/
  9. #ifndef __TORINT_H
  10. #define __TORINT_H
  11. #define TORINT_H_ID "$Id$"
  12. #include "orconfig.h"
  13. #ifdef HAVE_STDINT_H
  14. #include <stdint.h>
  15. #endif
  16. #ifdef HAVE_SYS_TYPES_H
  17. #include <sys/types.h>
  18. #endif
  19. #ifdef HAVE_LIMITS_H
  20. #include <limits.h>
  21. #endif
  22. #ifdef HAVE_SYS_LIMITS_H
  23. #include <sys/limits.h>
  24. #endif
  25. #ifdef HAVE_MACHINE_LIMITS_H
  26. #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
  27. /* FreeBSD has a bug where it complains that this file is obsolete,
  28. and I should migrate to using sys/limits. It complains even when
  29. I include both.
  30. __FreeBSD_kernel__ is defined by Debian GNU/kFreeBSD which
  31. does the same thing (but doesn't defined __FreeBSD__).
  32. */
  33. #include <machine/limits.h>
  34. #endif
  35. #endif
  36. #ifdef HAVE_INTTYPES_H
  37. #include <inttypes.h>
  38. #endif
  39. #ifdef MS_WINDOWS
  40. /* Windows likes to capitalize ssize_t. Charming. */
  41. #define ssize_t SSIZE_T
  42. #endif
  43. #if (SIZEOF_INT8_T != 0)
  44. #define HAVE_INT8_T
  45. #endif
  46. #if (SIZEOF_INT16_T != 0)
  47. #define HAVE_INT16_T
  48. #endif
  49. #if (SIZEOF_INT32_T != 0)
  50. #define HAVE_INT32_T
  51. #endif
  52. #if (SIZEOF_INT64_T != 0)
  53. #define HAVE_INT64_T
  54. #endif
  55. #if (SIZEOF_UINT8_T != 0)
  56. #define HAVE_UINT8_T
  57. #endif
  58. #if (SIZEOF_UINT16_T != 0)
  59. #define HAVE_UINT16_T
  60. #endif
  61. #if (SIZEOF_UINT32_T != 0)
  62. #define HAVE_UINT32_T
  63. #endif
  64. #if (SIZEOF_UINT64_T != 0)
  65. #define HAVE_UINT64_T
  66. #endif
  67. #if (SIZEOF_INTPTR_T != 0)
  68. #define HAVE_INTPTR_T
  69. #endif
  70. #if (SIZEOF_UINTPTR_T != 0)
  71. #define HAVE_UINTPTR_T
  72. #endif
  73. #if (SIZEOF_CHAR == 1)
  74. #ifndef HAVE_INT8_T
  75. typedef signed char int8_t;
  76. #define HAVE_INT8_T
  77. #endif
  78. #ifndef HAVE_UINT8_T
  79. typedef unsigned char uint8_t;
  80. #define HAVE_UINT8_T
  81. #endif
  82. #endif
  83. #if (SIZEOF_SHORT == 2)
  84. #ifndef HAVE_INT16_T
  85. typedef signed short int16_t;
  86. #define HAVE_INT16_T
  87. #endif
  88. #ifndef HAVE_UINT16_T
  89. typedef unsigned short uint16_t;
  90. #define HAVE_UINT16_T
  91. #endif
  92. #endif
  93. #if (SIZEOF_INT == 2)
  94. #ifndef HAVE_INT16_T
  95. typedef signed int int16_t;
  96. #define HAVE_INT16_T
  97. #endif
  98. #ifndef HAVE_UINT16_T
  99. typedef unsigned int uint16_t;
  100. #define HAVE_UINT16_T
  101. #endif
  102. #elif (SIZEOF_INT == 4)
  103. #ifndef HAVE_INT32_T
  104. typedef signed int int32_t;
  105. #define HAVE_INT32_T
  106. #endif
  107. #ifndef HAVE_UINT32_T
  108. typedef unsigned int uint32_t;
  109. #define HAVE_UINT32_T
  110. #endif
  111. #ifndef UINT32_MAX
  112. #define UINT32_MAX 0xffffffffu
  113. #endif
  114. #endif
  115. #if (SIZEOF_LONG == 4)
  116. #ifndef HAVE_INT32_T
  117. typedef signed long int32_t;
  118. #define HAVE_INT32_T
  119. #endif
  120. #ifndef HAVE_UINT32_T
  121. typedef unsigned long uint32_t;
  122. #define HAVE_UINT32_T
  123. #ifndef UINT32_MAX
  124. #define UINT32_MAX 0xfffffffful
  125. #endif
  126. #endif
  127. #elif (SIZEOF_LONG == 8)
  128. #ifndef HAVE_INT64_T
  129. typedef signed long int64_t;
  130. #define HAVE_INT64_T
  131. #endif
  132. #ifndef HAVE_UINT32_T
  133. typedef unsigned long uint64_t;
  134. #define HAVE_UINT32_T
  135. #endif
  136. #ifndef UINT64_MAX
  137. #define UINT64_MAX 0xfffffffffffffffful
  138. #endif
  139. #endif
  140. #if (SIZEOF_LONG_LONG == 8)
  141. #ifndef HAVE_INT64_T
  142. typedef signed long long int64_t;
  143. #define HAVE_INT64_T
  144. #endif
  145. #ifndef HAVE_UINT64_T
  146. typedef unsigned long long uint64_t;
  147. #define HAVE_UINT64_T
  148. #endif
  149. #ifndef UINT64_MAX
  150. #define UINT64_MAX 0xffffffffffffffffull
  151. #endif
  152. #ifndef INT64_MAX
  153. #define INT64_MAX 0x7fffffffffffffffll
  154. #endif
  155. #endif
  156. #if (SIZEOF___INT64 == 8)
  157. #ifndef HAVE_INT64_T
  158. typedef signed __int64 int64_t;
  159. #define HAVE_INT64_T
  160. #endif
  161. #ifndef HAVE_UINT64_T
  162. typedef unsigned __int64 uint64_t;
  163. #define HAVE_UINT64_T
  164. #endif
  165. #ifndef UINT64_MAX
  166. #define UINT64_MAX 0xffffffffffffffffui64
  167. #endif
  168. #ifndef INT64_MAX
  169. #define INT64_MAX 0x7fffffffffffffffi64
  170. #endif
  171. #endif
  172. #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
  173. #ifndef HAVE_INTPTR_T
  174. typedef int64_t intptr_t;
  175. #endif
  176. #ifndef HAVE_UINTPTR_T
  177. typedef uint64_t uintptr_t;
  178. #endif
  179. #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
  180. #ifndef HAVE_INTPTR_T
  181. typedef int32_t intptr_t;
  182. #endif
  183. #ifndef HAVE_UINTPTR_T
  184. typedef uint32_t uintptr_t;
  185. #endif
  186. #else
  187. #error "void * is either >8 bytes or <= 2. In either case, I am confused."
  188. #endif
  189. #ifndef HAVE_INT8_T
  190. #error "Missing type int8_t"
  191. #endif
  192. #ifndef HAVE_UINT8_T
  193. #error "Missing type uint8_t"
  194. #endif
  195. #ifndef HAVE_INT16_T
  196. #error "Missing type int16_t"
  197. #endif
  198. #ifndef HAVE_UINT16_T
  199. #error "Missing type uint16_t"
  200. #endif
  201. #ifndef HAVE_INT32_T
  202. #error "Missing type int32_t"
  203. #endif
  204. #ifndef HAVE_UINT32_T
  205. #error "Missing type uint32_t"
  206. #endif
  207. #ifndef HAVE_INT64_T
  208. #error "Missing type int64_t"
  209. #endif
  210. #ifndef HAVE_UINT64_T
  211. #error "Missing type uint64_t"
  212. #endif
  213. /* This assumes a sane (2's-complement) representation. But if you
  214. * aren't 2's complement, and you don't define LONG_MAX, then you're so
  215. * bizarre that I want nothing to do with you. */
  216. #ifndef USING_TWOS_COMPLEMENT
  217. #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
  218. #endif
  219. #ifndef LONG_MAX
  220. #if (SIZEOF_LONG == 4)
  221. #define LONG_MAX 0x7fffffffL
  222. #elif (SIZEOF_LONG == 8)
  223. #define LONG_MAX 0x7fffffffffffffffL
  224. #else
  225. #error "Can't define LONG_MAX"
  226. #endif
  227. #endif
  228. #ifndef INT_MAX
  229. #if (SIZEOF_INT == 4)
  230. #define INT_MAX 0x7fffffffL
  231. #elif (SIZEOF_INT == 8)
  232. #define INT_MAX 0x7fffffffffffffffL
  233. #else
  234. #error "Can't define INT_MAX"
  235. #endif
  236. #endif
  237. #ifndef UINT_MAX
  238. #if (SIZEOF_INT == 2)
  239. #define UINT_MAX 0xffffu
  240. #elif (SIZEOF_INT == 4)
  241. #define UINT_MAX 0xffffffffu
  242. #elif (SIZEOF_INT == 8)
  243. #define UINT_MAX 0xffffffffffffffffu
  244. #else
  245. #error "Can't define UINT_MAX"
  246. #endif
  247. #endif
  248. #ifndef TIME_MAX
  249. #ifdef TIME_T_IS_SIGNED
  250. #if (SIZEOF_TIME_T == SIZEOF_INT)
  251. #define TIME_MAX ((time_t)INT_MAX)
  252. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  253. #define TIME_MAX ((time_t)LONG_MAX)
  254. #else
  255. #error "Can't define (signed) TIME_MAX"
  256. #endif
  257. #else
  258. /* Unsigned case */
  259. #if (SIZEOF_TIME_T == 4)
  260. #define TIME_MAX ((time_t)UINT32_MAX)
  261. #elif (SIZEOF_TIME_T == 8)
  262. #define TIME_MAX ((time_t)UINT64_MAX)
  263. #else
  264. #error "Can't define (unsigned) TIME_MAX"
  265. #endif
  266. #endif /* time_t_is_signed */
  267. #endif /* ifndef(TIME_MAX) */
  268. #ifndef SIZE_T_MAX
  269. #if (SIZEOF_SIZE_T == 4)
  270. #define SIZE_T_MAX 0xfffffffful
  271. #elif (SIZEOF_SIZE_T == 8)
  272. #define SIZE_T_MAX 0xfffffffffffffffful
  273. #else
  274. #error "Can't define SIZE_T_MAX"
  275. #endif
  276. #endif
  277. /* Any size_t larger than this amount is likely to be an underflow. */
  278. #define SIZE_T_CEILING (sizeof(char)<<(sizeof(size_t)*8 - 1))
  279. #endif /* __TORINT_H */