MessagePack for C++
sysdep.hpp
Go to the documentation of this file.
1 /*
2  * MessagePack system dependencies
3  *
4  * Copyright (C) 2008-2010 FURUHASHI Sadayuki
5  *
6  * Distributed under the Boost Software License, Version 1.0.
7  * (See accompanying file LICENSE_1_0.txt or copy at
8  * http://www.boost.org/LICENSE_1_0.txt)
9  */
10 #ifndef MSGPACK_SYSDEP_HPP
11 #define MSGPACK_SYSDEP_HPP
12 
13 #include <stdlib.h>
14 #include <stddef.h>
15 
16 #if defined(_MSC_VER) && _MSC_VER <= 1800
17 # define snprintf(buf, len, format,...) _snprintf_s(buf, len, _TRUNCATE, format, __VA_ARGS__)
18 #endif
19 
20 #if defined(_MSC_VER) && _MSC_VER < 1600
21  typedef signed __int8 int8_t;
22  typedef unsigned __int8 uint8_t;
23  typedef signed __int16 int16_t;
24  typedef unsigned __int16 uint16_t;
25  typedef signed __int32 int32_t;
26  typedef unsigned __int32 uint32_t;
27  typedef signed __int64 int64_t;
28  typedef unsigned __int64 uint64_t;
29 # if defined(_WIN64)
30  typedef signed __int64 intptr_t;
31  typedef unsigned __int64 uintptr_t;
32 # else
33  typedef signed __int32 intptr_t;
34  typedef unsigned __int32 uintptr_t;
35 # endif
36 #elif defined(_MSC_VER) // && _MSC_VER >= 1600
37 # include <stdint.h>
38 #else
39 # include <stdint.h>
40 # include <stdbool.h>
41 #endif
42 
43 #if !defined(MSGPACK_DLLEXPORT)
44 #if defined(_MSC_VER)
45 # define MSGPACK_DLLEXPORT __declspec(dllexport)
46 #else /* _MSC_VER */
47 # define MSGPACK_DLLEXPORT
48 #endif /* _MSC_VER */
49 #endif
50 
51 #ifdef _WIN32
52 # if defined(_KERNEL_MODE)
53 # define _msgpack_atomic_counter_header <ntddk.h>
54 # else
55 # define _msgpack_atomic_counter_header <windows.h>
56 # if !defined(WIN32_LEAN_AND_MEAN)
57 # define WIN32_LEAN_AND_MEAN
58 # endif /* WIN32_LEAN_AND_MEAN */
59 # endif
60  typedef long _msgpack_atomic_counter_t;
61 #if defined(_AMD64_) || defined(_M_X64) || defined(_M_ARM64)
62 # define _msgpack_sync_decr_and_fetch(ptr) _InterlockedDecrement(ptr)
63 # define _msgpack_sync_incr_and_fetch(ptr) _InterlockedIncrement(ptr)
64 #else
65 # define _msgpack_sync_decr_and_fetch(ptr) InterlockedDecrement(ptr)
66 # define _msgpack_sync_incr_and_fetch(ptr) InterlockedIncrement(ptr)
67 #endif
68 #elif defined(__GNUC__) && ((__GNUC__*10 + __GNUC_MINOR__) < 41)
69 
70 # define _msgpack_atomic_counter_header "msgpack/gcc_atomic.hpp"
71 
72 #else
73  typedef unsigned int _msgpack_atomic_counter_t;
74 # define _msgpack_sync_decr_and_fetch(ptr) __sync_sub_and_fetch(ptr, 1)
75 # define _msgpack_sync_incr_and_fetch(ptr) __sync_add_and_fetch(ptr, 1)
76 #endif
77 
78 #ifdef _WIN32
79 
80  /* numeric_limits<T>::min,max */
81 # ifdef max
82 # undef max
83 # endif
84 # ifdef min
85 # undef min
86 # endif
87 
88 #elif defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
89 
90 #include <arpa/inet.h> /* __BYTE_ORDER */
91 # if defined(linux)
92 # include <byteswap.h>
93 # endif
94 
95 #endif
96 
97 #if !defined(MSGPACK_ENDIAN_LITTLE_BYTE) && !defined(MSGPACK_ENDIAN_BIG_BYTE)
98 #include <boost/predef/other/endian.h>
99 #define MSGPACK_ENDIAN_LITTLE_BYTE BOOST_ENDIAN_LITTLE_BYTE
100 #define MSGPACK_ENDIAN_BIG_BYTE BOOST_ENDIAN_BIG_BYTE
101 #endif // !defined(MSGPACK_ENDIAN_LITTLE_BYTE) && !defined(MSGPACK_ENDIAN_BIG_BYTE)
102 
103 #if MSGPACK_ENDIAN_LITTLE_BYTE
104 
105 # if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
106 # define _msgpack_be16(x) ntohs((uint16_t)x)
107 # else
108 # if defined(ntohs)
109 # define _msgpack_be16(x) ntohs(x)
110 # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400)
111 # define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
112 # else
113 # define _msgpack_be16(x) ( \
114  ((((uint16_t)x) << 8) ) | \
115  ((((uint16_t)x) >> 8) ) )
116 # endif
117 # endif
118 
119 # if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
120 # define _msgpack_be32(x) ntohl((uint32_t)x)
121 # else
122 # if defined(ntohl)
123 # define _msgpack_be32(x) ntohl(x)
124 # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400)
125 # define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x))
126 # else
127 # define _msgpack_be32(x) \
128  ( ((((uint32_t)x) << 24) ) | \
129  ((((uint32_t)x) << 8) & 0x00ff0000U ) | \
130  ((((uint32_t)x) >> 8) & 0x0000ff00U ) | \
131  ((((uint32_t)x) >> 24) ) )
132 # endif
133 # endif
134 
135 # if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400)
136 # define _msgpack_be64(x) (_byteswap_uint64(x))
137 # elif defined(bswap_64)
138 # define _msgpack_be64(x) bswap_64(x)
139 # elif defined(__DARWIN_OSSwapInt64)
140 # define _msgpack_be64(x) __DARWIN_OSSwapInt64(x)
141 # else
142 # define _msgpack_be64(x) \
143  ( ((((uint64_t)x) << 56) ) | \
144  ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
145  ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
146  ((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
147  ((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
148  ((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
149  ((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
150  ((((uint64_t)x) >> 56) ) )
151 # endif
152 
153 #elif MSGPACK_ENDIAN_BIG_BYTE
154 
155 # define _msgpack_be16(x) (x)
156 # define _msgpack_be32(x) (x)
157 # define _msgpack_be64(x) (x)
158 
159 #else
160 # error msgpack-c supports only big endian and little endian
161 #endif /* MSGPACK_ENDIAN_LITTLE_BYTE */
162 
163 #define _msgpack_load16(cast, from, to) do { \
164  memcpy((cast*)(to), (from), sizeof(cast)); \
165  *(to) = (cast)_msgpack_be16(*(to)); \
166  } while (0);
167 
168 #define _msgpack_load32(cast, from, to) do { \
169  memcpy((cast*)(to), (from), sizeof(cast)); \
170  *(to) = (cast)_msgpack_be32(*(to)); \
171  } while (0);
172 #define _msgpack_load64(cast, from, to) do { \
173  memcpy((cast*)(to), (from), sizeof(cast)); \
174  *(to) = (cast)_msgpack_be64(*(to)); \
175  } while (0);
176 
177 #define _msgpack_store16(to, num) \
178  do { uint16_t val = _msgpack_be16(num); memcpy(to, &val, 2); } while(0)
179 #define _msgpack_store32(to, num) \
180  do { uint32_t val = _msgpack_be32(num); memcpy(to, &val, 4); } while(0)
181 #define _msgpack_store64(to, num) \
182  do { uint64_t val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0)
183 
184 /*
185 #define _msgpack_load16(cast, from) \
186  ({ cast val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); })
187 #define _msgpack_load32(cast, from) \
188  ({ cast val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); })
189 #define _msgpack_load64(cast, from) \
190  ({ cast val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); })
191 */
192 
193 
194 #ifdef __APPLE__
195 # include <TargetConditionals.h>
196 #endif
197 
198 #endif /* msgpack/sysdep.hpp */
unsigned int _msgpack_atomic_counter_t
Definition: sysdep.hpp:73