hotspot/src/share/vm/runtime/handles.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
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 1997-2003 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/_handles.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
oop* HandleArea::allocate_handle(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  assert(SharedSkipVerify || obj->is_oop(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  return real_allocate_handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
Handle::Handle(Thread* thread, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  assert(thread == Thread::current(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    _handle = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    _handle = thread->handle_area()->allocate_handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  oop* bottom = (oop*) chunk->bottom();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  oop* top    = (oop*) chunk_top;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  uintx handles_visited = top - bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // during GC phase 3, a handle may be a forward pointer that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // is not yet valid, so loosen the assertion
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  while (bottom < top) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
//    assert((*bottom)->is_oop(), "handle should point to oop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
      assert(Universe::heap()->is_in(*bottom), "handle should be valid heap address");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    f->do_oop(bottom++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  return handles_visited;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// Used for debugging handle allocation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
NOT_PRODUCT(jint _nof_handlemarks  = 0;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
void HandleArea::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  uintx handles_visited = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // First handle the current chunk. It is filled to the high water mark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  handles_visited += chunk_oops_do(f, _chunk, _hwm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  // Then handle all previous chunks. They are completely filled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  Chunk* k = _first;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  while(k != _chunk) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    handles_visited += chunk_oops_do(f, k, k->top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    k = k->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // The thread local handle areas should not get very large
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  if (TraceHandleAllocation && handles_visited > TotalHandleAllocationLimit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    warning("%d: Visited in HandleMark : %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      _nof_handlemarks, handles_visited);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    warning("Visited in HandleMark : %d", handles_visited);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  if (_prev != NULL) _prev->oops_do(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
void HandleMark::initialize(Thread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  _thread = thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // Save area
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  _area  = thread->handle_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // Save current top
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  _chunk = _area->_chunk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  _hwm   = _area->_hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  _max   = _area->_max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  NOT_PRODUCT(_size_in_bytes = _area->_size_in_bytes;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  debug_only(_area->_handle_mark_nesting++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  debug_only(Atomic::inc(&_nof_handlemarks);)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Link this in the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  set_previous_handle_mark(thread->last_handle_mark());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  thread->set_last_handle_mark(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
HandleMark::~HandleMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  HandleArea* area = _area;   // help compilers with poor alias analysis
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  assert(area == _thread->handle_area(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  debug_only(area->_handle_mark_nesting--);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // Debug code to trace the number of handles allocated per mark/
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  if (TraceHandleAllocation) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    size_t handles = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    Chunk *c = _chunk->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    if (c == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      handles = area->_hwm - _hwm; // no new chunk allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      handles = _max - _hwm;      // add rest in first chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      while(c != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
        handles += c->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
        c = c->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      handles -= area->_max - area->_hwm; // adjust for last trunk not full
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    handles /= sizeof(void *); // Adjust for size of a handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    if (handles > HandleAllocationLimit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      // Note: _nof_handlemarks is only set in debug mode
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Delete later chunks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  if( _chunk->next() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    _chunk->next_chop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  // Roll back arena to saved top markers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  area->_chunk = _chunk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  area->_hwm = _hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  area->_max = _max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  NOT_PRODUCT(area->set_size_in_bytes(_size_in_bytes);)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // clear out first chunk (to detect allocation bugs)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  if (ZapVMHandleArea) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    memset(_hwm, badHandleValue, _max - _hwm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  Atomic::dec(&_nof_handlemarks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // Unlink this from the thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  _thread->set_last_handle_mark(previous_handle_mark());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
NoHandleMark::NoHandleMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  HandleArea* area = Thread::current()->handle_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  area->_no_handle_mark_nesting++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
NoHandleMark::~NoHandleMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  HandleArea* area = Thread::current()->handle_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  area->_no_handle_mark_nesting--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
ResetNoHandleMark::ResetNoHandleMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  HandleArea* area = Thread::current()->handle_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  _no_handle_mark_nesting = area->_no_handle_mark_nesting;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  area->_no_handle_mark_nesting = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
ResetNoHandleMark::~ResetNoHandleMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  HandleArea* area = Thread::current()->handle_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  area->_no_handle_mark_nesting = _no_handle_mark_nesting;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
#endif