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