< prev index next >

src/share/vm/memory/allocation.inline.hpp

Print this page




  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_MEMORY_ALLOCATION_INLINE_HPP
  26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
  27 
  28 #include "runtime/atomic.inline.hpp"
  29 #include "runtime/os.hpp"
  30 #include "services/memTracker.hpp"

  31 
  32 // Explicit C-heap memory management
  33 
  34 void trace_heap_malloc(size_t size, const char* name, void *p);
  35 void trace_heap_free(void *p);
  36 
  37 #ifndef PRODUCT
  38 // Increments unsigned long value for statistics (not atomic on MP).
  39 inline void inc_stat_counter(volatile julong* dest, julong add_value) {
  40 #if defined(SPARC) || defined(X86)
  41   // Sparc and X86 have atomic jlong (8 bytes) instructions
  42   julong value = Atomic::load((volatile jlong*)dest);
  43   value += add_value;
  44   Atomic::store((jlong)value, (volatile jlong*)dest);
  45 #else
  46   // possible word-tearing during load/store
  47   *dest += add_value;
  48 #endif
  49 }
  50 #endif
  51 
  52 // allocate using malloc; will fail if no memory available
  53 inline char* AllocateHeap(size_t size, MEMFLAGS flags,
  54     const NativeCallStack& stack,
  55     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  56   char* p = (char*) os::malloc(size, flags, stack);
  57   #ifdef ASSERT
  58   if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);
  59   #endif
  60   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  61     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
  62   }
  63   return p;
  64 }
  65 
  66 #ifdef __GNUC__
  67 __attribute__((always_inline))
  68 #endif
  69 inline char* AllocateHeap(size_t size, MEMFLAGS flags,
  70     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  71   return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode);
  72 }
  73 
  74 #ifdef __GNUC__
  75 __attribute__((always_inline))
  76 #endif
  77 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag,
  78     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  79   char* p = (char*) os::realloc(old, size, flag, CURRENT_PC);
  80   #ifdef ASSERT
  81   if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);
  82   #endif
  83   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  84     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
  85   }
  86   return p;
  87 }
  88 
  89 inline void FreeHeap(void* p) {
  90   #ifdef ASSERT
  91   if (PrintMallocFree) trace_heap_free(p);
  92   #endif
  93   os::free(p);
  94 }
  95 
  96 
  97 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,




  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_MEMORY_ALLOCATION_INLINE_HPP
  26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
  27 
  28 #include "runtime/atomic.inline.hpp"
  29 #include "runtime/os.hpp"
  30 #include "services/memTracker.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 // Explicit C-heap memory management
  34 
  35 void trace_heap_malloc(size_t size, const char* name, void *p);
  36 void trace_heap_free(void *p);
  37 
  38 #ifndef PRODUCT
  39 // Increments unsigned long value for statistics (not atomic on MP).
  40 inline void inc_stat_counter(volatile julong* dest, julong add_value) {
  41 #if defined(SPARC) || defined(X86)
  42   // Sparc and X86 have atomic jlong (8 bytes) instructions
  43   julong value = Atomic::load((volatile jlong*)dest);
  44   value += add_value;
  45   Atomic::store((jlong)value, (volatile jlong*)dest);
  46 #else
  47   // possible word-tearing during load/store
  48   *dest += add_value;
  49 #endif
  50 }
  51 #endif
  52 
  53 // allocate using malloc; will fail if no memory available
  54 inline char* AllocateHeap(size_t size, MEMFLAGS flags,
  55     const NativeCallStack& stack,
  56     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  57   char* p = (char*) os::malloc(size, flags, stack);
  58   #ifdef ASSERT
  59   if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);
  60   #endif
  61   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  62     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
  63   }
  64   return p;
  65 }
  66 
  67 ALWAYSINLINE char* AllocateHeap(size_t size, MEMFLAGS flags,



  68     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  69   return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode);
  70 }
  71 
  72 ALWAYSINLINE char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag,



  73     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  74   char* p = (char*) os::realloc(old, size, flag, CURRENT_PC);
  75   #ifdef ASSERT
  76   if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);
  77   #endif
  78   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  79     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
  80   }
  81   return p;
  82 }
  83 
  84 inline void FreeHeap(void* p) {
  85   #ifdef ASSERT
  86   if (PrintMallocFree) trace_heap_free(p);
  87   #endif
  88   os::free(p);
  89 }
  90 
  91 
  92 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,


< prev index next >