freeList.hpp 9.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * 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.
D
duke 已提交
22 23 24
 *
 */

25 26
#ifndef SHARE_VM_MEMORY_FREELIST_HPP
#define SHARE_VM_MEMORY_FREELIST_HPP
27 28 29

#include "gc_implementation/shared/allocationStats.hpp"

D
duke 已提交
30 31
class CompactibleFreeListSpace;

32
// A class for maintaining a free list of Chunk's.  The FreeList
D
duke 已提交
33 34 35
// maintains a the structure of the list (head, tail, etc.) plus
// statistics for allocations from the list.  The links between items
// are not part of FreeList.  The statistics are
36
// used to make decisions about coalescing Chunk's when they
D
duke 已提交
37 38 39 40 41 42
// are swept during collection.
//
// See the corresponding .cpp file for a description of the specifics
// for that implementation.

class Mutex;
43 44
template <class Chunk> class TreeList;
template <class Chunk> class PrintTreeCensusClosure;
D
duke 已提交
45

46
template <class Chunk>
D
duke 已提交
47 48
class FreeList VALUE_OBJ_CLASS_SPEC {
  friend class CompactibleFreeListSpace;
49
  friend class VMStructs;
50
  friend class PrintTreeCensusClosure<Chunk>;
51 52

 private:
53 54
  Chunk*        _head;          // Head of list of free chunks
  Chunk*        _tail;          // Tail of list of free chunks
55
  size_t        _size;          // Size in Heap words of each chunk
D
duke 已提交
56 57 58
  ssize_t       _count;         // Number of entries in list
  size_t        _hint;          // next larger size list with a positive surplus

59
  AllocationStats _allocation_stats; // allocation-related statistics
D
duke 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#ifdef ASSERT
  Mutex*        _protecting_lock;
#endif

  // Asserts false if the protecting lock (if any) is not held.
  void assert_proper_lock_protection_work() const PRODUCT_RETURN;
  void assert_proper_lock_protection() const {
#ifdef ASSERT
    if (_protecting_lock != NULL)
      assert_proper_lock_protection_work();
#endif
  }

  // Initialize the allocation statistics.
 protected:
76
  void init_statistics(bool split_birth = false);
D
duke 已提交
77
  void set_count(ssize_t v) { _count = v;}
78 79 80 81
  void increment_count()    {
    _count++;
  }

D
duke 已提交
82 83
  void decrement_count() {
    _count--;
84 85
    assert(_count >= 0, "Count should not be negative");
  }
D
duke 已提交
86 87 88 89 90 91

 public:
  // Constructor
  // Construct a list without any entries.
  FreeList();
  // Construct a list with "fc" as the first (and lone) entry in the list.
92
  FreeList(Chunk* fc);
D
duke 已提交
93 94 95 96 97 98 99 100 101 102 103 104

  // Reset the head, tail, hint, and count of a free list.
  void reset(size_t hint);

  // Declare the current free list to be protected by the given lock.
#ifdef ASSERT
  void set_protecting_lock(Mutex* protecting_lock) {
    _protecting_lock = protecting_lock;
  }
#endif

  // Accessors.
105
  Chunk* head() const {
D
duke 已提交
106 107 108
    assert_proper_lock_protection();
    return _head;
  }
109
  void set_head(Chunk* v) {
D
duke 已提交
110 111 112 113 114 115
    assert_proper_lock_protection();
    _head = v;
    assert(!_head || _head->size() == _size, "bad chunk size");
  }
  // Set the head of the list and set the prev field of non-null
  // values to NULL.
116
  void link_head(Chunk* v) {
D
duke 已提交
117 118 119 120 121
    assert_proper_lock_protection();
    set_head(v);
    // If this method is not used (just set the head instead),
    // this check can be avoided.
    if (v != NULL) {
122
      v->link_prev(NULL);
D
duke 已提交
123 124 125
    }
  }

126
  Chunk* tail() const {
D
duke 已提交
127 128 129
    assert_proper_lock_protection();
    return _tail;
  }
130
  void set_tail(Chunk* v) {
D
duke 已提交
131 132 133 134 135 136
    assert_proper_lock_protection();
    _tail = v;
    assert(!_tail || _tail->size() == _size, "bad chunk size");
  }
  // Set the tail of the list and set the next field of non-null
  // values to NULL.
137
  void link_tail(Chunk* v) {
D
duke 已提交
138 139 140
    assert_proper_lock_protection();
    set_tail(v);
    if (v != NULL) {
141
      v->clear_next();
D
duke 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    }
  }

