$darkmode
cmocka 2.0.2
Unit testing library with mock support
cmocka_private.h
1 /*
2  * Copyright 2008 Google 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 #ifndef CMOCKA_PRIVATE_H_
18 #define CMOCKA_PRIVATE_H_
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #ifndef CMOCKA_NO_STANDARD_INCLUDES
25 #include <stdint.h>
26 #endif
27 
28 #ifdef _WIN32
29 #include <windows.h>
30 
31 # ifdef _MSC_VER
32 # include <stdio.h> /* _snprintf */
33 # include <string.h> /* strtok_s */
34 
35 # undef inline
36 # define inline __inline
37 
38 # ifndef va_copy
39 # define va_copy(dest, src) (dest = src)
40 # endif
41 
42 # define strcasecmp _stricmp
43 # define strncasecmp _strnicmp
44 # define strtok_r strtok_s
45 # define strdup _strdup
46 
47 # if defined(HAVE__SNPRINTF_S)
48 # undef snprintf
49 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
50 # else /* HAVE__SNPRINTF_S */
51 # if defined(HAVE__SNPRINTF)
52 # undef snprintf
53 # define snprintf _snprintf
54 # else /* HAVE__SNPRINTF */
55 # if !defined(HAVE_SNPRINTF)
56 # error "no snprintf compatible function found"
57 # endif /* HAVE_SNPRINTF */
58 # endif /* HAVE__SNPRINTF */
59 # endif /* HAVE__SNPRINTF_S */
60 
61 # if defined(HAVE__VSNPRINTF_S)
62 # undef vsnprintf
63 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
64 # else /* HAVE__VSNPRINTF_S */
65 # if defined(HAVE__VSNPRINTF)
66 # undef vsnprintf
67 # define vsnprintf _vsnprintf
68 # else
69 # if !defined(HAVE_VSNPRINTF)
70 # error "No vsnprintf compatible function found"
71 # endif /* HAVE_VSNPRINTF */
72 # endif /* HAVE__VSNPRINTF */
73 # endif /* HAVE__VSNPRINTF_S */
74 # endif /* _MSC_VER */
75 
76 #ifndef PRIdS
77 # define PRIdS "Id"
78 #endif
79 
80 #ifndef PRIu64
81 # define PRIu64 "I64u"
82 #endif
83 
84 #ifndef PRIuMAX
85 # define PRIuMAX PRIu64
86 #endif
87 
88 #ifndef PRIxMAX
89 #define PRIxMAX "I64x"
90 #endif
91 
92 #ifndef PRIXMAX
93 #define PRIXMAX "I64X"
94 #endif
95 
96 #ifndef PATH_MAX
97 #ifdef MAX_PATH
98 #define PATH_MAX MAX_PATH
99 #else
100 #define PATH_MAX 256
101 #endif /* MAX_PATH */
102 #endif /* PATH_MAX */
103 
104 #else /* _WIN32 */
105 
106 #ifndef __PRI64_PREFIX
107 # if __WORDSIZE == 64
108 # define __PRI64_PREFIX "l"
109 # else
110 # define __PRI64_PREFIX "ll"
111 # endif
112 #endif
113 
114 #ifndef PRIdS
115 # define PRIdS "zd"
116 #endif
117 
118 #ifndef PRIu64
119 # define PRIu64 __PRI64_PREFIX "u"
120 #endif
121 
122 #ifndef PRIuMAX
123 # define PRIuMAX __PRI64_PREFIX "u"
124 #endif
125 
126 #ifndef PRIxMAX
127 #define PRIxMAX __PRI64_PREFIX "x"
128 #endif
129 
130 #ifndef PRIXMAX
131 #define PRIXMAX __PRI64_PREFIX "X"
132 #endif
133 
134 #endif /* _WIN32 */
135 
137 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
138 
140 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
141 
143 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
144 
146 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
147 
149 #define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
150 
163 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
164 
168 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
169 
170 /* Cmocka does not rely on math.h, so NAN and INFINITY are handled below */
171 
172 #ifndef INFINITY
173 # if defined(__GNUC__) || defined(__clang__)
174 # define INFINITY __builtin_inff()
175 # else
176 # define INFINITY ((float)(1e+300 * 1e+300)) /* Goes to infinity when squared */
177 # endif
178 #endif
179 
180 #ifndef NAN
181 # if defined(__GNUC__) || defined(__clang__)
182 # define NAN __builtin_nanf("")
183 # else
184 # define NAN ((float)(INFINITY * 0.0f)) /* MSVC technique to create float NAN */
185 # endif
186 #endif
187 
188 #if !defined(isnan)
189 static inline int cmocka_isnan(double x) {
190  union { double d; uint64_t i; } u;
191  u.d = x;
192  /* NaN: exponent all 1s, mantissa non-zero */
193  return ((u.i & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL) &&
194  ((u.i & 0x000FFFFFFFFFFFFFULL) != 0);
195 }
196 # define isnan(x) cmocka_isnan(x)
197 #endif
198 
199 #if !defined(isinf)
200 static inline int cmocka_isinf(double x) {
201  union { double d; uint64_t i; } u;
202  u.d = x;
203  /* Infinity: exponent all 1s, mantissa all 0s */
204  return ((u.i & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL) &&
205  ((u.i & 0x000FFFFFFFFFFFFFULL) == 0);
206 }
207 # define isinf(x) cmocka_isinf(x)
208 #endif
209 
210 #endif /* CMOCKA_PRIVATE_H_ */