1 /*
2  * Copyright 2013 MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef BSON_COMPAT_H
19 #define BSON_COMPAT_H
20 
21 
22 #if !defined(BSON_INSIDE) && !defined(BSON_COMPILATION)
23 #error "Only <bson.h> can be included directly."
24 #endif
25 
26 
27 #if defined(__MINGW32__)
28 #if defined(__USE_MINGW_ANSI_STDIO)
29 #if __USE_MINGW_ANSI_STDIO < 1
30 #error "__USE_MINGW_ANSI_STDIO > 0 is required for correct PRI* macros"
31 #endif
32 #else
33 #define __USE_MINGW_ANSI_STDIO 1
34 #endif
35 #endif
36 
37 #include "bson-config.h"
38 #include "bson-macros.h"
39 
40 
41 #ifdef BSON_OS_WIN32
42 #if defined(_WIN32_WINNT) && (_WIN32_WINNT < 0x0600)
43 #undef _WIN32_WINNT
44 #endif
45 #ifndef _WIN32_WINNT
46 #define _WIN32_WINNT 0x0600
47 #endif
48 #ifndef NOMINMAX
49 #define NOMINMAX
50 #endif
51 #include <winsock2.h>
52 #ifndef WIN32_LEAN_AND_MEAN
53 #define WIN32_LEAN_AND_MEAN
54 #include <windows.h>
55 #undef WIN32_LEAN_AND_MEAN
56 #else
57 #include <windows.h>
58 #endif
59 #include <direct.h>
60 #include <io.h>
61 #endif
62 
63 
64 #ifdef BSON_OS_UNIX
65 #include <unistd.h>
66 #include <sys/time.h>
67 #endif
68 
69 
70 #include "bson-macros.h"
71 
72 
73 #include <errno.h>
74 #include <ctype.h>
75 #include <limits.h>
76 #include <fcntl.h>
77 #include <sys/stat.h>
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <time.h>
83 
84 
85 BSON_BEGIN_DECLS
86 
87 
88 #ifdef _MSC_VER
89 #include <time.h>
90 #include "bson-stdint-win32.h"
91 #ifndef __cplusplus
92 /* benign redefinition of type */
93 #pragma warning(disable : 4142)
94 #ifndef _SSIZE_T_DEFINED
95 #define _SSIZE_T_DEFINED
96 typedef SSIZE_T ssize_t;
97 #endif
98 #ifndef _SIZE_T_DEFINED
99 #define _SIZE_T_DEFINED
100 typedef SIZE_T size_t;
101 #endif
102 #pragma warning(default : 4142)
103 #else
104 /*
105  * MSVC++ does not include ssize_t, just size_t.
106  * So we need to synthesize that as well.
107  */
108 #pragma warning(disable : 4142)
109 #ifndef _SSIZE_T_DEFINED
110 #define _SSIZE_T_DEFINED
111 typedef SSIZE_T ssize_t;
112 #endif
113 #pragma warning(default : 4142)
114 #endif
115 #define PRIi32 "d"
116 #define PRId32 "d"
117 #define PRIu32 "u"
118 #define PRIi64 "I64i"
119 #define PRId64 "I64i"
120 #define PRIu64 "I64u"
121 #else
122 #include "bson-stdint.h"
123 #include <inttypes.h>
124 #endif
125 
126 #if defined(__MINGW32__) && !defined(INIT_ONCE_STATIC_INIT)
127 #define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT
128 typedef RTL_RUN_ONCE INIT_ONCE;
129 #endif
130 
131 #ifdef BSON_HAVE_STDBOOL_H
132 #include <stdbool.h>
133 #elif !defined(__bool_true_false_are_defined)
134 #ifndef __cplusplus
135 typedef signed char bool;
136 #define false 0
137 #define true 1
138 #endif
139 #define __bool_true_false_are_defined 1
140 #endif
141 
142 
143 #if defined(__GNUC__)
144 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
145 #define bson_sync_synchronize() __sync_synchronize ()
146 #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
147    defined(__i686__) || defined(__x86_64__)
148 #define bson_sync_synchronize() asm volatile("mfence" ::: "memory")
149 #else
150 #define bson_sync_synchronize() asm volatile("sync" ::: "memory")
151 #endif
152 #elif defined(_MSC_VER)
153 #define bson_sync_synchronize() MemoryBarrier ()
154 #endif
155 
156 
157 #if !defined(va_copy) && defined(__va_copy)
158 #define va_copy(dst, src) __va_copy (dst, src)
159 #endif
160 
161 
162 #if !defined(va_copy)
163 #define va_copy(dst, src) ((dst) = (src))
164 #endif
165 
166 
167 BSON_END_DECLS
168 
169 
170 #endif /* BSON_COMPAT_H */
171