c99_compat.h revision 848b8605
1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2007-2013 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#ifndef _C99_COMPAT_H_
29848b8605Smrg#define _C99_COMPAT_H_
30848b8605Smrg
31848b8605Smrg
32848b8605Smrg/*
33848b8605Smrg * MSVC hacks.
34848b8605Smrg */
35848b8605Smrg#if defined(_MSC_VER)
36848b8605Smrg   /*
37848b8605Smrg    * Visual Studio 2012 will complain if we define the `inline` keyword, but
38848b8605Smrg    * actually it only supports the keyword on C++.
39848b8605Smrg    *
40848b8605Smrg    * To avoid this the _ALLOW_KEYWORD_MACROS must be set.
41848b8605Smrg    */
42848b8605Smrg#  if (_MSC_VER >= 1700) && !defined(_ALLOW_KEYWORD_MACROS)
43848b8605Smrg#    define _ALLOW_KEYWORD_MACROS
44848b8605Smrg#  endif
45848b8605Smrg
46848b8605Smrg   /*
47848b8605Smrg    * XXX: MSVC has a `__restrict` keyword, but it also has a
48848b8605Smrg    * `__declspec(restrict)` modifier, so it is impossible to define a
49848b8605Smrg    * `restrict` macro without interfering with the latter.  Furthermore the
50848b8605Smrg    * MSVC standard library uses __declspec(restrict) under the _CRTRESTRICT
51848b8605Smrg    * macro.  For now resolve this issue by redefining _CRTRESTRICT, but going
52848b8605Smrg    * forward we should probably should stop using restrict, especially
53848b8605Smrg    * considering that our code does not obbey strict aliasing rules any way.
54848b8605Smrg    */
55848b8605Smrg#  include <crtdefs.h>
56848b8605Smrg#  undef _CRTRESTRICT
57848b8605Smrg#  define _CRTRESTRICT
58848b8605Smrg#endif
59848b8605Smrg
60848b8605Smrg
61848b8605Smrg/*
62848b8605Smrg * C99 inline keyword
63848b8605Smrg */
64848b8605Smrg#ifndef inline
65848b8605Smrg#  ifdef __cplusplus
66848b8605Smrg     /* C++ supports inline keyword */
67848b8605Smrg#  elif defined(__GNUC__)
68848b8605Smrg#    define inline __inline__
69848b8605Smrg#  elif defined(_MSC_VER)
70848b8605Smrg#    define inline __inline
71848b8605Smrg#  elif defined(__ICL)
72848b8605Smrg#    define inline __inline
73848b8605Smrg#  elif defined(__INTEL_COMPILER)
74848b8605Smrg     /* Intel compiler supports inline keyword */
75848b8605Smrg#  elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
76848b8605Smrg#    define inline __inline
77848b8605Smrg#  elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
78848b8605Smrg     /* C99 supports inline keyword */
79848b8605Smrg#  elif (__STDC_VERSION__ >= 199901L)
80848b8605Smrg     /* C99 supports inline keyword */
81848b8605Smrg#  else
82848b8605Smrg#    define inline
83848b8605Smrg#  endif
84848b8605Smrg#endif
85848b8605Smrg
86848b8605Smrg
87848b8605Smrg/*
88848b8605Smrg * C99 restrict keyword
89848b8605Smrg *
90848b8605Smrg * See also:
91848b8605Smrg * - http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html
92848b8605Smrg */
93848b8605Smrg#ifndef restrict
94848b8605Smrg#  if (__STDC_VERSION__ >= 199901L)
95848b8605Smrg     /* C99 */
96848b8605Smrg#  elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
97848b8605Smrg     /* C99 */
98848b8605Smrg#  elif defined(__GNUC__)
99848b8605Smrg#    define restrict __restrict__
100848b8605Smrg#  elif defined(_MSC_VER)
101848b8605Smrg#    define restrict __restrict
102848b8605Smrg#  else
103848b8605Smrg#    define restrict /* */
104848b8605Smrg#  endif
105848b8605Smrg#endif
106848b8605Smrg
107848b8605Smrg
108848b8605Smrg/*
109848b8605Smrg * C99 __func__ macro
110848b8605Smrg */
111848b8605Smrg#ifndef __func__
112848b8605Smrg#  if (__STDC_VERSION__ >= 199901L)
113848b8605Smrg     /* C99 */
114848b8605Smrg#  elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
115848b8605Smrg     /* C99 */
116848b8605Smrg#  elif defined(__GNUC__)
117848b8605Smrg#    if __GNUC__ >= 2
118848b8605Smrg#      define __func__ __FUNCTION__
119848b8605Smrg#    else
120848b8605Smrg#      define __func__ "<unknown>"
121848b8605Smrg#    endif
122848b8605Smrg#  elif defined(_MSC_VER)
123848b8605Smrg#    if _MSC_VER >= 1300
124848b8605Smrg#      define __func__ __FUNCTION__
125848b8605Smrg#    else
126848b8605Smrg#      define __func__ "<unknown>"
127848b8605Smrg#    endif
128848b8605Smrg#  else
129848b8605Smrg#    define __func__ "<unknown>"
130848b8605Smrg#  endif
131848b8605Smrg#endif
132848b8605Smrg
133848b8605Smrg
134848b8605Smrg/* Simple test case for debugging */
135848b8605Smrg#if 0
136848b8605Smrgstatic inline const char *
137848b8605Smrgtest_c99_compat_h(const void * restrict a,
138848b8605Smrg                  const void * restrict b)
139848b8605Smrg{
140848b8605Smrg   return __func__;
141848b8605Smrg}
142848b8605Smrg#endif
143848b8605Smrg
144848b8605Smrg
145848b8605Smrg#endif /* _C99_COMPAT_H_ */
146