1/*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#ifndef SHARE_VM_UTILITIES_GLOBALDEFINITIONS_VISCPP_HPP
26#define SHARE_VM_UTILITIES_GLOBALDEFINITIONS_VISCPP_HPP
27
28#include "prims/jni.h"
29
30// This file holds compiler-dependent includes,
31// globally used constants & types, class (forward)
32// declarations and a few frequently used utility functions.
33
34# include <ctype.h>
35# include <string.h>
36# include <stdarg.h>
37# include <stdlib.h>
38# include <stddef.h>// for offsetof
39# include <io.h>    // for stream.cpp
40# include <float.h> // for _isnan
41# include <stdio.h> // for va_list
42# include <time.h>
43# include <fcntl.h>
44# include <limits.h>
45// Need this on windows to get the math constants (e.g., M_PI).
46#define _USE_MATH_DEFINES
47# include <math.h>
48
49// 4810578: varargs unsafe on 32-bit integer/64-bit pointer architectures
50// When __cplusplus is defined, NULL is defined as 0 (32-bit constant) in
51// system header files.  On 32-bit architectures, there is no problem.
52// On 64-bit architectures, defining NULL as a 32-bit constant can cause
53// problems with varargs functions: C++ integral promotion rules say for
54// varargs, we pass the argument 0 as an int.  So, if NULL was passed to a
55// varargs function it will remain 32-bits.  Depending on the calling
56// convention of the machine, if the argument is passed on the stack then
57// only 32-bits of the "NULL" pointer may be initialized to zero.  The
58// other 32-bits will be garbage.  If the varargs function is expecting a
59// pointer when it extracts the argument, then we may have a problem.
60//
61// Solution: For 64-bit architectures, redefine NULL as 64-bit constant 0.
62#ifdef _LP64
63#undef NULL
64// 64-bit Windows uses a P64 data model (not LP64, although we define _LP64)
65// Since longs are 32-bit we cannot use 0L here.  Use the Visual C++ specific
66// 64-bit integer-suffix (LL) instead.
67#define NULL 0LL
68#else
69#ifndef NULL
70#define NULL 0
71#endif
72#endif
73
74// NULL vs NULL_WORD:
75// On Linux NULL is defined as a special type '__null'. Assigning __null to
76// integer variable will cause gcc warning. Use NULL_WORD in places where a
77// pointer is stored as integer value.
78#define NULL_WORD NULL
79
80// Compiler-specific primitive types
81typedef unsigned __int8  uint8_t;
82typedef unsigned __int16 uint16_t;
83typedef unsigned __int32 uint32_t;
84typedef unsigned __int64 uint64_t;
85
86#ifdef _WIN64
87typedef unsigned __int64 uintptr_t;
88#else
89typedef unsigned int uintptr_t;
90#endif
91typedef signed   __int8  int8_t;
92typedef signed   __int16 int16_t;
93typedef signed   __int32 int32_t;
94typedef signed   __int64 int64_t;
95#ifdef _WIN64
96typedef signed   __int64 intptr_t;
97typedef signed   __int64 ssize_t;
98#else
99typedef signed   int intptr_t;
100typedef signed   int ssize_t;
101#endif
102
103#ifndef UINTPTR_MAX
104#ifdef _WIN64
105#define UINTPTR_MAX _UI64_MAX
106#else
107#define UINTPTR_MAX _UI32_MAX
108#endif
109#endif
110
111// Additional Java basic types
112
113typedef unsigned char    jubyte;
114typedef unsigned short   jushort;
115typedef unsigned int     juint;
116typedef unsigned __int64 julong;
117
118// Non-standard stdlib-like stuff:
119inline int strcasecmp(const char *s1, const char *s2) { return _stricmp(s1,s2); }
120inline int strncasecmp(const char *s1, const char *s2, size_t n) {
121  return _strnicmp(s1,s2,n);
122}
123
124// Checking for nanness
125
126inline int g_isnan(jfloat  f)                    { return _isnan(f); }
127inline int g_isnan(jdouble f)                    { return _isnan(f); }
128
129// Checking for finiteness
130
131inline int g_isfinite(jfloat  f)                 { return _finite(f); }
132inline int g_isfinite(jdouble f)                 { return _finite(f); }
133
134// Miscellaneous
135
136// Visual Studio 2005 deprecates POSIX names - use ISO C++ names instead
137#if _MSC_VER >= 1400
138#define open _open
139#define close _close
140#define read  _read
141#define write _write
142#define lseek _lseek
143#define unlink _unlink
144#define strdup _strdup
145#endif
146
147#if _MSC_VER < 1800
148// Visual Studio 2013 introduced strtoull(); before, one has to use _strtoui64() instead.
149#define strtoull _strtoui64
150// Visual Studio prior to 2013 had no va_copy, but could safely copy va_list by assignement
151#define va_copy(dest, src) dest = src
152// Fixes some wrong warnings about 'this' : used in base member initializer list
153#pragma warning( disable : 4355 )
154#endif
155
156
157#pragma warning( disable : 4100 ) // unreferenced formal parameter
158#pragma warning( disable : 4127 ) // conditional expression is constant
159#pragma warning( disable : 4514 ) // unreferenced inline function has been removed
160#pragma warning( disable : 4244 ) // possible loss of data
161#pragma warning( disable : 4512 ) // assignment operator could not be generated
162#pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union (needed in windows.h)
163#pragma warning( disable : 4511 ) // copy constructor could not be generated
164#pragma warning( disable : 4291 ) // no matching operator delete found; memory will not be freed if initialization thows an exception
165#ifdef CHECK_UNHANDLED_OOPS
166#pragma warning( disable : 4521 ) // class has multiple copy ctors of a single type
167#pragma warning( disable : 4522 ) // class has multiple assignment operators of a single type
168#endif // CHECK_UNHANDLED_OOPS
169#if _MSC_VER >= 1400
170#pragma warning( disable : 4996 ) // unsafe string functions. Same as define _CRT_SECURE_NO_WARNINGS/_CRT_SECURE_NO_DEPRICATE
171#endif
172
173inline int vsnprintf(char* buf, size_t count, const char* fmt, va_list argptr) {
174  // If number of characters written == count, Windows doesn't write a
175  // terminating NULL, so we do it ourselves.
176  int ret = _vsnprintf(buf, count, fmt, argptr);
177  if (count > 0) buf[count-1] = '\0';
178  return ret;
179}
180
181// Portability macros
182#define PRAGMA_INTERFACE
183#define PRAGMA_IMPLEMENTATION
184#define PRAGMA_IMPLEMENTATION_(arg)
185#define VALUE_OBJ_CLASS_SPEC    : public _ValueObj
186
187// Formatting.
188#define FORMAT64_MODIFIER "I64"
189
190// Visual Studio doesn't provide inttypes.h so provide appropriate definitions here.
191// The 32 bits ones might need I32 but seem to work ok without it.
192#define PRId32       "d"
193#define PRIu32       "u"
194#define PRIx32       "x"
195
196#define PRId64       "I64d"
197#define PRIu64       "I64u"
198#define PRIx64       "I64x"
199
200#ifdef _LP64
201#define PRIdPTR       "I64d"
202#define PRIuPTR       "I64u"
203#define PRIxPTR       "I64x"
204#else
205#define PRIdPTR       "d"
206#define PRIuPTR       "u"
207#define PRIxPTR       "x"
208#endif
209
210#define offset_of(klass,field) offsetof(klass,field)
211
212#ifndef USE_LIBRARY_BASED_TLS_ONLY
213#define THREAD_LOCAL_DECL __declspec( thread )
214#endif
215
216// Inlining support
217// MSVC has '__declspec(noinline)' but according to the official documentation
218// it only applies to member functions. There are reports though which pretend
219// that it also works for freestanding functions.
220#define NOINLINE     __declspec(noinline)
221#define ALWAYSINLINE __forceinline
222
223#endif // SHARE_VM_UTILITIES_GLOBALDEFINITIONS_VISCPP_HPP
224