hotspot/src/share/vm/memory/heap.cpp
author kvn
Wed, 09 Mar 2011 09:15:16 -0800
changeset 8672 26a427ab6f32
parent 7715 12998f95a334
child 8921 14bfe81f2a9d
permissions -rw-r--r--
7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache Summary: Use largest_free_block() instead of unallocated_capacity(). Reviewed-by: iveresov, never, ysr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4493
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4493
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4493
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "memory/heap.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "runtime/os.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
size_t CodeHeap::header_size() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  return sizeof(HeapBlock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// Implementation of Heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
CodeHeap::CodeHeap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  _number_of_committed_segments = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  _number_of_reserved_segments  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  _segment_size                 = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  _log2_segment_size            = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  _next_segment                 = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  _freelist                     = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  _free_segments                = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  assert(0   <= beg && beg <  _number_of_committed_segments, "interval begin out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  assert(beg <  end && end <= _number_of_committed_segments, "interval end   out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // setup _segmap pointers for faster indexing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  address p = (address)_segmap.low() + beg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  address q = (address)_segmap.low() + end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // initialize interval
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  while (p < q) *p++ = 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  assert(0   <= beg && beg <  _number_of_committed_segments, "interval begin out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  assert(beg <  end && end <= _number_of_committed_segments, "interval end   out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  // setup _segmap pointers for faster indexing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  address p = (address)_segmap.low() + beg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  address q = (address)_segmap.low() + end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  // initialize interval
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  while (p < q) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    *p++ = i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    if (i == 0xFF) i = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
static size_t align_to_page_size(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  const size_t alignment = (size_t)os::vm_page_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  assert(is_power_of_2(alignment), "no kidding ???");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  return (size + alignment - 1) & ~(alignment - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
static size_t align_to_allocation_size(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  const size_t alignment = (size_t)os::vm_allocation_granularity();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  assert(is_power_of_2(alignment), "no kidding ???");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  return (size + alignment - 1) & ~(alignment - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
void CodeHeap::on_code_mapping(char* base, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
#ifdef LINUX
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  extern void linux_wrap_code(char* base, size_t size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  linux_wrap_code(base, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
bool CodeHeap::reserve(size_t reserved_size, size_t committed_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
                       size_t segment_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  assert(reserved_size >= committed_size, "reserved < committed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  assert(segment_size >= sizeof(FreeBlock), "segment size is too small");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  assert(is_power_of_2(segment_size), "segment_size must be a power of 2");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  _segment_size      = segment_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  _log2_segment_size = exact_log2(segment_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  // Reserve and initialize space for _memory.
252
050143a0dbfb 6642862: Code cache allocation fails with large pages after 6588638
jcoomes
parents: 1
diff changeset
   107
  const size_t page_size = os::can_execute_large_page_memory() ?
050143a0dbfb 6642862: Code cache allocation fails with large pages after 6588638
jcoomes
parents: 1
diff changeset
   108
          os::page_size_for_region(committed_size, reserved_size, 8) :
050143a0dbfb 6642862: Code cache allocation fails with large pages after 6588638
jcoomes
parents: 1
diff changeset
   109
          os::vm_page_size();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  const size_t granularity = os::vm_allocation_granularity();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  const size_t r_align = MAX2(page_size, granularity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  const size_t r_size = align_size_up(reserved_size, r_align);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  const size_t c_size = align_size_up(committed_size, page_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    MAX2(page_size, granularity);
2268
bea8be80ec88 6541756: Reduce executable C-heap
coleenp
parents: 670
diff changeset
   117
  ReservedCodeSpace rs(r_size, rs_align, rs_align > 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  os::trace_page_sizes("code heap", committed_size, reserved_size, page_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                       rs.base(), rs.size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  if (!_memory.initialize(rs, c_size)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  on_code_mapping(_memory.low(), _memory.committed_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  _number_of_committed_segments = number_of_segments(_memory.committed_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  _number_of_reserved_segments  = number_of_segments(_memory.reserved_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  // reserve space for _segmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  if (!_segmap.initialize(align_to_page_size(_number_of_reserved_segments), align_to_page_size(_number_of_committed_segments))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit  enough space for segment map");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  assert(_segmap.reserved_size()  >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  assert(_segmap.reserved_size()  >= _segmap.committed_size()     , "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  // initialize remaining instance variables
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
void CodeHeap::release() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
bool CodeHeap::expand_by(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  // expand _memory space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  if (dm > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    char* base = _memory.low() + _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    if (!_memory.expand_by(dm)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    on_code_mapping(base, dm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    size_t i = _number_of_committed_segments;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    _number_of_committed_segments = number_of_segments(_memory.committed_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    assert(_number_of_reserved_segments == number_of_segments(_memory.reserved_size()), "number of reserved segments should not change");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    // expand _segmap space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    if (ds > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      if (!_segmap.expand_by(ds)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    // initialize additional segmap entries
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    mark_segmap_as_free(i, _number_of_committed_segments);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
void CodeHeap::shrink_by(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
void CodeHeap::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  _next_segment = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  mark_segmap_as_free(0, _number_of_committed_segments);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
void* CodeHeap::allocate(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  size_t length = number_of_segments(size + sizeof(HeapBlock));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  assert(length *_segment_size >= sizeof(FreeBlock), "not enough room for FreeList");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  // First check if we can satify request from freelist
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  debug_only(verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  HeapBlock* block = search_freelist(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  debug_only(if (VerifyCodeCacheOften) verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  if (block != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    assert(block->length() >= length && block->length() < length + CodeCacheMinBlockLength, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    assert(!block->free(), "must be marked free");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    memset((void *)block->allocated_space(), badCodeHeapNewVal, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    return block->allocated_space();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  if (length < CodeCacheMinBlockLength) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    length = CodeCacheMinBlockLength;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  if (_next_segment + length <= _number_of_committed_segments) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    mark_segmap_as_used(_next_segment, _next_segment + length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    HeapBlock* b =  block_at(_next_segment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    b->initialize(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    _next_segment += length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    memset((void *)b->allocated_space(), badCodeHeapNewVal, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    return b->allocated_space();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
void CodeHeap::deallocate(void* p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  assert(p == find_start(p), "illegal deallocation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  // Find start of HeapBlock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  HeapBlock* b = (((HeapBlock *)p) - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  assert(b->allocated_space() == p, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  memset((void *)b->allocated_space(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
         badCodeHeapFreeVal,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
         size(b->length()) - sizeof(HeapBlock));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  add_to_freelist(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  debug_only(if (VerifyCodeCacheOften) verify());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
void* CodeHeap::find_start(void* p) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  if (!contains(p)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  size_t i = segment_for(p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  address b = (address)_segmap.low();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  if (b[i] == 0xFF) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  while (b[i] > 0) i -= (int)b[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  HeapBlock* h = block_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  if (h->free()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  return h->allocated_space();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
size_t CodeHeap::alignment_unit() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  // this will be a power of two
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  return _segment_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
size_t CodeHeap::alignment_offset() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  // The lowest address in any allocated block will be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  // equal to alignment_offset (mod alignment_unit).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  return sizeof(HeapBlock) & (_segment_size - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
// Finds the next free heapblock. If the current one is free, that it returned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
void* CodeHeap::next_free(HeapBlock *b) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  // Since free blocks are merged, there is max. on free block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  // between two used ones
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  if (b != NULL && b->free()) b = next_block(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  assert(b == NULL || !b->free(), "must be in use or at end of heap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  return (b == NULL) ? NULL : b->allocated_space();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// Returns the first used HeapBlock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
HeapBlock* CodeHeap::first_block() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  if (_next_segment > 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    return block_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
HeapBlock *CodeHeap::block_start(void *q) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  HeapBlock* b = (HeapBlock*)find_start(q);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  if (b == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  return b - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
// Returns the next Heap block an offset into one
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
HeapBlock* CodeHeap::next_block(HeapBlock *b) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  if (b == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  size_t i = segment_for(b) + b->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  if (i < _next_segment)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    return block_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
// Returns current capacity
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
size_t CodeHeap::capacity() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  return _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
size_t CodeHeap::max_capacity() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  return _memory.reserved_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
size_t CodeHeap::allocated_capacity() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  // Start with the committed size in _memory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  size_t l = _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  // Subtract the committed, but unused, segments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  l -= size(_number_of_committed_segments - _next_segment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  // Subtract the size of the freelist
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  l -= size(_free_segments);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  return l;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
7715
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   318
size_t CodeHeap::largest_free_block() const {
8672
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   319
  // First check unused space excluding free blocks.
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   320
  size_t free_sz = size(_free_segments);
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   321
  size_t unused  = max_capacity() - allocated_capacity() - free_sz;
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   322
  if (unused >= free_sz)
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   323
    return unused;
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   324
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   325
  // Now check largest free block.
7715
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   326
  size_t len = 0;
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   327
  for (FreeBlock* b = _freelist; b != NULL; b = b->link()) {
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   328
    if (b->length() > len)
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   329
      len = b->length();
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   330
  }
8672
26a427ab6f32 7025742: Can not use CodeCache::unallocated_capacity() with fragmented CodeCache
kvn
parents: 7715
diff changeset
   331
  return MAX2(unused, size(len));
7715
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   332
}
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   333
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
// Free list management
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
FreeBlock *CodeHeap::following_block(FreeBlock *b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  return (FreeBlock*)(((address)b) + _segment_size * b->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
// Inserts block b after a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  assert(a != NULL && b != NULL, "must be real pointers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  // Link b into the list after a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  b->set_link(a->link());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  a->set_link(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  // See if we can merge blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  merge_right(b); // Try to make b bigger
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  merge_right(a); // Try to make a include b
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
// Try to merge this block with the following block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
void CodeHeap::merge_right(FreeBlock *a) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  assert(a->free(), "must be a free block");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  if (following_block(a) == a->link()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    assert(a->link() != NULL && a->link()->free(), "must be free too");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    // Update block a to include the following block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    a->set_length(a->length() + a->link()->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    a->set_link(a->link()->link());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    // Update find_start map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    size_t beg = segment_for(a);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    mark_segmap_as_used(beg, beg + a->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
void CodeHeap::add_to_freelist(HeapBlock *a) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  FreeBlock* b = (FreeBlock*)a;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  assert(b != _freelist, "cannot be removed twice");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
  // Mark as free and update free space count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  _free_segments += b->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  b->set_free();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  // First element in list?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  if (_freelist == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    _freelist = b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    b->set_link(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  // Scan for right place to put into list. List
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  // is sorted by increasing addresseses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  FreeBlock* prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
  FreeBlock* cur  = _freelist;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
  while(cur != NULL && cur < b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
    assert(prev == NULL || prev < cur, "must be ordered");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    prev = cur;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    cur  = cur->link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  assert( (prev == NULL && b < _freelist) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
          (prev < b && (cur == NULL || b < cur)), "list must be ordered");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  if (prev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    // Insert first in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
    b->set_link(_freelist);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    _freelist = b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    merge_right(_freelist);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
    insert_after(prev, b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
// Search freelist for an entry on the list with the best fit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
// Return NULL if no one was found
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
FreeBlock* CodeHeap::search_freelist(size_t length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  FreeBlock *best_block = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  FreeBlock *best_prev  = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  size_t best_length = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  // Search for smallest block which is bigger than length
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  FreeBlock *prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  FreeBlock *cur = _freelist;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  while(cur != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    size_t l = cur->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
    if (l >= length && (best_block == NULL || best_length > l)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
      // Remember best block, its previous element, and its length
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
      best_block = cur;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
      best_prev  = prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
      best_length = best_block->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
    // Next element in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    prev = cur;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    cur  = cur->link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  if (best_block == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    // None found
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  assert((best_prev == NULL && _freelist == best_block ) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
         (best_prev != NULL && best_prev->link() == best_block), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  // Exact (or at least good enough) fit. Remove from list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  if (best_length < length + CodeCacheMinBlockLength) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    length = best_length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    if (best_prev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
      assert(_freelist == best_block, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
      _freelist = _freelist->link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      // Unmap element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
      best_prev->set_link(best_block->link());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
    // Truncate block and return a pointer to the following block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
    best_block->set_length(best_length - length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
    best_block = following_block(best_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    // Set used bit and length on new block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    size_t beg = segment_for(best_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    mark_segmap_as_used(beg, beg + length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
    best_block->set_length(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  best_block->set_used();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  _free_segments -= length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  return best_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
//----------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
void CodeHeap::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  tty->print_cr("The Heap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
void CodeHeap::verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  // Count the number of blocks on the freelist, and the amount of space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  // represented.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  int count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  size_t len = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    len += b->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
    count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  // Verify that freelist contains the right amount of free space
4493
9204129f065e 6843629: Make current hotspot build part of jdk5 control build
phh
parents: 3261
diff changeset
   485
  //  guarantee(len == _free_segments, "wrong freelist");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  // Verify that the number of free blocks is not out of hand.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  static int free_block_threshold = 10000;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  if (count > free_block_threshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
    warning("CodeHeap: # of free blocks > %d", free_block_threshold);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
    // Double the warning limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
    free_block_threshold *= 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  // Verify that the freelist contains the same number of free blocks that is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  // found on the full list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
    if (h->free()) count--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  }
4493
9204129f065e 6843629: Make current hotspot build part of jdk5 control build
phh
parents: 3261
diff changeset
   500
  //  guarantee(count == 0, "missing free blocks");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
}