handles.cpp revision 6646:b596a1063e90
1/*
2 * Copyright (c) 1997, 2014, 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 "precompiled.hpp"
26#include "memory/allocation.inline.hpp"
27#include "oops/constantPool.hpp"
28#include "oops/oop.inline.hpp"
29#include "runtime/atomic.inline.hpp"
30#include "runtime/handles.inline.hpp"
31#include "runtime/thread.inline.hpp"
32#ifdef TARGET_OS_FAMILY_linux
33# include "os_linux.inline.hpp"
34#endif
35#ifdef TARGET_OS_FAMILY_solaris
36# include "os_solaris.inline.hpp"
37#endif
38#ifdef TARGET_OS_FAMILY_windows
39# include "os_windows.inline.hpp"
40#endif
41#ifdef TARGET_OS_FAMILY_bsd
42# include "os_bsd.inline.hpp"
43#endif
44
45PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
46
47#ifdef ASSERT
48oop* HandleArea::allocate_handle(oop obj) {
49  assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark");
50  assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark");
51  assert(obj->is_oop(), err_msg("not an oop: " INTPTR_FORMAT, (intptr_t*) obj));
52  return real_allocate_handle(obj);
53}
54
55Handle::Handle(Thread* thread, oop obj) {
56  assert(thread == Thread::current(), "sanity check");
57  if (obj == NULL) {
58    _handle = NULL;
59  } else {
60    _handle = thread->handle_area()->allocate_handle(obj);
61  }
62}
63
64#endif
65
66static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
67  oop* bottom = (oop*) chunk->bottom();
68  oop* top    = (oop*) chunk_top;
69  uintx handles_visited = top - bottom;
70  assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
71  // during GC phase 3, a handle may be a forward pointer that
72  // is not yet valid, so loosen the assertion
73  while (bottom < top) {
74    // This test can be moved up but for now check every oop.
75
76    assert((*bottom)->is_oop(), "handle should point to oop");
77
78    f->do_oop(bottom++);
79  }
80  return handles_visited;
81}
82
83// Used for debugging handle allocation.
84NOT_PRODUCT(jint _nof_handlemarks  = 0;)
85
86void HandleArea::oops_do(OopClosure* f) {
87  uintx handles_visited = 0;
88  // First handle the current chunk. It is filled to the high water mark.
89  handles_visited += chunk_oops_do(f, _chunk, _hwm);
90  // Then handle all previous chunks. They are completely filled.
91  Chunk* k = _first;
92  while(k != _chunk) {
93    handles_visited += chunk_oops_do(f, k, k->top());
94    k = k->next();
95  }
96
97  // The thread local handle areas should not get very large
98  if (TraceHandleAllocation && handles_visited > TotalHandleAllocationLimit) {
99#ifdef ASSERT
100    warning("%d: Visited in HandleMark : %d",
101      _nof_handlemarks, handles_visited);
102#else
103    warning("Visited in HandleMark : %d", handles_visited);
104#endif
105  }
106  if (_prev != NULL) _prev->oops_do(f);
107}
108
109void HandleMark::initialize(Thread* thread) {
110  _thread = thread;
111  // Save area
112  _area  = thread->handle_area();
113  // Save current top
114  _chunk = _area->_chunk;
115  _hwm   = _area->_hwm;
116  _max   = _area->_max;
117  _size_in_bytes = _area->_size_in_bytes;
118  debug_only(_area->_handle_mark_nesting++);
119  assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
120  debug_only(Atomic::inc(&_nof_handlemarks);)
121
122  // Link this in the thread
123  set_previous_handle_mark(thread->last_handle_mark());
124  thread->set_last_handle_mark(this);
125}
126
127
128HandleMark::~HandleMark() {
129  HandleArea* area = _area;   // help compilers with poor alias analysis
130  assert(area == _thread->handle_area(), "sanity check");
131  assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
132  debug_only(area->_handle_mark_nesting--);
133
134  // Debug code to trace the number of handles allocated per mark/
135#ifdef ASSERT
136  if (TraceHandleAllocation) {
137    size_t handles = 0;
138    Chunk *c = _chunk->next();
139    if (c == NULL) {
140      handles = area->_hwm - _hwm; // no new chunk allocated
141    } else {
142      handles = _max - _hwm;      // add rest in first chunk
143      while(c != NULL) {
144        handles += c->length();
145        c = c->next();
146      }
147      handles -= area->_max - area->_hwm; // adjust for last trunk not full
148    }
149    handles /= sizeof(void *); // Adjust for size of a handle
150    if (handles > HandleAllocationLimit) {
151      // Note: _nof_handlemarks is only set in debug mode
152      warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles);
153    }
154
155    tty->print_cr("Handles %d", handles);
156  }
157#endif
158
159  // Delete later chunks
160  if( _chunk->next() ) {
161    // reset arena size before delete chunks. Otherwise, the total
162    // arena size could exceed total chunk size
163    assert(area->size_in_bytes() > size_in_bytes(), "Sanity check");
164    area->set_size_in_bytes(size_in_bytes());
165    _chunk->next_chop();
166  } else {
167    assert(area->size_in_bytes() == size_in_bytes(), "Sanity check");
168  }
169  // Roll back arena to saved top markers
170  area->_chunk = _chunk;
171  area->_hwm = _hwm;
172  area->_max = _max;
173#ifdef ASSERT
174  // clear out first chunk (to detect allocation bugs)
175  if (ZapVMHandleArea) {
176    memset(_hwm, badHandleValue, _max - _hwm);
177  }
178  Atomic::dec(&_nof_handlemarks);
179#endif
180
181  // Unlink this from the thread
182  _thread->set_last_handle_mark(previous_handle_mark());
183}
184
185void* HandleMark::operator new(size_t size) throw() {
186  return AllocateHeap(size, mtThread);
187}
188
189void* HandleMark::operator new [] (size_t size) throw() {
190  return AllocateHeap(size, mtThread);
191}
192
193void HandleMark::operator delete(void* p) {
194  FreeHeap(p, mtThread);
195}
196
197void HandleMark::operator delete[](void* p) {
198  FreeHeap(p, mtThread);
199}
200
201#ifdef ASSERT
202
203NoHandleMark::NoHandleMark() {
204  HandleArea* area = Thread::current()->handle_area();
205  area->_no_handle_mark_nesting++;
206  assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
207}
208
209
210NoHandleMark::~NoHandleMark() {
211  HandleArea* area = Thread::current()->handle_area();
212  assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
213  area->_no_handle_mark_nesting--;
214}
215
216
217ResetNoHandleMark::ResetNoHandleMark() {
218  HandleArea* area = Thread::current()->handle_area();
219  _no_handle_mark_nesting = area->_no_handle_mark_nesting;
220  area->_no_handle_mark_nesting = 0;
221}
222
223
224ResetNoHandleMark::~ResetNoHandleMark() {
225  HandleArea* area = Thread::current()->handle_area();
226  area->_no_handle_mark_nesting = _no_handle_mark_nesting;
227}
228
229#endif
230