1 /*
   2  * Copyright (c) 1998, 2020, 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_GCC_HPP
  26 #define SHARE_UTILITIES_GLOBALDEFINITIONS_GCC_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 <stddef.h>
  38 #include <stdio.h>
  39 #include <stdint.h>
  40 #include <stdlib.h>
  41 #include <wchar.h>
  42 
  43 #include <math.h>
  44 #include <time.h>
  45 #include <fcntl.h>
  46 #include <dlfcn.h>
  47 #include <pthread.h>
  48 
  49 #include <limits.h>
  50 #include <errno.h>
  51 
  52 #if defined(LINUX) || defined(_ALLBSD_SOURCE)
  53 #include <inttypes.h>
  54 #include <signal.h>
  55 #ifndef __OpenBSD__
  56 #include <ucontext.h>
  57 #endif
  58 #ifdef __APPLE__
  59   #include <AvailabilityMacros.h>
  60   #include <mach/mach.h>
  61 #endif
  62 #include <sys/time.h>
  63 #endif // LINUX || _ALLBSD_SOURCE
  64 
  65 // NULL vs NULL_WORD:
  66 // On Linux NULL is defined as a special type '__null'. Assigning __null to
  67 // integer variable will cause gcc warning. Use NULL_WORD in places where a
  68 // pointer is stored as integer value.  On some platforms, sizeof(intptr_t) >
  69 // sizeof(void*), so here we want something which is integer type, but has the
  70 // same size as a pointer.
  71 #ifdef __GNUC__
  72   #ifdef _LP64
  73     #define NULL_WORD  0L
  74   #else
  75     // Cast 0 to intptr_t rather than int32_t since they are not the same type
  76     // on platforms such as Mac OS X.
  77     #define NULL_WORD  ((intptr_t)0)
  78   #endif
  79 #else
  80   #define NULL_WORD  NULL
  81 #endif
  82 
  83 #if !defined(LINUX) && !defined(_ALLBSD_SOURCE)
  84 // Compiler-specific primitive types
  85 typedef unsigned short     uint16_t;
  86 #ifndef _UINT32_T
  87 #define _UINT32_T
  88 typedef unsigned int       uint32_t;
  89 #endif // _UINT32_T
  90 
  91 #if !defined(_SYS_INT_TYPES_H)
  92 #ifndef _UINT64_T
  93 #define _UINT64_T
  94 typedef unsigned long long uint64_t;
  95 #endif // _UINT64_T
  96 // %%%% how to access definition of intptr_t portably in 5.5 onward?
  97 typedef int                     intptr_t;
  98 typedef unsigned int            uintptr_t;
  99 // If this gets an error, figure out a symbol XXX that implies the
 100 // prior definition of intptr_t, and add "&& !defined(XXX)" above.
 101 #endif // _SYS_INT_TYPES_H
 102 
 103 #endif // !LINUX && !_ALLBSD_SOURCE
 104 
 105 // Additional Java basic types
 106 
 107 typedef uint8_t  jubyte;
 108 typedef uint16_t jushort;
 109 typedef uint32_t juint;
 110 typedef uint64_t julong;
 111 
 112 // checking for nanness
 113 #if defined(__APPLE__)
 114 inline int g_isnan(double f) { return isnan(f); }
 115 #elif defined(LINUX) || defined(_ALLBSD_SOURCE)
 116 inline int g_isnan(float  f) { return isnanf(f); }
 117 inline int g_isnan(double f) { return isnan(f); }
 118 #else
 119 #error "missing platform-specific definition here"
 120 #endif
 121 
 122 #define CAN_USE_NAN_DEFINE 1
 123 
 124 
 125 // Checking for finiteness
 126 
 127 inline int g_isfinite(jfloat  f)                 { return isfinite(f); }
 128 inline int g_isfinite(jdouble f)                 { return isfinite(f); }
 129 
 130 
 131 // Wide characters
 132 
 133 inline int wcslen(const jchar* x) { return wcslen((const wchar_t*)x); }
 134 
 135 
 136 // Formatting.
 137 #ifdef _LP64
 138 # ifdef __APPLE__
 139 # define FORMAT64_MODIFIER "ll"
 140 # else
 141 # define FORMAT64_MODIFIER "l"
 142 # endif
 143 #else // !_LP64
 144 #define FORMAT64_MODIFIER "ll"
 145 #endif // _LP64
 146 
 147 // gcc warns about applying offsetof() to non-POD object or calculating
 148 // offset directly when base address is NULL. Use 16 to get around the
 149 // warning. The -Wno-invalid-offsetof option could be used to suppress
 150 // this warning, but we instead just avoid the use of offsetof().
 151 #define offset_of(klass,field) (size_t)((intx)&(((klass*)16)->field) - 16)
 152 
 153 #if defined(_LP64) && defined(__APPLE__)
 154 #define JLONG_FORMAT          "%ld"
 155 #define JLONG_FORMAT_W(width) "%" #width "ld"
 156 #endif // _LP64 && __APPLE__
 157 
 158 #define THREAD_LOCAL __thread
 159 
 160 // Inlining support
 161 #define NOINLINE     __attribute__ ((noinline))
 162 #define ALWAYSINLINE inline __attribute__ ((always_inline))
 163 
 164 // Alignment
 165 //
 166 #define ATTRIBUTE_ALIGNED(x) __attribute__((aligned(x)))
 167 
 168 #endif // SHARE_UTILITIES_GLOBALDEFINITIONS_GCC_HPP