hotspot/src/share/vm/gc_implementation/shared/mutableSpace.cpp
author jmasa
Mon, 28 Jul 2008 15:30:23 -0700
changeset 977 b90650e2a9f7
parent 670 ddf3e9583f2f
parent 971 f0b20be4165d
child 1911 b7cfe7eb809c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
670
ddf3e9583f2f 6719955: Update copyright year
xdono
parents: 616
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_mutableSpace.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
971
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    28
MutableSpace::MutableSpace(): ImmutableSpace(), _top(NULL) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    29
  _mangler = new MutableSpaceMangler(this);
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    30
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    31
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    32
MutableSpace::~MutableSpace() {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    33
  delete _mangler;
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    34
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    35
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    36
void MutableSpace::initialize(MemRegion mr,
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    37
                              bool clear_space,
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    38
                              bool mangle_space) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  HeapWord* bottom = mr.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  HeapWord* end    = mr.end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
         "invalid space boundaries");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  set_bottom(bottom);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  set_end(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
971
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    47
  if (clear_space) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    48
    clear(mangle_space);
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    49
  }
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    50
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    51
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    52
void MutableSpace::clear(bool mangle_space) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    53
  set_top(bottom());
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    54
  if (ZapUnusedHeapArea && mangle_space) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    55
    mangle_unused_area();
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    56
  }
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    57
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    58
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    59
#ifndef PRODUCT
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    60
void MutableSpace::check_mangled_unused_area(HeapWord* limit) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    61
  mangler()->check_mangled_unused_area(limit);
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    62
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    63
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    64
void MutableSpace::check_mangled_unused_area_complete() {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    65
  mangler()->check_mangled_unused_area_complete();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
971
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    68
// Mangle only the unused space that has not previously
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    69
// been mangled and that has not been allocated since being
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    70
// mangled.
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    71
void MutableSpace::mangle_unused_area() {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    72
  mangler()->mangle_unused_area();
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    73
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    74
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    75
void MutableSpace::mangle_unused_area_complete() {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    76
  mangler()->mangle_unused_area_complete();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
971
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    79
void MutableSpace::mangle_region(MemRegion mr) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    80
  SpaceMangler::mangle_region(mr);
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    81
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    82
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    83
void MutableSpace::set_top_for_allocations(HeapWord* v) {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    84
  mangler()->set_top_for_allocations(v);
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    85
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    86
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    87
void MutableSpace::set_top_for_allocations() {
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    88
  mangler()->set_top_for_allocations(top());
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    89
}
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    90
#endif
f0b20be4165d 6672698: mangle_unused_area() should not remangle the entire heap at each collection.
jmasa
parents: 616
diff changeset
    91
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
// This version requires locking. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
HeapWord* MutableSpace::allocate(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  assert(Heap_lock->owned_by_self() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
         (SafepointSynchronize::is_at_safepoint() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
          Thread::current()->is_VM_thread()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
         "not locked");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  HeapWord* obj = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  if (pointer_delta(end(), obj) >= size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    HeapWord* new_top = obj + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    set_top(new_top);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
           "checking alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
// This version is lock-free.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
HeapWord* MutableSpace::cas_allocate(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    HeapWord* obj = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    if (pointer_delta(end(), obj) >= size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      HeapWord* new_top = obj + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      // result can be one of two:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
      //  the old top value: the exchange succeeded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      //  otherwise: the new value of the top is returned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      if (result != obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
        continue; // another thread beat us to the allocation, try again
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
             "checking alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      return obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  } while (true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
// Try to deallocate previous allocation. Returns true upon success.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  HeapWord* expected_top = obj + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
void MutableSpace::oop_iterate(OopClosure* cl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  HeapWord* obj_addr = bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  HeapWord* t = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // Could call objects iterate, but this is easier.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  while (obj_addr < t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    obj_addr += oop(obj_addr)->oop_iterate(cl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
void MutableSpace::object_iterate(ObjectClosure* cl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  HeapWord* p = bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  while (p < top()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    cl->do_object(oop(p));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    p += oop(p)->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
void MutableSpace::print_short() const { print_short_on(tty); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
void MutableSpace::print_short_on( outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
            (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
void MutableSpace::print() const { print_on(tty); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
void MutableSpace::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  MutableSpace::print_short_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
                 bottom(), top(), end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
616
4f2dfc0168e2 6711930: NUMA allocator: ParOld can create a hole less than minimal object size in the lgrp chunk
iveresov
parents: 1
diff changeset
   168
void MutableSpace::verify(bool allow_dirty) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  HeapWord* p = bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  HeapWord* t = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  HeapWord* prev_p = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  while (p < t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    oop(p)->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    prev_p = p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    p += oop(p)->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  guarantee(p == top(), "end of last object must match end of space");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
}