Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / include / portability.h
blobd15c1bcfac6a8543a5f2e8370fb81c6d087bddbf
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.
34 * This file should be at the top of the #include group, followed by
35 * standard system #include files, then by native client specific
36 * includes.
38 * TODO: explain why. (Something to do with windows include
39 * files, to be reconstructed.)
42 #ifndef _PORTABILITY_H
43 #define _PORTABILITY_H
45 #include <stdlib.h>
47 #if NACL_WINDOWS
48 # include <malloc.h>
49 # include <process.h>
50 # include "win/port_win.h"
51 #else
52 # include <sys/types.h>
53 # include <stdint.h>
54 # include <unistd.h>
55 # include <sys/time.h>
56 #endif /*NACL_WINDOWS*/
58 #if NACL_OSX
59 #include "osx/port_osx.h"
60 #endif
62 /* MSVC supports "inline" only in C++ */
63 #if NACL_WINDOWS
64 # define INLINE __inline
65 #else
66 # if __GNUC_MINOR__ >= 2
67 # define INLINE __inline__ __attribute__((gnu_inline))
68 # else
69 # define INLINE __inline__
70 # endif
71 #endif
73 #if NACL_WINDOWS
74 # define DLLEXPORT __declspec(dllexport)
75 #elif NACL_OSX
76 # define DLLEXPORT
77 #elif NACL_LINUX
78 # define DLLEXPORT __attribute__ ((visibility("default")))
79 #else
80 # error "what platform?"
81 #endif
84 #if NACL_WINDOWS
85 # define UNREFERENCED_PARAMETER(P) (P)
86 #else
87 # define UNREFERENCED_PARAMETER(P) do { ; } while (0)
88 #endif
90 #if NACL_WINDOWS
91 # define NORETURN __declspec(noreturn)
92 #else
93 # define NORETURN __attribute__((noreturn)) /* assumes gcc */
94 # define _cdecl /* empty */
95 #endif
97 #if NACL_WINDOWS
98 # define THREAD __declspec(thread)
99 #else
100 # define THREAD __thread
101 # define WINAPI
102 #endif
105 Per C99 7.8.14, define __STDC_CONSTANT_MACROS before including <stdint.h>
106 to get the INTn_C and UINTn_C macros for integer constants. It's difficult
107 to guarantee any specific ordering of header includes, so it's difficult to
108 guarantee that the INTn_C macros can be defined by including <stdint.h> at
109 any specific point. Provide GG_INTn_C macros instead.
112 #define GG_INT8_C(x) (x)
113 #define GG_INT16_C(x) (x)
114 #define GG_INT32_C(x) (x)
115 #define GG_INT64_C(x) GG_LONGLONG(x)
117 #define GG_UINT8_C(x) (x ## U)
118 #define GG_UINT16_C(x) (x ## U)
119 #define GG_UINT32_C(x) (x ## U)
120 #define GG_UINT64_C(x) GG_ULONGLONG(x)
122 #if NACL_WINDOWS
123 # define SNPRINTF _snprintf
124 # define STRNCPY(dest, size, src, len) strncpy_s(dest, size, src, len)
125 # define STRDUP _strdup
126 # define LOCALTIME_R(in_time_t_ptr, out_struct_tm_ptr) \
127 (0 == localtime_s(out_struct_tm_ptr, in_time_t_ptr) \
128 ? out_struct_tm_ptr : (struct tm *) 0) /* NULL requires stdio.h */
129 # define GETPID _getpid
130 # define DUP2 _dup2
131 # define FDOPEN _fdopen
132 # define STRTOULL _strtoui64
133 #else
134 # define SNPRINTF snprintf
135 # define STRNCPY(dest, size, src, len) strncpy(dest, src, len)
136 # define STRDUP strdup
137 # define LOCALTIME_R(in_time_t_ptr, out_struct_tm_ptr) \
138 localtime_r(in_time_t_ptr, out_struct_tm_ptr)
139 # define GETPID getpid
140 # define DUP2 dup2
141 # define FDOPEN fdopen
142 # define STRTOULL strtoull
143 #endif
146 * printf macros for size_t, in the style of inttypes.h. this is
147 * needed since the windows compiler does not understand %zd etc.
149 #if NACL_WINDOWS
150 # define __PRIS_PREFIX
151 #else
152 # if defined(__STRICT_ANSI__)
153 # define __PRIS_PREFIX
154 # else
155 # define __PRIS_PREFIX "z"
156 # endif
157 #endif
159 #define PRIdS __PRIS_PREFIX "d"
160 #define PRIxS __PRIS_PREFIX "x"
161 #define PRIuS __PRIS_PREFIX "u"
162 #define PRIXS __PRIS_PREFIX "X"
163 #define PRIoS __PRIS_PREFIX "o"
166 * printf macros for intptr_t and uintptr_t, int{8,16,32,64}
168 #if !NACL_WINDOWS
169 # define __STDC_FORMAT_MACROS /* C++ */
170 # include <inttypes.h>
171 #else
172 # define __PRIPTR_PREFIX "l"
173 # define PRIdPTR __PRIPTR_PREFIX "d"
174 # define PRIiPTR __PRIPTR_PREFIX "i"
175 # define PRIoPTR __PRIPTR_PREFIX "o"
176 # define PRIuPTR __PRIPTR_PREFIX "u"
177 # define PRIxPTR __PRIPTR_PREFIX "x"
178 # define PRIXPTR __PRIPTR_PREFIX "X"
180 # define PRId8 "d"
181 # define PRIi8 "i"
182 # define PRIo8 "o"
183 # define PRIu8 "u"
184 # define PRIx8 "x"
185 # define PRIX8 "X"
187 # define PRId16 "d"
188 # define PRIi16 "i"
189 # define PRIo16 "o"
190 # define PRIu16 "u"
191 # define PRIx16 "x"
192 # define PRIX16 "X"
194 # define __PRI32_PREFIX "I32"
196 # define PRId32 __PRI32_PREFIX "d"
197 # define PRIi32 __PRI32_PREFIX "i"
198 # define PRIo32 __PRI32_PREFIX "o"
199 # define PRIu32 __PRI32_PREFIX "u"
200 # define PRIx32 __PRI32_PREFIX "x"
201 # define PRIX32 __PRI32_PREFIX "X"
203 # define __PRI64_PREFIX "I32"
205 # define PRId64 __PRI64_PREFIX "d"
206 # define PRIi64 __PRI64_PREFIX "i"
207 # define PRIo64 __PRI64_PREFIX "o"
208 # define PRIu64 __PRI64_PREFIX "u"
209 # define PRIx64 __PRI64_PREFIX "x"
210 # define PRIX64 __PRI64_PREFIX "X"
212 #endif
214 #endif /* _PORTABILITY_H */