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