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#include <stdint.h>
21
22#ifdef _WIN32
23#include <windows.h>
24
25#ifdef _MSC_VER
26#include <stdio.h> /* _snprintf */
27
28#undef inline
29#define inline __inline
30
31#define strcasecmp _stricmp
32#define strncasecmp _strnicmp
33
34#if defined(HAVE__SNPRINTF_S)
35#undef snprintf
36#define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
37#else /* HAVE__SNPRINTF_S */
38#if defined(HAVE__SNPRINTF)
39#undef snprintf
40#define snprintf _snprintf
41#else /* HAVE__SNPRINTF */
42#if !defined(HAVE_SNPRINTF)
43#pragma message "no snprintf compatible function found"
44#endif /* HAVE_SNPRINTF */
45#endif /* HAVE__SNPRINTF */
46#endif /* HAVE__SNPRINTF_S */
47
48#if defined(HAVE__VSNPRINTF_S)
49#undef vsnprintf
50#define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
51#else /* HAVE__VSNPRINTF_S */
52#if defined(HAVE__VSNPRINTF)
53#undef vsnprintf
54#define vsnprintf _vsnprintf
55#else
56#if !defined(HAVE_VSNPRINTF)
57#pragma message "No vsnprintf compatible function found"
58#endif /* HAVE_VSNPRINTF */
59#endif /* HAVE__VSNPRINTF */
60#endif /* HAVE__VSNPRINTF_S */
61#endif /* _MSC_VER */
62
63/*
64 * Backwards compatibility with headers shipped with Visual Studio 2005 and
65 * earlier.
66 */
67WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID);
68
69#ifndef PRIdS
70#define PRIdS "Id"
71#endif
72
73#ifndef PRIu64
74#define PRIu64 "I64u"
75#endif
76
77#ifndef PRIuMAX
78#define PRIuMAX PRIu64
79#endif
80
81#ifndef PRIxMAX
82#define PRIxMAX "I64x"
83#endif
84
85#ifndef PRIXMAX
86#define PRIXMAX "I64X"
87#endif
88
89#else /* _WIN32 */
90
91#ifndef __PRI64_PREFIX
92#if __WORDSIZE == 64
93#define __PRI64_PREFIX "l"
94#else
95#define __PRI64_PREFIX "ll"
96#endif
97#endif
98
99#ifndef PRIdS
100#define PRIdS "zd"
101#endif
102
103#ifndef PRIu64
104#define PRIu64 __PRI64_PREFIX "u"
105#endif
106
107#ifndef PRIuMAX
108#define PRIuMAX __PRI64_PREFIX "u"
109#endif
110
111#ifndef PRIxMAX
112#define PRIxMAX __PRI64_PREFIX "x"
113#endif
114
115#ifndef PRIXMAX
116#define PRIXMAX __PRI64_PREFIX "X"
117#endif
118
119#endif /* _WIN32 */
120
121/** Free memory space */
122#define SAFE_FREE(x) \
123 do { \
124 if ((x) != NULL) { \
125 free(x); \
126 x = NULL; \
127 } \
128 } while (0)
129
130/** Zero a structure */
131#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
132
133/** Zero a structure given a pointer to the structure */
134#define ZERO_STRUCTP(x) \
135 do { \
136 if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); \
137 } while (0)
138
139/** Get the size of an array */
140#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
141
142/** Overwrite the complete string with 'X' */
143#define BURN_STRING(x) \
144 do { \
145 if ((x) != NULL) memset((x), 'X', strlen((x))); \
146 } while (0)
147
148/**
149 * This is a hack to fix warnings. The idea is to use this everywhere that we
150 * get the "discarding const" warning by the compiler. That doesn't actually
151 * fix the real issue, but marks the place and you can search the code for
152 * discard_const.
153 *
154 * Please use this macro only when there is no other way to fix the warning.
155 * We should use this function in only in a very few places.
156 *
157 * Also, please call this via the discard_const_p() macro interface, as that
158 * makes the return type safe.
159 */
160#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
161
162/**
163 * Type-safe version of discard_const
164 */
165#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
166
167#endif /* CMOCKA_PRIVATE_H_ */
168