1/*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "memory/allocation.hpp"
27#include "memory/metachunk.hpp"
28#include "utilities/align.hpp"
29#include "utilities/copy.hpp"
30#include "utilities/debug.hpp"
31
32class VirtualSpaceNode;
33
34size_t Metachunk::object_alignment() {
35  // Must align pointers and sizes to 8,
36  // so that 64 bit types get correctly aligned.
37  const size_t alignment = 8;
38
39  // Make sure that the Klass alignment also agree.
40  STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes);
41
42  return alignment;
43}
44
45size_t Metachunk::overhead() {
46  return align_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
47}
48
49// Metachunk methods
50
51Metachunk::Metachunk(size_t word_size,
52                     VirtualSpaceNode* container)
53    : Metabase<Metachunk>(word_size),
54    _top(NULL),
55    _container(container)
56{
57  _top = initial_top();
58#ifdef ASSERT
59  set_is_tagged_free(false);
60  mangle(uninitMetaWordVal);
61#endif
62}
63
64MetaWord* Metachunk::allocate(size_t word_size) {
65  MetaWord* result = NULL;
66  // If available, bump the pointer to allocate.
67  if (free_word_size() >= word_size) {
68    result = _top;
69    _top = _top + word_size;
70  }
71  return result;
72}
73
74// _bottom points to the start of the chunk including the overhead.
75size_t Metachunk::used_word_size() const {
76  return pointer_delta(_top, bottom(), sizeof(MetaWord));
77}
78
79size_t Metachunk::free_word_size() const {
80  return pointer_delta(end(), _top, sizeof(MetaWord));
81}
82
83void Metachunk::print_on(outputStream* st) const {
84  st->print_cr("Metachunk:"
85               " bottom " PTR_FORMAT " top " PTR_FORMAT
86               " end " PTR_FORMAT " size " SIZE_FORMAT,
87               p2i(bottom()), p2i(_top), p2i(end()), word_size());
88  if (Verbose) {
89    st->print_cr("    used " SIZE_FORMAT " free " SIZE_FORMAT,
90                 used_word_size(), free_word_size());
91  }
92}
93
94#ifndef PRODUCT
95void Metachunk::mangle(juint word_value) {
96  // Overwrite the payload of the chunk and not the links that
97  // maintain list of chunks.
98  HeapWord* start = (HeapWord*)initial_top();
99  size_t size = word_size() - overhead();
100  Copy::fill_to_words(start, size, word_value);
101}
102#endif // PRODUCT
103
104void Metachunk::verify() {
105#ifdef ASSERT
106  // Cannot walk through the blocks unless the blocks have
107  // headers with sizes.
108  assert(bottom() <= _top &&
109         _top <= (MetaWord*)end(),
110         "Chunk has been smashed");
111#endif
112  return;
113}
114
115