2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2015-02-16 14:07:36 +01:00
|
|
|
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "precompiled.hpp"
|
|
|
|
#include "memory/heap.hpp"
|
|
|
|
#include "oops/oop.inline.hpp"
|
|
|
|
#include "runtime/os.hpp"
|
2012-06-28 17:03:16 -04:00
|
|
|
#include "services/memTracker.hpp"
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
size_t CodeHeap::header_size() {
|
|
|
|
return sizeof(HeapBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Implementation of Heap
|
|
|
|
|
2014-09-17 08:00:07 +02:00
|
|
|
CodeHeap::CodeHeap(const char* name, const int code_blob_type)
|
|
|
|
: _code_blob_type(code_blob_type) {
|
|
|
|
_name = name;
|
2007-12-01 00:00:00 +00:00
|
|
|
_number_of_committed_segments = 0;
|
|
|
|
_number_of_reserved_segments = 0;
|
|
|
|
_segment_size = 0;
|
|
|
|
_log2_segment_size = 0;
|
|
|
|
_next_segment = 0;
|
|
|
|
_freelist = NULL;
|
2013-04-11 13:57:44 +02:00
|
|
|
_freelist_segments = 0;
|
2014-03-07 07:42:40 +01:00
|
|
|
_freelist_length = 0;
|
2014-09-17 08:00:07 +02:00
|
|
|
_max_allocated_capacity = 0;
|
2015-11-09 11:35:44 +01:00
|
|
|
_blob_count = 0;
|
|
|
|
_nmethod_count = 0;
|
|
|
|
_adapter_count = 0;
|
|
|
|
_full_count = 0;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) {
|
2015-02-16 14:07:36 +01:00
|
|
|
assert( beg < _number_of_committed_segments, "interval begin out of bounds");
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds");
|
|
|
|
// setup _segmap pointers for faster indexing
|
|
|
|
address p = (address)_segmap.low() + beg;
|
|
|
|
address q = (address)_segmap.low() + end;
|
|
|
|
// initialize interval
|
2014-03-07 07:42:40 +01:00
|
|
|
while (p < q) *p++ = free_sentinel;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) {
|
2015-02-16 14:07:36 +01:00
|
|
|
assert( beg < _number_of_committed_segments, "interval begin out of bounds");
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds");
|
|
|
|
// setup _segmap pointers for faster indexing
|
|
|
|
address p = (address)_segmap.low() + beg;
|
|
|
|
address q = (address)_segmap.low() + end;
|
|
|
|
// initialize interval
|
|
|
|
int i = 0;
|
|
|
|
while (p < q) {
|
|
|
|
*p++ = i++;
|
2014-03-07 07:42:40 +01:00
|
|
|
if (i == free_sentinel) i = 1;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t align_to_page_size(size_t size) {
|
|
|
|
const size_t alignment = (size_t)os::vm_page_size();
|
|
|
|
assert(is_power_of_2(alignment), "no kidding ???");
|
|
|
|
return (size + alignment - 1) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeHeap::on_code_mapping(char* base, size_t size) {
|
|
|
|
#ifdef LINUX
|
|
|
|
extern void linux_wrap_code(char* base, size_t size);
|
|
|
|
linux_wrap_code(base, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-17 08:00:07 +02:00
|
|
|
bool CodeHeap::reserve(ReservedSpace rs, size_t committed_size, size_t segment_size) {
|
|
|
|
assert(rs.size() >= committed_size, "reserved < committed");
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(segment_size >= sizeof(FreeBlock), "segment size is too small");
|
|
|
|
assert(is_power_of_2(segment_size), "segment_size must be a power of 2");
|
|
|
|
|
|
|
|
_segment_size = segment_size;
|
|
|
|
_log2_segment_size = exact_log2(segment_size);
|
|
|
|
|
|
|
|
// Reserve and initialize space for _memory.
|
2014-06-24 15:50:50 +02:00
|
|
|
size_t page_size = os::vm_page_size();
|
|
|
|
if (os::can_execute_large_page_memory()) {
|
|
|
|
const size_t min_pages = 8;
|
2015-01-16 10:29:12 +01:00
|
|
|
page_size = MIN2(os::page_size_for_region_aligned(committed_size, min_pages),
|
|
|
|
os::page_size_for_region_aligned(rs.size(), min_pages));
|
2014-06-24 15:50:50 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
const size_t granularity = os::vm_allocation_granularity();
|
|
|
|
const size_t c_size = align_size_up(committed_size, page_size);
|
|
|
|
|
2014-09-17 08:00:07 +02:00
|
|
|
os::trace_page_sizes(_name, committed_size, rs.size(), page_size,
|
2007-12-01 00:00:00 +00:00
|
|
|
rs.base(), rs.size());
|
|
|
|
if (!_memory.initialize(rs, c_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_code_mapping(_memory.low(), _memory.committed_size());
|
2013-04-11 13:57:44 +02:00
|
|
|
_number_of_committed_segments = size_to_segments(_memory.committed_size());
|
|
|
|
_number_of_reserved_segments = size_to_segments(_memory.reserved_size());
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
|
2013-08-15 20:04:10 -04:00
|
|
|
const size_t reserved_segments_alignment = MAX2((size_t)os::vm_page_size(), granularity);
|
|
|
|
const size_t reserved_segments_size = align_size_up(_number_of_reserved_segments, reserved_segments_alignment);
|
|
|
|
const size_t committed_segments_size = align_to_page_size(_number_of_committed_segments);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// reserve space for _segmap
|
2013-08-15 20:04:10 -04:00
|
|
|
if (!_segmap.initialize(reserved_segments_size, committed_segments_size)) {
|
2007-12-01 00:00:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-28 17:03:16 -04:00
|
|
|
|
|
|
|
MemTracker::record_virtual_memory_type((address)_segmap.low_boundary(), mtCode);
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit enough space for segment map");
|
|
|
|
assert(_segmap.reserved_size() >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map");
|
|
|
|
assert(_segmap.reserved_size() >= _segmap.committed_size() , "just checking");
|
|
|
|
|
|
|
|
// initialize remaining instance variables
|
|
|
|
clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CodeHeap::expand_by(size_t size) {
|
|
|
|
// expand _memory space
|
|
|
|
size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
|
|
|
|
if (dm > 0) {
|
|
|
|
char* base = _memory.low() + _memory.committed_size();
|
|
|
|
if (!_memory.expand_by(dm)) return false;
|
|
|
|
on_code_mapping(base, dm);
|
|
|
|
size_t i = _number_of_committed_segments;
|
2013-04-11 13:57:44 +02:00
|
|
|
_number_of_committed_segments = size_to_segments(_memory.committed_size());
|
|
|
|
assert(_number_of_reserved_segments == size_to_segments(_memory.reserved_size()), "number of reserved segments should not change");
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
|
|
|
|
// expand _segmap space
|
|
|
|
size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
|
2014-03-07 07:42:40 +01:00
|
|
|
if ((ds > 0) && !_segmap.expand_by(ds)) {
|
|
|
|
return false;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
|
|
|
|
// initialize additional segmap entries
|
|
|
|
mark_segmap_as_free(i, _number_of_committed_segments);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeHeap::clear() {
|
|
|
|
_next_segment = 0;
|
|
|
|
mark_segmap_as_free(0, _number_of_committed_segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-24 14:25:46 +02:00
|
|
|
void* CodeHeap::allocate(size_t instance_size) {
|
2014-03-07 07:42:40 +01:00
|
|
|
size_t number_of_segments = size_to_segments(instance_size + header_size());
|
2013-04-11 13:57:44 +02:00
|
|
|
assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList");
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2014-01-23 14:47:23 +01:00
|
|
|
// First check if we can satisfy request from freelist
|
2014-03-07 07:42:40 +01:00
|
|
|
NOT_PRODUCT(verify());
|
2014-10-24 14:25:46 +02:00
|
|
|
HeapBlock* block = search_freelist(number_of_segments);
|
2014-03-07 07:42:40 +01:00
|
|
|
NOT_PRODUCT(verify());
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
if (block != NULL) {
|
2013-04-11 13:57:44 +02:00
|
|
|
assert(block->length() >= number_of_segments && block->length() < number_of_segments + CodeCacheMinBlockLength, "sanity check");
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(!block->free(), "must be marked free");
|
2014-03-07 07:42:40 +01:00
|
|
|
DEBUG_ONLY(memset((void*)block->allocated_space(), badCodeHeapNewVal, instance_size));
|
2014-09-17 08:00:07 +02:00
|
|
|
_max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
|
2015-11-09 11:35:44 +01:00
|
|
|
_blob_count++;
|
2007-12-01 00:00:00 +00:00
|
|
|
return block->allocated_space();
|
|
|
|
}
|
|
|
|
|
2013-04-11 13:57:44 +02:00
|
|
|
// Ensure minimum size for allocation to the heap.
|
2014-03-07 07:42:40 +01:00
|
|
|
number_of_segments = MAX2((int)CodeCacheMinBlockLength, (int)number_of_segments);
|
2013-04-11 13:57:44 +02:00
|
|
|
|
|
|
|
if (_next_segment + number_of_segments <= _number_of_committed_segments) {
|
|
|
|
mark_segmap_as_used(_next_segment, _next_segment + number_of_segments);
|
2007-12-01 00:00:00 +00:00
|
|
|
HeapBlock* b = block_at(_next_segment);
|
2013-04-11 13:57:44 +02:00
|
|
|
b->initialize(number_of_segments);
|
|
|
|
_next_segment += number_of_segments;
|
2014-03-07 07:42:40 +01:00
|
|
|
DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapNewVal, instance_size));
|
2014-09-17 08:00:07 +02:00
|
|
|
_max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
|
2015-11-09 11:35:44 +01:00
|
|
|
_blob_count++;
|
2007-12-01 00:00:00 +00:00
|
|
|
return b->allocated_space();
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeHeap::deallocate(void* p) {
|
|
|
|
assert(p == find_start(p), "illegal deallocation");
|
|
|
|
// Find start of HeapBlock
|
|
|
|
HeapBlock* b = (((HeapBlock *)p) - 1);
|
|
|
|
assert(b->allocated_space() == p, "sanity check");
|
2014-03-07 07:42:40 +01:00
|
|
|
DEBUG_ONLY(memset((void *)b->allocated_space(), badCodeHeapFreeVal,
|
|
|
|
segments_to_size(b->length()) - sizeof(HeapBlock)));
|
2007-12-01 00:00:00 +00:00
|
|
|
add_to_freelist(b);
|
2014-03-07 07:42:40 +01:00
|
|
|
NOT_PRODUCT(verify());
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
/**
|
|
|
|
* Uses segment map to find the the start (header) of a nmethod. This works as follows:
|
|
|
|
* The memory of the code cache is divided into 'segments'. The size of a segment is
|
|
|
|
* determined by -XX:CodeCacheSegmentSize=XX. Allocation in the code cache can only
|
|
|
|
* happen at segment boundaries. A pointer in the code cache can be mapped to a segment
|
|
|
|
* by calling segment_for(addr). Each time memory is requested from the code cache,
|
|
|
|
* the segmap is updated accordingly. See the following example, which illustrates the
|
|
|
|
* state of code cache and the segment map: (seg -> segment, nm ->nmethod)
|
|
|
|
*
|
|
|
|
* code cache segmap
|
|
|
|
* ----------- ---------
|
|
|
|
* seg 1 | nm 1 | -> | 0 |
|
|
|
|
* seg 2 | nm 1 | -> | 1 |
|
|
|
|
* ... | nm 1 | -> | .. |
|
|
|
|
* seg m | nm 2 | -> | 0 |
|
|
|
|
* seg m+1 | nm 2 | -> | 1 |
|
|
|
|
* ... | nm 2 | -> | 2 |
|
|
|
|
* ... | nm 2 | -> | .. |
|
|
|
|
* ... | nm 2 | -> | 0xFE |
|
|
|
|
* seg m+n | nm 2 | -> | 1 |
|
|
|
|
* ... | nm 2 | -> | |
|
|
|
|
*
|
|
|
|
* A value of '0' in the segmap indicates that this segment contains the beginning of
|
|
|
|
* an nmethod. Let's walk through a simple example: If we want to find the start of
|
|
|
|
* an nmethod that falls into seg 2, we read the value of the segmap[2]. The value
|
|
|
|
* is an offset that points to the segment that contains the start of the nmethod.
|
|
|
|
* Another example: If we want to get the start of nm 2, and we happen to get a pointer
|
|
|
|
* that points to seg m+n, we first read seg[n+m], which returns '1'. So we have to
|
|
|
|
* do one more read of the segmap[m+n-1] to finally get the segment header.
|
|
|
|
*/
|
2007-12-01 00:00:00 +00:00
|
|
|
void* CodeHeap::find_start(void* p) const {
|
|
|
|
if (!contains(p)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-07 07:42:40 +01:00
|
|
|
size_t seg_idx = segment_for(p);
|
|
|
|
address seg_map = (address)_segmap.low();
|
|
|
|
if (is_segment_unused(seg_map[seg_idx])) {
|
2007-12-01 00:00:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-07 07:42:40 +01:00
|
|
|
while (seg_map[seg_idx] > 0) {
|
|
|
|
seg_idx -= (int)seg_map[seg_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapBlock* h = block_at(seg_idx);
|
2007-12-01 00:00:00 +00:00
|
|
|
if (h->free()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return h->allocated_space();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t CodeHeap::alignment_unit() const {
|
|
|
|
// this will be a power of two
|
|
|
|
return _segment_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t CodeHeap::alignment_offset() const {
|
|
|
|
// The lowest address in any allocated block will be
|
|
|
|
// equal to alignment_offset (mod alignment_unit).
|
|
|
|
return sizeof(HeapBlock) & (_segment_size - 1);
|
|
|
|
}
|
|
|
|
|
2015-01-12 09:55:20 +01:00
|
|
|
// Returns the current block if available and used.
|
|
|
|
// If not, it returns the subsequent block (if available), NULL otherwise.
|
|
|
|
// Free blocks are merged, therefore there is at most one free block
|
|
|
|
// between two used ones. As a result, the subsequent block (if available) is
|
|
|
|
// guaranteed to be used.
|
|
|
|
void* CodeHeap::next_used(HeapBlock* b) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (b != NULL && b->free()) b = next_block(b);
|
|
|
|
assert(b == NULL || !b->free(), "must be in use or at end of heap");
|
|
|
|
return (b == NULL) ? NULL : b->allocated_space();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the first used HeapBlock
|
|
|
|
HeapBlock* CodeHeap::first_block() const {
|
|
|
|
if (_next_segment > 0)
|
|
|
|
return block_at(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
HeapBlock* CodeHeap::block_start(void* q) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
HeapBlock* b = (HeapBlock*)find_start(q);
|
|
|
|
if (b == NULL) return NULL;
|
|
|
|
return b - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the next Heap block an offset into one
|
|
|
|
HeapBlock* CodeHeap::next_block(HeapBlock *b) const {
|
|
|
|
if (b == NULL) return NULL;
|
|
|
|
size_t i = segment_for(b) + b->length();
|
|
|
|
if (i < _next_segment)
|
|
|
|
return block_at(i);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns current capacity
|
|
|
|
size_t CodeHeap::capacity() const {
|
|
|
|
return _memory.committed_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CodeHeap::max_capacity() const {
|
|
|
|
return _memory.reserved_size();
|
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
int CodeHeap::allocated_segments() const {
|
|
|
|
return (int)_next_segment;
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
size_t CodeHeap::allocated_capacity() const {
|
2013-04-11 13:57:44 +02:00
|
|
|
// size of used heap - size on freelist
|
|
|
|
return segments_to_size(_next_segment - _freelist_segments);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 13:57:44 +02:00
|
|
|
// Returns size of the unallocated heap block
|
|
|
|
size_t CodeHeap::heap_unallocated_capacity() const {
|
|
|
|
// Total number of segments - number currently used
|
|
|
|
return segments_to_size(_number_of_reserved_segments - _next_segment);
|
2010-12-29 10:41:43 -08:00
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Free list management
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
FreeBlock* CodeHeap::following_block(FreeBlock *b) {
|
2007-12-01 00:00:00 +00:00
|
|
|
return (FreeBlock*)(((address)b) + _segment_size * b->length());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts block b after a
|
|
|
|
void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
|
|
|
|
assert(a != NULL && b != NULL, "must be real pointers");
|
|
|
|
|
|
|
|
// Link b into the list after a
|
|
|
|
b->set_link(a->link());
|
|
|
|
a->set_link(b);
|
|
|
|
|
|
|
|
// See if we can merge blocks
|
|
|
|
merge_right(b); // Try to make b bigger
|
|
|
|
merge_right(a); // Try to make a include b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to merge this block with the following block
|
2014-03-07 07:42:40 +01:00
|
|
|
bool CodeHeap::merge_right(FreeBlock* a) {
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(a->free(), "must be a free block");
|
|
|
|
if (following_block(a) == a->link()) {
|
|
|
|
assert(a->link() != NULL && a->link()->free(), "must be free too");
|
|
|
|
// Update block a to include the following block
|
|
|
|
a->set_length(a->length() + a->link()->length());
|
|
|
|
a->set_link(a->link()->link());
|
|
|
|
// Update find_start map
|
|
|
|
size_t beg = segment_for(a);
|
|
|
|
mark_segmap_as_used(beg, beg + a->length());
|
2014-03-07 07:42:40 +01:00
|
|
|
_freelist_length--;
|
|
|
|
return true;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2014-03-07 07:42:40 +01:00
|
|
|
return false;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
|
|
|
|
void CodeHeap::add_to_freelist(HeapBlock* a) {
|
2007-12-01 00:00:00 +00:00
|
|
|
FreeBlock* b = (FreeBlock*)a;
|
2014-03-07 07:42:40 +01:00
|
|
|
_freelist_length++;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(b != _freelist, "cannot be removed twice");
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Mark as free and update free space count
|
2013-04-11 13:57:44 +02:00
|
|
|
_freelist_segments += b->length();
|
2007-12-01 00:00:00 +00:00
|
|
|
b->set_free();
|
|
|
|
|
|
|
|
// First element in list?
|
|
|
|
if (_freelist == NULL) {
|
|
|
|
_freelist = b;
|
|
|
|
b->set_link(NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
// Since the freelist is ordered (smaller addresses -> larger addresses) and the
|
|
|
|
// element we want to insert into the freelist has a smaller address than the first
|
|
|
|
// element, we can simply add 'b' as the first element and we are done.
|
|
|
|
if (b < _freelist) {
|
|
|
|
// Insert first in list
|
|
|
|
b->set_link(_freelist);
|
|
|
|
_freelist = b;
|
|
|
|
merge_right(_freelist);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Scan for right place to put into list. List
|
2014-01-23 14:47:23 +01:00
|
|
|
// is sorted by increasing addresses
|
2014-03-07 07:42:40 +01:00
|
|
|
FreeBlock* prev = _freelist;
|
|
|
|
FreeBlock* cur = _freelist->link();
|
2007-12-01 00:00:00 +00:00
|
|
|
while(cur != NULL && cur < b) {
|
2014-03-07 07:42:40 +01:00
|
|
|
assert(prev < cur, "Freelist must be ordered");
|
2007-12-01 00:00:00 +00:00
|
|
|
prev = cur;
|
|
|
|
cur = cur->link();
|
|
|
|
}
|
2014-03-07 07:42:40 +01:00
|
|
|
assert((prev < b) && (cur == NULL || b < cur), "free-list must be ordered");
|
|
|
|
insert_after(prev, b);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
/**
|
|
|
|
* Search freelist for an entry on the list with the best fit.
|
|
|
|
* @return NULL, if no one was found
|
|
|
|
*/
|
2014-10-24 14:25:46 +02:00
|
|
|
FreeBlock* CodeHeap::search_freelist(size_t length) {
|
2014-03-07 07:42:40 +01:00
|
|
|
FreeBlock* found_block = NULL;
|
|
|
|
FreeBlock* found_prev = NULL;
|
|
|
|
size_t found_length = 0;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
FreeBlock* prev = NULL;
|
|
|
|
FreeBlock* cur = _freelist;
|
2013-04-11 13:57:44 +02:00
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
// Search for first block that fits
|
|
|
|
while(cur != NULL) {
|
|
|
|
if (cur->length() >= length) {
|
|
|
|
// Remember block, its previous element, and its length
|
|
|
|
found_block = cur;
|
|
|
|
found_prev = prev;
|
|
|
|
found_length = found_block->length();
|
2013-04-11 13:57:44 +02:00
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
break;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
// Next element in list
|
|
|
|
prev = cur;
|
|
|
|
cur = cur->link();
|
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
if (found_block == NULL) {
|
2007-12-01 00:00:00 +00:00
|
|
|
// None found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exact (or at least good enough) fit. Remove from list.
|
|
|
|
// Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
|
2014-03-07 07:42:40 +01:00
|
|
|
if (found_length - length < CodeCacheMinBlockLength) {
|
|
|
|
_freelist_length--;
|
|
|
|
length = found_length;
|
|
|
|
if (found_prev == NULL) {
|
|
|
|
assert(_freelist == found_block, "sanity check");
|
2007-12-01 00:00:00 +00:00
|
|
|
_freelist = _freelist->link();
|
|
|
|
} else {
|
2014-03-07 07:42:40 +01:00
|
|
|
assert((found_prev->link() == found_block), "sanity check");
|
2007-12-01 00:00:00 +00:00
|
|
|
// Unmap element
|
2014-03-07 07:42:40 +01:00
|
|
|
found_prev->set_link(found_block->link());
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Truncate block and return a pointer to the following block
|
|
|
|
// Set used bit and length on new block
|
2014-03-07 07:42:40 +01:00
|
|
|
found_block->set_length(found_length - length);
|
|
|
|
found_block = following_block(found_block);
|
|
|
|
|
|
|
|
size_t beg = segment_for(found_block);
|
2007-12-01 00:00:00 +00:00
|
|
|
mark_segmap_as_used(beg, beg + length);
|
2014-03-07 07:42:40 +01:00
|
|
|
found_block->set_length(length);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
found_block->set_used();
|
2013-04-11 13:57:44 +02:00
|
|
|
_freelist_segments -= length;
|
2014-03-07 07:42:40 +01:00
|
|
|
return found_block;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Non-product code
|
|
|
|
|
|
|
|
#ifndef PRODUCT
|
|
|
|
|
|
|
|
void CodeHeap::print() {
|
|
|
|
tty->print_cr("The Heap");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeHeap::verify() {
|
2014-03-07 07:42:40 +01:00
|
|
|
if (VerifyCodeCache) {
|
|
|
|
size_t len = 0;
|
|
|
|
int count = 0;
|
|
|
|
for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
|
|
|
|
len += b->length();
|
|
|
|
count++;
|
|
|
|
// Check if we have merged all free blocks
|
|
|
|
assert(merge_right(b) == false, "Missed merging opportunity");
|
|
|
|
}
|
|
|
|
// Verify that freelist contains the right amount of free space
|
|
|
|
assert(len == _freelist_segments, "wrong freelist");
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
for(HeapBlock* h = first_block(); h != NULL; h = next_block(h)) {
|
|
|
|
if (h->free()) count--;
|
|
|
|
}
|
|
|
|
// Verify that the freelist contains the same number of blocks
|
|
|
|
// than free blocks found on the full list.
|
|
|
|
assert(count == 0, "missing free blocks");
|
|
|
|
|
|
|
|
// Verify that the number of free blocks is not out of hand.
|
|
|
|
static int free_block_threshold = 10000;
|
|
|
|
if (count > free_block_threshold) {
|
|
|
|
warning("CodeHeap: # of free blocks > %d", free_block_threshold);
|
|
|
|
// Double the warning limit
|
|
|
|
free_block_threshold *= 2;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-07 07:42:40 +01:00
|
|
|
|
|
|
|
#endif
|