1 /*
   2  * Copyright (c) 1999, 2002, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef _ALLOC_H_
  27 #define _ALLOC_H_
  28 
  29 #include "stdhdrs.h"
  30 
  31 // By defining std::bad_alloc in a local header file instead of including
  32 // the Standard C++ <new> header file, we avoid making awt.dll dependent
  33 // on msvcp50.dll. This reduces the size of the JRE by 500kb.
  34 namespace std {
  35     class bad_alloc {};
  36 }
  37 
  38 class awt_toolkit_shutdown {};
  39 
  40 // Disable "C++ Exception Specification ignored" warnings.
  41 // These warnings are generated because VC++ 5.0 allows, but does not enforce,
  42 // exception specifications. This #pragma can be safely removed when VC++
  43 // is updated to enforce exception specifications.
  44 #pragma warning(disable : 4290)
  45 
  46 #ifdef TRY
  47 #error Multiple definitions of TRY
  48 #endif
  49 
  50 #ifdef TRY_NO_VERIFY
  51 #error Multiple definitions of TRY_NO_VERIFY
  52 #endif
  53 
  54 #ifdef CATCH_BAD_ALLOC
  55 #error Multiple definitions of CATCH_BAD_ALLOC
  56 #endif
  57 
  58 #ifdef CATCH_BAD_ALLOC_RET
  59 #error Multiple defintions of CATCH_BAD_ALLOC_RET
  60 #endif
  61 
  62 #ifdef TRY_NO_JNI
  63 #error Multiple definitions of TRY_NO_JNI
  64 #endif
  65 
  66 #ifdef TRY_NO_VERIFY_NO_JNI
  67 #error Multiple definitions of TRY_NO_VERIFY_NO_JNI
  68 #endif
  69 
  70 #ifdef CATCH_BAD_ALLOC_NO_JNI
  71 #error Multiple definitions of CATCH_BAD_ALLOC_NO_JNI
  72 #endif
  73 
  74 #ifdef CATCH_BAD_ALLOC_RET_NO_JNI
  75 #error Multiple defintions of CATCH_BAD_ALLOC_RET_NO_JNI
  76 #endif
  77 
  78 // The unsafe versions of malloc, calloc, and realloc should not be used
  79 #define malloc Do_Not_Use_malloc_Use_safe_Malloc_Instead
  80 #define calloc Do_Not_Use_calloc_Use_safe_Calloc_Instead
  81 #define realloc Do_Not_Use_realloc_Use_safe_Realloc_Instead
  82 #define ExceptionOccurred Do_Not_Use_ExceptionOccurred_Use_safe_\
  83 ExceptionOccurred_Instead
  84 
  85 // These three functions throw std::bad_alloc in an out of memory condition
  86 // instead of returning 0. safe_Realloc will return 0 if memblock is not
  87 // NULL and size is 0. safe_Malloc and safe_Calloc will never return 0.
  88 void *safe_Malloc(size_t size) throw (std::bad_alloc);
  89 void *safe_Calloc(size_t num, size_t size) throw (std::bad_alloc);
  90 void *safe_Realloc(void *memblock, size_t size) throw (std::bad_alloc);
  91 
  92 // This function should be called instead of ExceptionOccurred. It throws
  93 // std::bad_alloc if a java.lang.OutOfMemoryError is currently pending
  94 // on the calling thread.
  95 jthrowable safe_ExceptionOccurred(JNIEnv *env) throw (std::bad_alloc);
  96 
  97 // This function is called at the beginning of an entry point.
  98 // Entry points are functions which are declared:
  99 //   1. CALLBACK,
 100 //   2. JNIEXPORT,
 101 //   3. __declspec(dllexport), or
 102 //   4. extern "C"
 103 // A function which returns an HRESULT (an OLE function) is also an entry
 104 // point.
 105 void entry_point(void);
 106 
 107 // This function hangs indefinitely if the Toolkit is not active
 108 void hang_if_shutdown(void);
 109 
 110 // This function throws awt_toolkit_shutdown if the Toolkit is not active
 111 void throw_if_shutdown(void) throw (awt_toolkit_shutdown);
 112 
 113 // This function is called when a std::bad_alloc exception is caught
 114 void handle_bad_alloc(void);
 115 
 116 // Uncomment to nondeterministically test OutOfMemory errors
 117 // #define OUTOFMEM_TEST
 118 
 119 #ifdef OUTOFMEM_TEST
 120     void *safe_Malloc_outofmem(size_t size, const char *, int)
 121         throw (std::bad_alloc);
 122     void *safe_Calloc_outofmem(size_t num, size_t size, const char *, int)
 123         throw (std::bad_alloc);
 124     void *safe_Realloc_outofmem(void *memblock, size_t size, const char *, int)
 125         throw (std::bad_alloc);
 126     void * CDECL operator new(size_t size, const char *, int)
 127         throw (std::bad_alloc);
 128 
 129     #define safe_Malloc(size) \
 130         safe_Malloc_outofmem(size, __FILE__, __LINE__)
 131     #define safe_Calloc(num, size) \
 132         safe_Calloc_outofmem(num, size, __FILE__, __LINE__)
 133     #define safe_Realloc(memblock, size) \
 134         safe_Realloc_outofmem(memblock, size, __FILE__, __LINE__)
 135     #define new new(__FILE__, __LINE__)
 136 #endif /* OUTOFMEM_TEST */
 137 
 138 #define TRY \
 139     try { \
 140         entry_point(); \
 141         hang_if_shutdown();
 142 // The _NO_HANG version of TRY causes the AWT native code to return to Java
 143 // immediately if the Toolkit is not active. Normal AWT operations should
 144 // never use this macro. It should only be used for cleanup routines where:
 145 // (1) Hanging is not a valid option, because the method is called during
 146 // execution of runFinalizersOnExit; and, (2) Execution of the method would
 147 // generate a NullPointerException or other Exception.
 148 #define TRY_NO_HANG \
 149     try { \
 150         entry_point(); \
 151         throw_if_shutdown();
 152 // The _NO_VERIFY version of TRY does not verify that the Toolkit is still
 153 // active before proceeding. Normal AWT operations should never use this
 154 // macro. It should only be used for cleanup routines which can safely
 155 // execute after the Toolkit is disposed, and then only with caution. Users
 156 // of this macro must be able to guarantee that the code which will execute
 157 // will not generate a NullPointerException or other Exception.
 158 #define TRY_NO_VERIFY \
 159     try { \
 160         entry_point();
 161 #define CATCH_BAD_ALLOC \
 162     } catch (std::bad_alloc&) { \
 163         handle_bad_alloc(); \
 164         return; \
 165     } catch (awt_toolkit_shutdown&) {\
 166         return; \
 167     }
 168 #define CATCH_BAD_ALLOC_RET(x) \
 169     } catch (std::bad_alloc&) { \
 170         handle_bad_alloc(); \
 171         return (x); \
 172     } catch (awt_toolkit_shutdown&) {\
 173         return (0); \
 174     }
 175 
 176 // The _NO_JNI versions of TRY and CATCH_BAD_ALLOC simply discard
 177 // std::bad_alloc exceptions and thus should be avoided at all costs. They
 178 // are only useful if the calling function currently holds the JNI lock
 179 // for the thread. This lock is acquired by calling GetPrimitiveArrayCritical
 180 // or GetStringCritical. No JNI function should be called by that thread
 181 // until the corresponding Release function has been called.
 182 
 183 #define TRY_NO_JNI \
 184     try { \
 185         hang_if_shutdown();
 186 #define TRY_NO_HANG_NO_JNI \
 187     try { \
 188         throw_if_shutdown();
 189 #define TRY_NO_VERIFY_NO_JNI \
 190     try {
 191 #define CATCH_BAD_ALLOC_NO_JNI \
 192     } catch (std::bad_alloc&) { \
 193         return; \
 194     } catch (awt_toolkit_shutdown&) {\
 195         return; \
 196     }
 197 #define CATCH_BAD_ALLOC_RET_NO_JNI(x) \
 198     } catch (std::bad_alloc&) { \
 199         return (x); \
 200     } catch (awt_toolkit_shutdown&) {\
 201         return (0); \
 202     }
 203 
 204 #endif /* _ALLOC_H_ */