Loading...
Searching...
No Matches
cmocka_private.h
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CMOCKA_PRIVATE_H_
#define CMOCKA_PRIVATE_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef CMOCKA_NO_STANDARD_INCLUDES
#include <stdint.h>
#endif
#ifdef _WIN32
#include <windows.h>
# ifdef _MSC_VER
# include <stdio.h> /* _snprintf */
# include <string.h> /* strtok_s */
# undef inline
# define inline __inline
# ifndef va_copy
# define va_copy(dest, src) (dest = src)
# endif
# define strcasecmp _stricmp
# define strncasecmp _strnicmp
# define strtok_r strtok_s
# define strdup _strdup
# if defined(HAVE__SNPRINTF_S)
# undef snprintf
# define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
# else /* HAVE__SNPRINTF_S */
# if defined(HAVE__SNPRINTF)
# undef snprintf
# define snprintf _snprintf
# else /* HAVE__SNPRINTF */
# if !defined(HAVE_SNPRINTF)
# error "no snprintf compatible function found"
# endif /* HAVE_SNPRINTF */
# endif /* HAVE__SNPRINTF */
# endif /* HAVE__SNPRINTF_S */
# if defined(HAVE__VSNPRINTF_S)
# undef vsnprintf
# define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
# else /* HAVE__VSNPRINTF_S */
# if defined(HAVE__VSNPRINTF)
# undef vsnprintf
# define vsnprintf _vsnprintf
# else
# if !defined(HAVE_VSNPRINTF)
# error "No vsnprintf compatible function found"
# endif /* HAVE_VSNPRINTF */
# endif /* HAVE__VSNPRINTF */
# endif /* HAVE__VSNPRINTF_S */
# endif /* _MSC_VER */
#ifndef PRIdS
# define PRIdS "Id"
#endif
#ifndef PRIu64
# define PRIu64 "I64u"
#endif
#ifndef PRIuMAX
# define PRIuMAX PRIu64
#endif
#ifndef PRIxMAX
#define PRIxMAX "I64x"
#endif
#ifndef PRIXMAX
#define PRIXMAX "I64X"
#endif
#ifndef PATH_MAX
#ifdef MAX_PATH
#define PATH_MAX MAX_PATH
#else
#define PATH_MAX 256
#endif /* MAX_PATH */
#endif /* PATH_MAX */
#else /* _WIN32 */
#ifndef __PRI64_PREFIX
# if __WORDSIZE == 64
# define __PRI64_PREFIX "l"
# else
# define __PRI64_PREFIX "ll"
# endif
#endif
#ifndef PRIdS
# define PRIdS "zd"
#endif
#ifndef PRIu64
# define PRIu64 __PRI64_PREFIX "u"
#endif
#ifndef PRIuMAX
# define PRIuMAX __PRI64_PREFIX "u"
#endif
#ifndef PRIxMAX
#define PRIxMAX __PRI64_PREFIX "x"
#endif
#ifndef PRIXMAX
#define PRIXMAX __PRI64_PREFIX "X"
#endif
#endif /* _WIN32 */
#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
/* Cmocka does not rely on math.h, so NAN and INFINITY are handled below */
#ifndef INFINITY
# if defined(__GNUC__) || defined(__clang__)
# define INFINITY __builtin_inff()
# else
# define INFINITY ((float)(1e+300 * 1e+300)) /* Goes to infinity when squared */
# endif
#endif
#ifndef NAN
# if defined(__GNUC__) || defined(__clang__)
# define NAN __builtin_nanf("")
# else
# define NAN ((float)(INFINITY * 0.0f)) /* MSVC technique to create float NAN */
# endif
#endif
#if !defined(isnan)
static inline int cmocka_isnan(double x) {
union { double d; uint64_t i; } u;
u.d = x;
/* NaN: exponent all 1s, mantissa non-zero */
return ((u.i & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL) &&
((u.i & 0x000FFFFFFFFFFFFFULL) != 0);
}
# define isnan(x) cmocka_isnan(x)
#endif
#if !defined(isinf)
static inline int cmocka_isinf(double x) {
union { double d; uint64_t i; } u;
u.d = x;
/* Infinity: exponent all 1s, mantissa all 0s */
return ((u.i & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL) &&
((u.i & 0x000FFFFFFFFFFFFFULL) == 0);
}
# define isinf(x) cmocka_isinf(x)
#endif
#endif /* CMOCKA_PRIVATE_H_ */