hotspot/src/share/vm/gc_implementation/shared/mutableSpace.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 616 4f2dfc0168e2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
void MutableSpace::initialize(MemRegion mr, bool clear_space) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  HeapWord* bottom = mr.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  HeapWord* end    = mr.end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
         "invalid space boundaries");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  set_bottom(bottom);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  set_end(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  if (clear_space) clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
void MutableSpace::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  set_top(bottom());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  if (ZapUnusedHeapArea) mangle_unused_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// This version requires locking. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
HeapWord* MutableSpace::allocate(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  assert(Heap_lock->owned_by_self() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
         (SafepointSynchronize::is_at_safepoint() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
          Thread::current()->is_VM_thread()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
         "not locked");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  HeapWord* obj = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  if (pointer_delta(end(), obj) >= size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    HeapWord* new_top = obj + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    set_top(new_top);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
           "checking alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    return obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// This version is lock-free.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
HeapWord* MutableSpace::cas_allocate(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    HeapWord* obj = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    if (pointer_delta(end(), obj) >= size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      HeapWord* new_top = obj + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      // result can be one of two:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      //  the old top value: the exchange succeeded
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
      //  otherwise: the new value of the top is returned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      if (result != obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
        continue; // another thread beat us to the allocation, try again
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
             "checking alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      return obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  } while (true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
// Try to deallocate previous allocation. Returns true upon success.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  HeapWord* expected_top = obj + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
void MutableSpace::oop_iterate(OopClosure* cl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  HeapWord* obj_addr = bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  HeapWord* t = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // Could call objects iterate, but this is easier.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  while (obj_addr < t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    obj_addr += oop(obj_addr)->oop_iterate(cl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
void MutableSpace::object_iterate(ObjectClosure* cl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  HeapWord* p = bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  while (p < top()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    cl->do_object(oop(p));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    p += oop(p)->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
void MutableSpace::print_short() const { print_short_on(tty); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
void MutableSpace::print_short_on( outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
            (int) ((double) used_in_bytes() * 100 / capacity_in_bytes()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
void MutableSpace::print() const { print_on(tty); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
void MutableSpace::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  MutableSpace::print_short_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
                 bottom(), top(), end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
void MutableSpace::verify(bool allow_dirty) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  HeapWord* p = bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  HeapWord* t = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  HeapWord* prev_p = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  while (p < t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    oop(p)->verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    prev_p = p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    p += oop(p)->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  guarantee(p == top(), "end of last object must match end of space");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}