imcplugin: Set NACL_FD environment variable
[nativeclient.git] / include / portability.h
blob901489e72c96e18929a94651ec99fb15f55fbfaf
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _PORTABILITY_H
33 #define _PORTABILITY_H
35 #include <stdlib.h>
37 #if NACL_WINDOWS
38 # include <malloc.h>
39 # include "win/port_win.h"
40 #else
41 # include <sys/types.h>
42 # include <stdint.h>
43 #endif /*NACL_WINDOWS*/
45 #if NACL_OSX
46 #include "osx/port_osx.h"
47 #endif
49 /* MSVC supports "inline" only in C++ */
50 #if NACL_WINDOWS
51 # define INLINE __inline
52 #else
53 # define INLINE inline
54 #endif
56 #if NACL_WINDOWS
57 # define DLLEXPORT __declspec(dllexport)
58 #elif NACL_OSX
59 # define DLLEXPORT
60 #elif NACL_LINUX
61 # define DLLEXPORT __attribute__ ((visibility("default")))
62 #else
63 # error "what platform?"
64 #endif
67 #if NACL_WINDOWS
68 # define UNREFERENCED_PARAMETER(P) (P)
69 #else
70 # define UNREFERENCED_PARAMETER(P) do { ; } while (0)
71 #endif
73 #if NACL_WINDOWS
74 # define NORETURN __declspec(noreturn)
75 #else
76 # define NORETURN __attribute__((noreturn)) /* assumes gcc */
77 # define _cdecl /* empty */
78 #endif
80 #if NACL_WINDOWS
81 # define THREAD __declspec(thread)
82 #else
83 # define THREAD __thread
84 # define WINAPI
85 #endif
88 Per C99 7.8.14, define __STDC_CONSTANT_MACROS before including <stdint.h>
89 to get the INTn_C and UINTn_C macros for integer constants. It's difficult
90 to guarantee any specific ordering of header includes, so it's difficult to
91 guarantee that the INTn_C macros can be defined by including <stdint.h> at
92 any specific point. Provide GG_INTn_C macros instead.
95 #define GG_INT8_C(x) (x)
96 #define GG_INT16_C(x) (x)
97 #define GG_INT32_C(x) (x)
98 #define GG_INT64_C(x) GG_LONGLONG(x)
100 #define GG_UINT8_C(x) (x ## U)
101 #define GG_UINT16_C(x) (x ## U)
102 #define GG_UINT32_C(x) (x ## U)
103 #define GG_UINT64_C(x) GG_ULONGLONG(x)
105 #if NACL_WINDOWS
106 #define SNPRINTF _snprintf
107 #define STRNCPY(dest, size, src, len) strncpy_s(dest, size, src, len)
108 #define STRDUP _strdup
109 #else
110 #define SNPRINTF snprintf
111 #define STRNCPY(dest, size, src, len) strncpy(dest, src, len)
112 #define STRDUP strdup
113 #endif
116 * printf macros for size_t, in the style of inttypes.h. this is
117 * needed since the windows compiler does not understand %zd etc.
119 #if NACL_WINDOWS
120 # define __PRIS_PREFIX
121 #else
122 # define __PRIS_PREFIX "z"
123 #endif
125 #define PRIdS __PRIS_PREFIX "d"
126 #define PRIxS __PRIS_PREFIX "x"
127 #define PRIuS __PRIS_PREFIX "u"
128 #define PRIXS __PRIS_PREFIX "X"
129 #define PRIoS __PRIS_PREFIX "o"
132 * printf macros for intptr_t and uintptr_t, int{8,16,32,64}
134 #if !NACL_WINDOWS
135 # define __STDC_FORMAT_MACROS /* C++ */
136 # include <inttypes.h>
137 #else
138 # define __PRIPTR_PREFIX "l"
139 # define PRIdPTR __PRIPTR_PREFIX "d"
140 # define PRIiPTR __PRIPTR_PREFIX "i"
141 # define PRIoPTR __PRIPTR_PREFIX "o"
142 # define PRIuPTR __PRIPTR_PREFIX "u"
143 # define PRIxPTR __PRIPTR_PREFIX "x"
144 # define PRIXPTR __PRIPTR_PREFIX "X"
146 # define PRId8 "d"
147 # define PRIi8 "i"
148 # define PRIo8 "o"
149 # define PRIu8 "u"
150 # define PRIx8 "x"
151 # define PRIX8 "X"
153 # define PRId16 "d"
154 # define PRIi16 "i"
155 # define PRIo16 "o"
156 # define PRIu16 "u"
157 # define PRIx16 "x"
158 # define PRIX16 "X"
160 # define __PRI32_PREFIX "I32"
162 # define PRId32 __PRI32_PREFIX "d"
163 # define PRIi32 __PRI32_PREFIX "i"
164 # define PRIo32 __PRI32_PREFIX "o"
165 # define PRIu32 __PRI32_PREFIX "u"
166 # define PRIx32 __PRI32_PREFIX "x"
167 # define PRIX32 __PRI32_PREFIX "X"
169 # define __PRI64_PREFIX "I32"
171 # define PRId64 __PRI64_PREFIX "d"
172 # define PRIi64 __PRI64_PREFIX "i"
173 # define PRIo64 __PRI64_PREFIX "o"
174 # define PRIu64 __PRI64_PREFIX "u"
175 # define PRIx64 __PRI64_PREFIX "x"
176 # define PRIX64 __PRI64_PREFIX "X"
178 #endif
180 #endif /* _PORTABILITY_H */