7479d79377a2329104b5dbb73d049e436218f302
[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 #include "config.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 #     error "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 #     error "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  */
67 WINBASEAPI 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 #include <signal.h>
120 #endif /* _WIN32 */
121
122 /** Free memory space */
123 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
124
125 /** Zero a structure */
126 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
127
128 /** Zero a structure given a pointer to the structure */
129 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
130
131 /** Get the size of an array */
132 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
133
134 /** Overwrite the complete string with 'X' */
135 #define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
136
137 /**
138  * This is a hack to fix warnings. The idea is to use this everywhere that we
139  * get the "discarding const" warning by the compiler. That doesn't actually
140  * fix the real issue, but marks the place and you can search the code for
141  * discard_const.
142  *
143  * Please use this macro only when there is no other way to fix the warning.
144  * We should use this function in only in a very few places.
145  *
146  * Also, please call this via the discard_const_p() macro interface, as that
147  * makes the return type safe.
148  */
149 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
150
151 /**
152  * Type-safe version of discard_const
153  */
154 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
155
156 #endif /* CMOCKA_PRIVATE_H_ */