torint.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Copyright 2003 Roger Dingledine
  2. * Copyright 2004-2006 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. #if (SIZEOF_INT8_T != 0)
  40. #define HAVE_INT8_T
  41. #endif
  42. #if (SIZEOF_INT16_T != 0)
  43. #define HAVE_INT16_T
  44. #endif
  45. #if (SIZEOF_INT32_T != 0)
  46. #define HAVE_INT32_T
  47. #endif
  48. #if (SIZEOF_INT64_T != 0)
  49. #define HAVE_INT64_T
  50. #endif
  51. #if (SIZEOF_UINT8_T != 0)
  52. #define HAVE_UINT8_T
  53. #endif
  54. #if (SIZEOF_UINT16_T != 0)
  55. #define HAVE_UINT16_T
  56. #endif
  57. #if (SIZEOF_UINT32_T != 0)
  58. #define HAVE_UINT32_T
  59. #endif
  60. #if (SIZEOF_UINT64_T != 0)
  61. #define HAVE_UINT64_T
  62. #endif
  63. #if (SIZEOF_INTPTR_T != 0)
  64. #define HAVE_INTPTR_T
  65. #endif
  66. #if (SIZEOF_UINTPTR_T != 0)
  67. #define HAVE_UINTPTR_T
  68. #endif
  69. #if (SIZEOF_CHAR == 1)
  70. #ifndef HAVE_INT8_T
  71. typedef signed char int8_t;
  72. #define HAVE_INT8_T
  73. #endif
  74. #ifndef HAVE_UINT8_T
  75. typedef unsigned char uint8_t;
  76. #define HAVE_UINT8_T
  77. #endif
  78. #endif
  79. #if (SIZEOF_SHORT == 2)
  80. #ifndef HAVE_INT16_T
  81. typedef signed short int16_t;
  82. #define HAVE_INT16_T
  83. #endif
  84. #ifndef HAVE_UINT16_T
  85. typedef unsigned short uint16_t;
  86. #define HAVE_UINT16_T
  87. #endif
  88. #endif
  89. #if (SIZEOF_INT == 2)
  90. #ifndef HAVE_INT16_T
  91. typedef signed int int16_t;
  92. #define HAVE_INT16_T
  93. #endif
  94. #ifndef HAVE_UINT16_T
  95. typedef unsigned int uint16_t;
  96. #define HAVE_UINT16_T
  97. #endif
  98. #elif (SIZEOF_INT == 4)
  99. #ifndef HAVE_INT32_T
  100. typedef signed int int32_t;
  101. #define HAVE_INT32_T
  102. #endif
  103. #ifndef HAVE_UINT32_T
  104. typedef unsigned int uint32_t;
  105. #define HAVE_UINT32_T
  106. #endif
  107. #ifndef UINT32_MAX
  108. #define UINT32_MAX 0xffffffffu
  109. #endif
  110. #endif
  111. #if (SIZEOF_LONG == 4)
  112. #ifndef HAVE_INT32_T
  113. typedef signed long int32_t;
  114. #define HAVE_INT32_T
  115. #endif
  116. #ifndef HAVE_UINT32_T
  117. typedef unsigned long uint32_t;
  118. #define HAVE_UINT32_T
  119. #ifndef UINT32_MAX
  120. #define UINT32_MAX 0xfffffffful
  121. #endif
  122. #endif
  123. #elif (SIZEOF_LONG == 8)
  124. #ifndef HAVE_INT64_T
  125. typedef signed long int64_t;
  126. #define HAVE_INT64_T
  127. #endif
  128. #ifndef HAVE_UINT32_T
  129. typedef unsigned long uint64_t;
  130. #define HAVE_UINT32_T
  131. #endif
  132. #ifndef UINT64_MAX
  133. #define UINT64_MAX 0xfffffffffffffffful
  134. #endif
  135. #endif
  136. #if (SIZEOF_LONG_LONG == 8)
  137. #ifndef HAVE_INT64_T
  138. typedef signed long long int64_t;
  139. #define HAVE_INT64_T
  140. #endif
  141. #ifndef HAVE_UINT64_T
  142. typedef unsigned long long uint64_t;
  143. #define HAVE_UINT64_T
  144. #endif
  145. #ifndef UINT64_MAX
  146. #define UINT64_MAX 0xffffffffffffffffull
  147. #endif
  148. #ifndef INT64_MAX
  149. #define INT64_MAX 0x7fffffffffffffffll
  150. #endif
  151. #endif
  152. #if (SIZEOF___INT64 == 8)
  153. #ifndef HAVE_INT64_T
  154. typedef signed __int64 int64_t;
  155. #define HAVE_INT64_T
  156. #endif
  157. #ifndef HAVE_UINT64_T
  158. typedef unsigned __int64 uint64_t;
  159. #define HAVE_UINT64_T
  160. #endif
  161. #ifndef UINT64_MAX
  162. #define UINT64_MAX 0xffffffffffffffffui64
  163. #endif
  164. #ifndef INT64_MAX
  165. #define INT64_MAX 0x7fffffffffffffffi64
  166. #endif
  167. #endif
  168. #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
  169. #ifndef HAVE_INTPTR_T
  170. typedef int64_t intptr_t;
  171. #endif
  172. #ifndef HAVE_UINTPTR_T
  173. typedef uint64_t uintptr_t;
  174. #endif
  175. #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
  176. #ifndef HAVE_INTPTR_T
  177. typedef int32_t intptr_t;
  178. #endif
  179. #ifndef HAVE_UINTPTR_T
  180. typedef uint32_t uintptr_t;
  181. #endif
  182. #else
  183. #error "void * is either >8 bytes or <= 2. In either case, I am confused."
  184. #endif
  185. #ifndef HAVE_INT8_T
  186. #error "Missing type int8_t"
  187. #endif
  188. #ifndef HAVE_UINT8_T
  189. #error "Missing type uint8_t"
  190. #endif
  191. #ifndef HAVE_INT16_T
  192. #error "Missing type int16_t"
  193. #endif
  194. #ifndef HAVE_UINT16_T
  195. #error "Missing type uint16_t"
  196. #endif
  197. #ifndef HAVE_INT32_T
  198. #error "Missing type int32_t"
  199. #endif
  200. #ifndef HAVE_UINT32_T
  201. #error "Missing type uint32_t"
  202. #endif
  203. #ifndef HAVE_INT64_T
  204. #error "Missing type int64_t"
  205. #endif
  206. #ifndef HAVE_UINT64_T
  207. #error "Missing type uint64_t"
  208. #endif
  209. /* XXXX This assumes a sane (2's-complement) representation. But if you
  210. * aren't 2's complement, and you don't define LONG_MAX, then you're so
  211. * bizarre that I want nothing to do with you. */
  212. #ifndef LONG_MAX
  213. #if (SIZEOF_LONG == 4)
  214. #define LONG_MAX 0x7fffffffL
  215. #elif (SIZEOF_LONG == 8)
  216. #define LONG_MAX 0x7fffffffffffffffL
  217. #else
  218. #error "Can't define LONG_MAX"
  219. #endif
  220. #endif
  221. #ifndef INT_MAX
  222. #if (SIZEOF_INT == 4)
  223. #define INT_MAX 0x7fffffffL
  224. #elif (SIZEOF_INT == 8)
  225. #define INT_MAX 0x7fffffffffffffffL
  226. #else
  227. #error "Can't define INT_MAX"
  228. #endif
  229. #endif
  230. #ifndef UINT_MAX
  231. #if (SIZEOF_INT == 2)
  232. #define UINT_MAX 0xffffu
  233. #elif (SIZEOF_INT == 4)
  234. #define UINT_MAX 0xffffffffu
  235. #elif (SIZEOF_INT == 8)
  236. #define UINT_MAX 0xffffffffffffffffu
  237. #else
  238. #error "Can't define UINT_MAX"
  239. #endif
  240. #endif
  241. #ifndef TIME_MAX
  242. #ifdef TIME_T_IS_SIGNED
  243. #if (SIZEOF_TIME_T == SIZEOF_INT)
  244. #define TIME_MAX ((time_t)INT_MAX)
  245. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  246. #define TIME_MAX ((time_t)LONG_MAX)
  247. #else
  248. #error "Can't define (signed) TIME_MAX"
  249. #endif
  250. #else
  251. /* Unsigned case */
  252. #if (SIZEOF_TIME_T == 4)
  253. #define TIME_MAX ((time_t)UINT32_MAX)
  254. #elif (SIZEOF_TIME_T == 8)
  255. #define TIME_MAX ((time_t)UINT64_MAX)
  256. #else
  257. #error "Can't define (unsigned) TIME_MAX"
  258. #endif
  259. #endif /* time_t_is_signed */
  260. #endif /* ifndef(TIME_MAX) */
  261. /* Any size_t larger than this amount is likely to be an underflow. */
  262. #define SIZE_T_CEILING (sizeof(char)<<(sizeof(size_t)*8 - 1))
  263. #endif /* __TORINT_H */