  // No locking checks in read-accessors: lock-free reads (only) are benign.
  // Readers are expected to have the lock if they are doing work that
  // requires atomicity guarantees in sections of code.
  size_t size() const {
    return _size;
  }
  void set_size(size_t v) {
    assert_proper_lock_protection();
    _size = v;
  }
  ssize_t count() const {
    return _count;
  }
  size_t hint() const {
    return _hint;
  }
  void set_hint(size_t v) {
    assert_proper_lock_protection();
    assert(v == 0 || _size < v, "Bad hint"); _hint = v;
  }

  // Accessors for statistics
  AllocationStats* allocation_stats() {
    assert_proper_lock_protection();
    return &_allocation_stats;
  }

  ssize_t desired() const {
    return _allocation_stats.desired();
  }
175 176 177 178
  void set_desired(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_desired(v);
  }
D
duke 已提交
179
  void compute_desired(float inter_sweep_current,
180 181
                       float inter_sweep_estimate,
                       float intra_sweep_estimate) {
D
duke 已提交
182 183 184
    assert_proper_lock_protection();
    _allocation_stats.compute_desired(_count,
                                      inter_sweep_current,
185 186
                                      inter_sweep_estimate,
                                      intra_sweep_estimate);
D
duke 已提交
187
  }
188 189
  ssize_t coal_desired() const {
    return _allocation_stats.coal_desired();
D
duke 已提交
190
  }
191
  void set_coal_desired(ssize_t v) {
D
duke 已提交
192
    assert_proper_lock_protection();
193
    _allocation_stats.set_coal_desired(v);
D
duke 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  }

  ssize_t surplus() const {
    return _allocation_stats.surplus();
  }
  void set_surplus(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_surplus(v);
  }
  void increment_surplus() {
    assert_proper_lock_protection();
    _allocation_stats.increment_surplus();
  }
  void decrement_surplus() {
    assert_proper_lock_protection();
    _allocation_stats.decrement_surplus();
  }

212 213
  ssize_t bfr_surp() const {
    return _allocation_stats.bfr_surp();
D
duke 已提交
214
  }
215
  void set_bfr_surp(ssize_t v) {
D
duke 已提交
216
    assert_proper_lock_protection();
217
    _allocation_stats.set_bfr_surp(v);
D
duke 已提交
218
  }
219 220
  ssize_t prev_sweep() const {
    return _allocation_stats.prev_sweep();
D
duke 已提交
221
  }
222
  void set_prev_sweep(ssize_t v) {
D
duke 已提交
223
    assert_proper_lock_protection();
224
    _allocation_stats.set_prev_sweep(v);
D
duke 已提交
225
  }
226 227
  ssize_t before_sweep() const {
    return _allocation_stats.before_sweep();
D
duke 已提交
228
  }
229
  void set_before_sweep(ssize_t v) {
D
duke 已提交
230
    assert_proper_lock_protection();
231
    _allocation_stats.set_before_sweep(v);
D
duke 已提交
232 233
  }

234 235
  ssize_t coal_births() const {
    return _allocation_stats.coal_births();
D
duke 已提交
236
  }
237
  void set_coal_births(ssize_t v) {
D
duke 已提交
238
    assert_proper_lock_protection();
239
    _allocation_stats.set_coal_births(v);
D
duke 已提交
240
  }
241
  void increment_coal_births() {
D
duke 已提交
242
    assert_proper_lock_protection();
243
    _allocation_stats.increment_coal_births();
D
duke 已提交
244 245
  }

246 247
  ssize_t coal_deaths() const {
    return _allocation_stats.coal_deaths();
D
duke 已提交
248
  }
249
  void set_coal_deaths(ssize_t v) {
D
duke 已提交
250
    assert_proper_lock_protection();
251
    _allocation_stats.set_coal_deaths(v);
D
duke 已提交
252
  }
253
  void increment_coal_deaths() {
D
duke 已提交
254
    assert_proper_lock_protection();
255
    _allocation_stats.increment_coal_deaths();
D
duke 已提交
256 257
  }

258 259
  ssize_t split_births() const {
    return _allocation_stats.split_births();
D
duke 已提交
260
  }
261
  void set_split_births(ssize_t v) {
D
duke 已提交
262
    assert_proper_lock_protection();
263
    _allocation_stats.set_split_births(v);
D
duke 已提交
264
  }
265
  void increment_split_births() {
D
duke 已提交
266
    assert_proper_lock_protection();
267
    _allocation_stats.increment_split_births();
D
duke 已提交
268 269
  }

270 271
  ssize_t split_deaths() const {
    return _allocation_stats.split_deaths();
D
duke 已提交
272
  }
273
  void set_split_deaths(ssize_t v) {
D
duke 已提交
274
    assert_proper_lock_protection();
275
    _allocation_stats.set_split_deaths(v);
D
duke 已提交
276
  }
277
  void increment_split_deaths() {
D
duke 已提交
278
    assert_proper_lock_protection();
279
    _allocation_stats.increment_split_deaths();
D
duke 已提交
280 281 282
  }

