sysdep.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * MessagePack system dependencies
  3. *
  4. * Copyright (C) 2008-2010 FURUHASHI Sadayuki
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MSGPACK_SYSDEP_H__
  19. #define MSGPACK_SYSDEP_H__
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22. #if defined(_MSC_VER) && _MSC_VER < 1600
  23. typedef __int8 int8_t;
  24. typedef unsigned __int8 uint8_t;
  25. typedef __int16 int16_t;
  26. typedef unsigned __int16 uint16_t;
  27. typedef __int32 int32_t;
  28. typedef unsigned __int32 uint32_t;
  29. typedef __int64 int64_t;
  30. typedef unsigned __int64 uint64_t;
  31. #elif defined(_MSC_VER) // && _MSC_VER >= 1600
  32. #include <stdint.h>
  33. #else
  34. #include <stdint.h>
  35. #include <stdbool.h>
  36. #endif
  37. #ifdef _WIN32
  38. #define _msgpack_atomic_counter_header <windows.h>
  39. typedef long _msgpack_atomic_counter_t;
  40. #define _msgpack_sync_decr_and_fetch(ptr) InterlockedDecrement(ptr)
  41. #define _msgpack_sync_incr_and_fetch(ptr) InterlockedIncrement(ptr)
  42. #elif defined(__GNUC__) && ((__GNUC__*10 + __GNUC_MINOR__) < 41)
  43. #define _msgpack_atomic_counter_header "gcc_atomic.h"
  44. #else
  45. typedef unsigned int _msgpack_atomic_counter_t;
  46. #define _msgpack_sync_decr_and_fetch(ptr) __sync_sub_and_fetch(ptr, 1)
  47. #define _msgpack_sync_incr_and_fetch(ptr) __sync_add_and_fetch(ptr, 1)
  48. #endif
  49. #ifdef _WIN32
  50. #ifdef __cplusplus
  51. /* numeric_limits<T>::min,max */
  52. #ifdef max
  53. #undef max
  54. #endif
  55. #ifdef min
  56. #undef min
  57. #endif
  58. #endif
  59. #else
  60. #include <arpa/inet.h> /* __BYTE_ORDER */
  61. #endif
  62. #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
  63. #if __BYTE_ORDER == __LITTLE_ENDIAN
  64. #define __LITTLE_ENDIAN__
  65. #elif __BYTE_ORDER == __BIG_ENDIAN
  66. #define __BIG_ENDIAN__
  67. #elif _WIN32
  68. #define __LITTLE_ENDIAN__
  69. #endif
  70. #endif
  71. #ifdef __LITTLE_ENDIAN__
  72. #ifdef _WIN32
  73. # if defined(ntohs)
  74. # define _msgpack_be16(x) ntohs(x)
  75. # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400)
  76. # define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
  77. # else
  78. # define _msgpack_be16(x) ( \
  79. ((((uint16_t)x) << 8) ) | \
  80. ((((uint16_t)x) >> 8) ) )
  81. # endif
  82. #else
  83. # define _msgpack_be16(x) ntohs(x)
  84. #endif
  85. #ifdef _WIN32
  86. # if defined(ntohl)
  87. # define _msgpack_be32(x) ntohl(x)
  88. # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400)
  89. # define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x))
  90. # else
  91. # define _msgpack_be32(x) \
  92. ( ((((uint32_t)x) << 24) ) | \
  93. ((((uint32_t)x) << 8) & 0x00ff0000U ) | \
  94. ((((uint32_t)x) >> 8) & 0x0000ff00U ) | \
  95. ((((uint32_t)x) >> 24) ) )
  96. # endif
  97. #else
  98. # define _msgpack_be32(x) ntohl(x)
  99. #endif
  100. #if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400)
  101. # define _msgpack_be64(x) (_byteswap_uint64(x))
  102. #elif defined(bswap_64)
  103. # define _msgpack_be64(x) bswap_64(x)
  104. #elif defined(__DARWIN_OSSwapInt64)
  105. # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x)
  106. #else
  107. #define _msgpack_be64(x) \
  108. ( ((((uint64_t)x) << 56) ) | \
  109. ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
  110. ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
  111. ((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
  112. ((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
  113. ((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
  114. ((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
  115. ((((uint64_t)x) >> 56) ) )
  116. #endif
  117. #define _msgpack_load16(cast, from) ((cast)( \
  118. (((uint16_t)((uint8_t*)(from))[0]) << 8) | \
  119. (((uint16_t)((uint8_t*)(from))[1]) ) ))
  120. #define _msgpack_load32(cast, from) ((cast)( \
  121. (((uint32_t)((uint8_t*)(from))[0]) << 24) | \
  122. (((uint32_t)((uint8_t*)(from))[1]) << 16) | \
  123. (((uint32_t)((uint8_t*)(from))[2]) << 8) | \
  124. (((uint32_t)((uint8_t*)(from))[3]) ) ))
  125. #define _msgpack_load64(cast, from) ((cast)( \
  126. (((uint64_t)((uint8_t*)(from))[0]) << 56) | \
  127. (((uint64_t)((uint8_t*)(from))[1]) << 48) | \
  128. (((uint64_t)((uint8_t*)(from))[2]) << 40) | \
  129. (((uint64_t)((uint8_t*)(from))[3]) << 32) | \
  130. (((uint64_t)((uint8_t*)(from))[4]) << 24) | \
  131. (((uint64_t)((uint8_t*)(from))[5]) << 16) | \
  132. (((uint64_t)((uint8_t*)(from))[6]) << 8) | \
  133. (((uint64_t)((uint8_t*)(from))[7]) ) ))
  134. #else
  135. #define _msgpack_be16(x) (x)
  136. #define _msgpack_be32(x) (x)
  137. #define _msgpack_be64(x) (x)
  138. #define _msgpack_load16(cast, from) ((cast)( \
  139. (((uint16_t)((uint8_t*)from)[0]) << 8) | \
  140. (((uint16_t)((uint8_t*)from)[1]) ) ))
  141. #define _msgpack_load32(cast, from) ((cast)( \
  142. (((uint32_t)((uint8_t*)from)[0]) << 24) | \
  143. (((uint32_t)((uint8_t*)from)[1]) << 16) | \
  144. (((uint32_t)((uint8_t*)from)[2]) << 8) | \
  145. (((uint32_t)((uint8_t*)from)[3]) ) ))
  146. #define _msgpack_load64(cast, from) ((cast)( \
  147. (((uint64_t)((uint8_t*)from)[0]) << 56) | \
  148. (((uint64_t)((uint8_t*)from)[1]) << 48) | \
  149. (((uint64_t)((uint8_t*)from)[2]) << 40) | \
  150. (((uint64_t)((uint8_t*)from)[3]) << 32) | \
  151. (((uint64_t)((uint8_t*)from)[4]) << 24) | \
  152. (((uint64_t)((uint8_t*)from)[5]) << 16) | \
  153. (((uint64_t)((uint8_t*)from)[6]) << 8) | \
  154. (((uint64_t)((uint8_t*)from)[7]) ) ))
  155. #endif
  156. #define _msgpack_store16(to, num) \
  157. do { uint16_t val = _msgpack_be16(num); memcpy(to, &val, 2); } while(0)
  158. #define _msgpack_store32(to, num) \
  159. do { uint32_t val = _msgpack_be32(num); memcpy(to, &val, 4); } while(0)
  160. #define _msgpack_store64(to, num) \
  161. do { uint64_t val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0)
  162. /*
  163. #define _msgpack_load16(cast, from) \
  164. ({ cast val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); })
  165. #define _msgpack_load32(cast, from) \
  166. ({ cast val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); })
  167. #define _msgpack_load64(cast, from) \
  168. ({ cast val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); })
  169. */
  170. #endif /* msgpack/sysdep.h */