metachunk.hpp 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * 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.
 *
 * 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.
 *
 */
#ifndef SHARE_VM_MEMORY_METACHUNK_HPP
#define SHARE_VM_MEMORY_METACHUNK_HPP

//  Metachunk - Quantum of allocation from a Virtualspace
//    Metachunks are reused (when freed are put on a global freelist) and
//    have no permanent association to a SpaceManager.

//            +--------------+ <- end
//            |              |          --+       ---+
//            |              |            | free     |
//            |              |            |          |
//            |              |            |          | capacity
//            |              |            |          |
//            |              | <- top   --+          |
//            |              |           ---+        |
//            |              |              | used   |
//            |              |              |        |
//            |              |              |        |
//            +--------------+ <- bottom ---+     ---+

class Metachunk VALUE_OBJ_CLASS_SPEC {
  // link to support lists of chunks
  Metachunk* _next;
  Metachunk* _prev;

  MetaWord* _bottom;
  MetaWord* _end;
  MetaWord* _top;
  size_t _word_size;
  // Used in a guarantee() so included in the Product builds
  // even through it is only for debugging.
  bool _is_free;

  // Metachunks are allocated out of a MetadataVirtualSpace and
  // and use some of its space to describe itself (plus alignment
  // considerations).  Metadata is allocated in the rest of the chunk.
  // This size is the overhead of maintaining the Metachunk within
  // the space.
  static size_t _overhead;

  void set_bottom(MetaWord* v) { _bottom = v; }
  void set_end(MetaWord* v) { _end = v; }
  void set_top(MetaWord* v) { _top = v; }
  void set_word_size(size_t v) { _word_size = v; }
 public:
#ifdef ASSERT
  Metachunk() : _bottom(NULL), _end(NULL), _top(NULL), _is_free(false) {}
#else
  Metachunk() : _bottom(NULL), _end(NULL), _top(NULL) {}
#endif

  // Used to add a Metachunk to a list of Metachunks
  void set_next(Metachunk* v) { _next = v; assert(v != this, "Boom");}
  void set_prev(Metachunk* v) { _prev = v; assert(v != this, "Boom");}

  MetaWord* allocate(size_t word_size);
  static Metachunk* initialize(MetaWord* ptr, size_t word_size);

  // Accessors
  Metachunk* next() const { return _next; }
  Metachunk* prev() const { return _prev; }
  MetaWord* bottom() const { return _bottom; }
  MetaWord* end() const { return _end; }
  MetaWord* top() const { return _top; }
  size_t word_size() const { return _word_size; }
  size_t size() const volatile { return _word_size; }
  void set_size(size_t v) { _word_size = v; }
  bool is_free() { return _is_free; }
  void set_is_free(bool v) { _is_free = v; }
  static size_t overhead() { return _overhead; }
  void clear_next()              { set_next(NULL); }
  void link_prev(Metachunk* ptr) { set_prev(ptr); }
  uintptr_t* end()              { return ((uintptr_t*) this) + size(); }
  bool cantCoalesce() const     { return false; }
  void link_next(Metachunk* ptr) { set_next(ptr); }
  void link_after(Metachunk* ptr){
    link_next(ptr);
    if (ptr != NULL) ptr->link_prev(this);
  }

  // Reset top to bottom so chunk can be reused.
  void reset_empty() { _top = (_bottom + _overhead); }
  bool is_empty() { return _top == (_bottom + _overhead); }

  // used (has been allocated)
  // free (available for future allocations)
  // capacity (total size of chunk)
  size_t used_word_size();
  size_t free_word_size();
  size_t capacity_word_size();

  // Debug support
#ifdef ASSERT
  void* prev_addr() const { return (void*)&_prev; }
  void* next_addr() const { return (void*)&_next; }
  void* size_addr() const { return (void*)&_word_size; }
#endif
  bool verify_chunk_in_free_list(Metachunk* tc) const { return true; }
  bool verify_par_locked() { return true; }

  void assert_is_mangled() const {/* Don't check "\*/}

126
  NOT_PRODUCT(void mangle();)
127 128 129 130 131

  void print_on(outputStream* st) const;
  void verify();
};
#endif  // SHARE_VM_MEMORY_METACHUNK_HPP