1 /*
   2  * Copyright (c) 1997, 2019, 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_UTILITIES_GLOBALDEFINITIONS_VISCPP_HPP
  26 #define SHARE_UTILITIES_GLOBALDEFINITIONS_VISCPP_HPP
  27 
  28 #include "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 <stdint.h>
  39 # include <stddef.h>// for offsetof
  40 # include <io.h>    // for stream.cpp
  41 # include <float.h> // for _isnan
  42 # include <stdio.h> // for va_list
  43 # include <time.h>
  44 # include <fcntl.h>
  45 # include <limits.h>
  46 # include <inttypes.h>
  47 // Need this on windows to get the math constants (e.g., M_PI).
  48 #define _USE_MATH_DEFINES
  49 # include <math.h>
  50 
  51 // 4810578: varargs unsafe on 32-bit integer/64-bit pointer architectures
  52 // When __cplusplus is defined, NULL is defined as 0 (32-bit constant) in
  53 // system header files.  On 32-bit architectures, there is no problem.
  54 // On 64-bit architectures, defining NULL as a 32-bit constant can cause
  55 // problems with varargs functions: C++ integral promotion rules say for
  56 // varargs, we pass the argument 0 as an int.  So, if NULL was passed to a
  57 // varargs function it will remain 32-bits.  Depending on the calling
  58 // convention of the machine, if the argument is passed on the stack then
  59 // only 32-bits of the "NULL" pointer may be initialized to zero.  The
  60 // other 32-bits will be garbage.  If the varargs function is expecting a
  61 // pointer when it extracts the argument, then we may have a problem.
  62 //
  63 // Solution: For 64-bit architectures, redefine NULL as 64-bit constant 0.
  64 #ifdef _LP64
  65 #undef NULL
  66 // 64-bit Windows uses a P64 data model (not LP64, although we define _LP64)
  67 // Since longs are 32-bit we cannot use 0L here.  Use the Visual C++ specific
  68 // 64-bit integer-suffix (LL) instead.
  69 #define NULL 0LL
  70 #else
  71 #ifndef NULL
  72 #define NULL 0
  73 #endif
  74 #endif
  75 
  76 // NULL vs NULL_WORD:
  77 // On Linux NULL is defined as a special type '__null'. Assigning __null to
  78 // integer variable will cause gcc warning. Use NULL_WORD in places where a
  79 // pointer is stored as integer value.
  80 #define NULL_WORD NULL
  81 
  82 #ifdef _WIN64
  83 typedef int64_t ssize_t;
  84 #else
  85 typedef int32_t ssize_t;
  86 #endif
  87 
  88 // Additional Java basic types
  89 
  90 typedef uint8_t  jubyte;
  91 typedef uint16_t jushort;
  92 typedef uint32_t juint;
  93 typedef uint64_t julong;
  94 
  95 // Non-standard stdlib-like stuff:
  96 inline int strcasecmp(const char *s1, const char *s2) { return _stricmp(s1,s2); }
  97 inline int strncasecmp(const char *s1, const char *s2, size_t n) {
  98   return _strnicmp(s1,s2,n);
  99 }
 100 
 101 // Checking for nanness
 102 
 103 inline int g_isnan(jfloat  f)                    { return _isnan(f); }
 104 inline int g_isnan(jdouble f)                    { return _isnan(f); }
 105 
 106 // Checking for finiteness
 107 
 108 inline int g_isfinite(jfloat  f)                 { return _finite(f); }
 109 inline int g_isfinite(jdouble f)                 { return _finite(f); }
 110 
 111 // Miscellaneous
 112 
 113 // Visual Studio 2005 deprecates POSIX names - use ISO C++ names instead
 114 #define open _open
 115 #define close _close
 116 #define read  _read
 117 #define write _write
 118 #define lseek _lseek
 119 #define unlink _unlink
 120 #define strdup _strdup
 121 
 122 // Formatting.
 123 #define FORMAT64_MODIFIER "I64"
 124 
 125 #define offset_of(klass,field) offsetof(klass,field)
 126 
 127 #define THREAD_LOCAL __declspec(thread)
 128 
 129 // Inlining support
 130 // MSVC has '__declspec(noinline)' but according to the official documentation
 131 // it only applies to member functions. There are reports though which pretend
 132 // that it also works for freestanding functions.
 133 #define NOINLINE     __declspec(noinline)
 134 #define ALWAYSINLINE __forceinline
 135 
 136 // Alignment
 137 #define ATTRIBUTE_ALIGNED(x) __declspec(align(x))
 138 
 139 #endif // SHARE_UTILITIES_GLOBALDEFINITIONS_VISCPP_HPP