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