  NOT_PRODUCT(
283
    // For debugging.  The "_returned_bytes" in all the lists are summed
D
duke 已提交
284 285
    // and compared with the total number of bytes swept during a
    // collection.
286 287 288 289
    size_t returned_bytes() const { return _allocation_stats.returned_bytes(); }
    void set_returned_bytes(size_t v) { _allocation_stats.set_returned_bytes(v); }
    void increment_returned_bytes_by(size_t v) {
      _allocation_stats.set_returned_bytes(_allocation_stats.returned_bytes() + v);
D
duke 已提交
290 291 292 293 294
    }
  )

  // Unlink head of list and return it.  Returns NULL if
  // the list is empty.
295
  Chunk* get_chunk_at_head();
D
duke 已提交
296 297 298

  // Remove the first "n" or "count", whichever is smaller, chunks from the
  // list, setting "fl", which is required to be empty, to point to them.
299
  void getFirstNChunksFromList(size_t n, FreeList<Chunk>* fl);
D
duke 已提交
300 301

  // Unlink this chunk from it's free list
302
  void remove_chunk(Chunk* fc);
D
duke 已提交
303 304

  // Add this chunk to this free list.
305 306
  void return_chunk_at_head(Chunk* fc);
  void return_chunk_at_tail(Chunk* fc);
D
duke 已提交
307 308 309

  // Similar to returnChunk* but also records some diagnostic
  // information.
310 311
  void return_chunk_at_head(Chunk* fc, bool record_return);
  void return_chunk_at_tail(Chunk* fc, bool record_return);
D
duke 已提交
312 313 314

  // Prepend "fl" (whose size is required to be the same as that of "this")
  // to the front of "this" list.
315
  void prepend(FreeList<Chunk>* fl);
D
duke 已提交
316 317 318

  // Verify that the chunk is in the list.
  // found.  Return NULL if "fc" is not found.
319
  bool verify_chunk_in_free_list(Chunk* fc) const;
320

321 322 323
  // Stats verification
  void verify_stats() const PRODUCT_RETURN;

324 325 326
  // Printing support
  static void print_labels_on(outputStream* st, const char* c);
  void print_on(outputStream* st, const char* c = NULL) const;
D
duke 已提交
327
};
328

329
#endif // SHARE_VM_MEMORY_FREELIST_HPP