handles.cpp revision 9056:dc9930a04ab0
1193323Sed/*
2193323Sed * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.
8193323Sed *
9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193323Sed * version 2 for more details (a copy is included in the LICENSE file that
13193323Sed * accompanied this code).
14193323Sed *
15193323Sed * You should have received a copy of the GNU General Public License version
16193323Sed * 2 along with this work; if not, write to the Free Software Foundation,
17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18193323Sed *
19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20193323Sed * or visit www.oracle.com if you need additional information or have any
21193323Sed * questions.
22193323Sed *
23193323Sed */
24198892Srdivacky
25198892Srdivacky#include "precompiled.hpp"
26193323Sed#include "memory/allocation.inline.hpp"
27193323Sed#include "oops/constantPool.hpp"
28249423Sdim#include "oops/oop.inline.hpp"
29249423Sdim#include "runtime/atomic.inline.hpp"
30249423Sdim#include "runtime/handles.inline.hpp"
31249423Sdim#include "runtime/thread.inline.hpp"
32249423Sdim
33249423SdimPRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
34249423Sdim
35249423Sdim#ifdef ASSERT
36249423Sdimoop* HandleArea::allocate_handle(oop obj) {
37249423Sdim  assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
38249423Sdim  assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
39249423Sdim  assert(obj->is_oop(), "not an oop: " INTPTR_FORMAT, (intptr_t*) obj);
40249423Sdim  return real_allocate_handle(obj);
41249423Sdim}
42193323Sed
43193323SedHandle::Handle(Thread* thread, oop obj) {
44193323Sed  assert(thread == Thread::current(), "sanity check");
45193323Sed  if (obj == NULL) {
46193323Sed    _handle = NULL;
47193323Sed  } else {
48193323Sed    _handle = thread->handle_area()->allocate_handle(obj);
49193323Sed  }
50193323Sed}
51193323Sed
52193323Sed#endif
53193323Sed
54193323Sedstatic uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
55193323Sed  oop* bottom = (oop*) chunk->bottom();
56198090Srdivacky  oop* top    = (oop*) chunk_top;
57198892Srdivacky  uintx handles_visited = top - bottom;
58193323Sed  assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
59193323Sed  // during GC phase 3, a handle may be a forward pointer that
60234353Sdim  // is not yet valid, so loosen the assertion
61193323Sed  while (bottom < top) {
62193323Sed    // This test can be moved up but for now check every oop.
63193323Sed
64193323Sed    assert((*bottom)->is_oop(), "handle should point to oop");
65193323Sed
66193323Sed    f->do_oop(bottom++);
67193323Sed  }
68193323Sed  return handles_visited;
69234353Sdim}
70193323Sed
71193323Sed// Used for debugging handle allocation.
72193323SedNOT_PRODUCT(jint _nof_handlemarks  = 0;)
73198892Srdivacky
74198892Srdivackyvoid HandleArea::oops_do(OopClosure* f) {
75198892Srdivacky  uintx handles_visited = 0;
76198892Srdivacky  // First handle the current chunk. It is filled to the high water mark.
77198892Srdivacky  handles_visited += chunk_oops_do(f, _chunk, _hwm);
78234353Sdim  // Then handle all previous chunks. They are completely filled.
79198892Srdivacky  Chunk* k = _first;
80198892Srdivacky  while(k != _chunk) {
81198892Srdivacky    handles_visited += chunk_oops_do(f, k, k->top());
82234353Sdim    k = k->next();
83193323Sed  }
84198892Srdivacky
85234353Sdim  // The thread local handle areas should not get very large
86198892Srdivacky  if (TraceHandleAllocation && (size_t)handles_visited > TotalHandleAllocationLimit) {
87198892Srdivacky#ifdef ASSERT
88198892Srdivacky    warning("%d: Visited in HandleMark : %d",
89193323Sed      _nof_handlemarks, handles_visited);
90198892Srdivacky#else
91234353Sdim    warning("Visited in HandleMark : %d", handles_visited);
92198892Srdivacky#endif
93198892Srdivacky  }
94198892Srdivacky  if (_prev != NULL) _prev->oops_do(f);
95198892Srdivacky}
96234353Sdim
97198892Srdivackyvoid HandleMark::initialize(Thread* thread) {
98198892Srdivacky  _thread = thread;
99198892Srdivacky  // Save area
100198892Srdivacky  _area  = thread->handle_area();
101234353Sdim  // Save current top
102198892Srdivacky  _chunk = _area->_chunk;
103198892Srdivacky  _hwm   = _area->_hwm;
104198892Srdivacky  _max   = _area->_max;
105193323Sed  _size_in_bytes = _area->_size_in_bytes;
106198892Srdivacky  debug_only(_area->_handle_mark_nesting++);
107198892Srdivacky  assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
108198892Srdivacky  debug_only(Atomic::inc(&_nof_handlemarks);)
109198892Srdivacky
110198892Srdivacky  // Link this in the thread
111198892Srdivacky  set_previous_handle_mark(thread->last_handle_mark());
112234353Sdim  thread->set_last_handle_mark(this);
113198892Srdivacky}
114198892Srdivacky
115198892Srdivacky
116198892SrdivackyHandleMark::~HandleMark() {
117193323Sed  HandleArea* area = _area;   // help compilers with poor alias analysis
118234353Sdim  assert(area == _thread->handle_area(), "sanity check");
119198892Srdivacky  assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
120198892Srdivacky  debug_only(area->_handle_mark_nesting--);
121198892Srdivacky
122234353Sdim  // Debug code to trace the number of handles allocated per mark/
123198892Srdivacky#ifdef ASSERT
124198892Srdivacky  if (TraceHandleAllocation) {
125198892Srdivacky    size_t handles = 0;
126198892Srdivacky    Chunk *c = _chunk->next();
127193323Sed    if (c == NULL) {
128198892Srdivacky      handles = area->_hwm - _hwm; // no new chunk allocated
129193323Sed    } else {
130193323Sed      handles = _max - _hwm;      // add rest in first chunk
131198892Srdivacky      while(c != NULL) {
132198892Srdivacky        handles += c->length();
133198892Srdivacky        c = c->next();
134198892Srdivacky      }
135198892Srdivacky      handles -= area->_max - area->_hwm; // adjust for last trunk not full
136198892Srdivacky    }
137193323Sed    handles /= sizeof(void *); // Adjust for size of a handle
138234353Sdim    if (handles > HandleAllocationLimit) {
139198892Srdivacky      // Note: _nof_handlemarks is only set in debug mode
140198892Srdivacky      warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles);
141198892Srdivacky    }
142198892Srdivacky
143193323Sed    tty->print_cr("Handles %d", handles);
144193323Sed  }
145198892Srdivacky#endif
146193323Sed
147198892Srdivacky  // Delete later chunks
148198892Srdivacky  if( _chunk->next() ) {
149198892Srdivacky    // reset arena size before delete chunks. Otherwise, the total
150193323Sed    // arena size could exceed total chunk size
151193323Sed    assert(area->size_in_bytes() > size_in_bytes(), "Sanity check");
152193323Sed    area->set_size_in_bytes(size_in_bytes());
153193323Sed    _chunk->next_chop();
154193323Sed  } else {
155193323Sed    assert(area->size_in_bytes() == size_in_bytes(), "Sanity check");
156243830Sdim  }
157234353Sdim  // Roll back arena to saved top markers
158226633Sdim  area->_chunk = _chunk;
159198892Srdivacky  area->_hwm = _hwm;
160193323Sed  area->_max = _max;
161198892Srdivacky#ifdef ASSERT
162198892Srdivacky  // clear out first chunk (to detect allocation bugs)
163198892Srdivacky  if (ZapVMHandleArea) {
164198892Srdivacky    memset(_hwm, badHandleValue, _max - _hwm);
165234353Sdim  }
166193323Sed  Atomic::dec(&_nof_handlemarks);
167193323Sed#endif
168193323Sed
169193323Sed  // Unlink this from the thread
170193323Sed  _thread->set_last_handle_mark(previous_handle_mark());
171193323Sed}
172193323Sed
173193323Sedvoid* HandleMark::operator new(size_t size) throw() {
174193323Sed  return AllocateHeap(size, mtThread);
175193323Sed}
176193323Sed
177193323Sedvoid* HandleMark::operator new [] (size_t size) throw() {
178193323Sed  return AllocateHeap(size, mtThread);
179193323Sed}
180234353Sdim
181198892Srdivackyvoid HandleMark::operator delete(void* p) {
182198892Srdivacky  FreeHeap(p);
183198892Srdivacky}
184193323Sed
185198892Srdivackyvoid HandleMark::operator delete[](void* p) {
186198892Srdivacky  FreeHeap(p);
187198892Srdivacky}
188198892Srdivacky
189234353Sdim#ifdef ASSERT
190198892Srdivacky
191198892SrdivackyNoHandleMark::NoHandleMark() {
192198892Srdivacky  HandleArea* area = Thread::current()->handle_area();
193198892Srdivacky  area->_no_handle_mark_nesting++;
194198892Srdivacky  assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
195198892Srdivacky}
196198892Srdivacky
197193323Sed
198193323SedNoHandleMark::~NoHandleMark() {
199193323Sed  HandleArea* area = Thread::current()->handle_area();
200193323Sed  assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
201193323Sed  area->_no_handle_mark_nesting--;
202193323Sed}
203193323Sed
204193323Sed
205193323SedResetNoHandleMark::ResetNoHandleMark() {
206193323Sed  HandleArea* area = Thread::current()->handle_area();
207193323Sed  _no_handle_mark_nesting = area->_no_handle_mark_nesting;
208243830Sdim  area->_no_handle_mark_nesting = 0;
209234353Sdim}
210193323Sed
211193323Sed
212193323SedResetNoHandleMark::~ResetNoHandleMark() {
213198892Srdivacky  HandleArea* area = Thread::current()->handle_area();
214198892Srdivacky  area->_no_handle_mark_nesting = _no_handle_mark_nesting;
215198892Srdivacky}
216198892Srdivacky
217263508Sdimbool instanceKlassHandle::is_instanceKlass(const Klass* k) {
218193323Sed  return k->oop_is_instance();
219198892Srdivacky}
220193323Sed
221193323Sed#endif
222193323Sed