torint.h 5.4 KB

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