freeList.hpp 9.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2001, 2010, 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 122 123 124 125
    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) {
      v->linkPrev(NULL);
    }
  }

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 141 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
    assert_proper_lock_protection();
    set_tail(v);
    if (v != NULL) {
      v->clearNext();
    }
  }

  // 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  }
  ssize_t coalDesired() const {
    return _allocation_stats.coalDesired();
  }
  void set_coalDesired(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_coalDesired(v);
  }

  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();
  }

  ssize_t bfrSurp() const {
    return _allocation_stats.bfrSurp();
  }
  void set_bfrSurp(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_bfrSurp(v);
  }
  ssize_t prevSweep() const {
    return _allocation_stats.prevSweep();
  }
  void set_prevSweep(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_prevSweep(v);
  }
  ssize_t beforeSweep() const {
    return _allocation_stats.beforeSweep();
  }
  void set_beforeSweep(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_beforeSweep(v);
  }

  ssize_t coalBirths() const {
    return _allocation_stats.coalBirths();
  }
  void set_coalBirths(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_coalBirths(v);
  }
  void increment_coalBirths() {
    assert_proper_lock_protection();
    _allocation_stats.increment_coalBirths();
  }

  ssize_t coalDeaths() const {
    return _allocation_stats.coalDeaths();
  }
  void set_coalDeaths(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_coalDeaths(v);
  }
  void increment_coalDeaths() {
    assert_proper_lock_protection();
    _allocation_stats.increment_coalDeaths();
  }

  ssize_t splitBirths() const {
    return _allocation_stats.splitBirths();
  }
  void set_splitBirths(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_splitBirths(v);
  }
  void increment_splitBirths() {
    assert_proper_lock_protection();
    _allocation_stats.increment_splitBirths();
  }

  ssize_t splitDeaths() const {
    return _allocation_stats.splitDeaths();
  }
  void set_splitDeaths(ssize_t v) {
    assert_proper_lock_protection();
    _allocation_stats.set_splitDeaths(v);
  }
  void increment_splitDeaths() {
    assert_proper_lock_protection();
    _allocation_stats.increment_splitDeaths();
  }

  NOT_PRODUCT(
    // For debugging.  The "_returnedBytes" in all the lists are summed
    // and compared with the total number of bytes swept during a
    // collection.
    size_t returnedBytes() const { return _allocation_stats.returnedBytes(); }
    void set_returnedBytes(size_t v) { _allocation_stats.set_returnedBytes(v); }
    void increment_returnedBytes_by(size_t v) {
      _allocation_stats.set_returnedBytes(_allocation_stats.returnedBytes() + v);
    }
  )

  // Unlink head of list and return it.  Returns NULL if
  // the list is empty.
295
  Chunk* getChunkAtHead();
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 removeChunk(Chunk* fc);
D
duke 已提交
303 304

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

  // Similar to returnChunk* but also records some diagnostic
  // information.
310 311
  void returnChunkAtHead(Chunk* fc, bool record_return);
  void returnChunkAtTail(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 verifyChunkInFreeLists(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