hotspot/src/share/vm/memory/allocation.inline.hpp
author nloodin
Wed, 17 Oct 2012 17:36:48 +0200
changeset 14083 103054a71a30
parent 13963 e5b53c306fb5
child 15855 2ac9ebea17f3
permissions -rw-r--r--
8000617: It should be possible to allocate memory without the VM dying. Reviewed-by: coleenp, kamg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
13963
e5b53c306fb5 7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents: 13195
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "runtime/os.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// Explicit C-heap memory management
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
void trace_heap_malloc(size_t size, const char* name, void *p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
void trace_heap_free(void *p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
8325
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    35
#ifndef PRODUCT
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    36
// Increments unsigned long value for statistics (not atomic on MP).
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    37
inline void inc_stat_counter(volatile julong* dest, julong add_value) {
8325
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    38
#if defined(SPARC) || defined(X86)
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    39
  // Sparc and X86 have atomic jlong (8 bytes) instructions
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    40
  julong value = Atomic::load((volatile jlong*)dest);
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    41
  value += add_value;
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    42
  Atomic::store((jlong)value, (volatile jlong*)dest);
8325
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    43
#else
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    44
  // possible word-tearing during load/store
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    45
  *dest += add_value;
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    46
#endif
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    47
}
8325
cc62dbe71e6a 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 8320
diff changeset
    48
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// allocate using malloc; will fail if no memory available
14083
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    51
inline char* AllocateHeap(size_t size, MEMFLAGS flags, address pc = 0,
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    52
    AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    53
  if (pc == 0) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    54
    pc = CURRENT_PC;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    55
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    56
  char* p = (char*) os::malloc(size, flags, pc);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  #ifdef ASSERT
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    58
  if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  #endif
14083
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    60
  if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) vm_exit_out_of_memory(size, "AllocateHeap");
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    61
  return p;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    62
}
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    63
14083
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    64
inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flags,
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    65
    AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    66
  char* p = (char*) os::realloc(old, size, flags, CURRENT_PC);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    67
  #ifdef ASSERT
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    68
  if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    69
  #endif
14083
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    70
  if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) vm_exit_out_of_memory(size, "ReallocateHeap");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  return p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    74
inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  if (PrintMallocFree) trace_heap_free(p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  #endif
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    78
  os::free(p, memflags);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
}
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    80
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    81
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    82
template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    83
      address caller_pc){
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    84
#ifdef ASSERT
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    85
    void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC));
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    86
    if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    87
    return p;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    88
#else
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    89
    return (void *) AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC));
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    90
#endif
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    91
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    92
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    93
template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    94
  const std::nothrow_t&  nothrow_constant, address caller_pc) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    95
#ifdef ASSERT
14083
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    96
  void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC),
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
    97
      AllocFailStrategy::RETURN_NULL);
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    98
    if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
    99
    return p;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   100
#else
14083
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
   101
  return (void *) AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC),
103054a71a30 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 13963
diff changeset
   102
      AllocFailStrategy::RETURN_NULL);
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   103
#endif
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   104
}
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   105
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   106
template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   107
   FreeHeap(p, F);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   108
}
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   109
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8325
diff changeset
   110
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   111
#endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP