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