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