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 <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <time.h>
82 
83 
84 BSON_BEGIN_DECLS
85 
86 
87 #ifdef _MSC_VER
88 # include "bson-stdint-win32.h"
89 # ifndef __cplusplus
90    /* benign redefinition of type */
91 #  pragma warning (disable :4142)
92 #  ifndef _SSIZE_T_DEFINED
93 #   define _SSIZE_T_DEFINED
94     typedef SSIZE_T ssize_t;
95 #  endif
96     typedef SIZE_T size_t;
97 #  pragma warning (default :4142)
98 # else
99     /*
100      * MSVC++ does not include ssize_t, just size_t.
101      * So we need to synthesize that as well.
102      */
103 #  pragma warning (disable :4142)
104 #  ifndef _SSIZE_T_DEFINED
105 #   define _SSIZE_T_DEFINED
106     typedef SSIZE_T ssize_t;
107 #  endif
108 #  pragma warning (default :4142)
109 # endif
110 # define PRIi32 "d"
111 # define PRId32 "d"
112 # define PRIu32 "u"
113 # define PRIi64 "I64i"
114 # define PRId64 "I64i"
115 # define PRIu64 "I64u"
116 #else
117 # include "bson-stdint.h"
118 # include <inttypes.h>
119 #endif
120 
121 #if defined(__MINGW32__) && ! defined(INIT_ONCE_STATIC_INIT)
122 # define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT
123 typedef RTL_RUN_ONCE INIT_ONCE;
124 #endif
125 
126 #ifdef BSON_HAVE_STDBOOL_H
127 # include <stdbool.h>
128 #elif !defined(__bool_true_false_are_defined)
129 # ifndef __cplusplus
130    typedef signed char bool;
131 #  define false 0
132 #  define true 1
133 # endif
134 # define __bool_true_false_are_defined 1
135 #endif
136 
137 
138 #if defined(__GNUC__)
139 # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
140 #  define bson_sync_synchronize() __sync_synchronize()
141 # elif defined(__i386__ ) || defined( __i486__ ) || defined( __i586__ ) || \
142           defined( __i686__ ) || defined( __x86_64__ )
143 #  define bson_sync_synchronize() asm volatile("mfence":::"memory")
144 # else
145 #  define bson_sync_synchronize() asm volatile("sync":::"memory")
146 # endif
147 #elif defined(_MSC_VER)
148 # define bson_sync_synchronize() MemoryBarrier()
149 #endif
150 
151 
152 #if !defined(va_copy) && defined(_MSC_VER)
153 # define va_copy(dst,src) ((dst) = (src))
154 #endif
155 
156 
157 #if !defined(va_copy) && defined(__GNUC__) && __GNUC__ < 3
158 # define va_copy(dst,src) __va_copy(dst, src)
159 #endif
160 
161 
162 BSON_END_DECLS
163 
164 
165 #endif /* BSON_COMPAT_H */
166