metachunk.cpp 5.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
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
 * 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.
 *
 */

#include "precompiled.hpp"
#include "memory/allocation.hpp"
#include "memory/metachunk.hpp"
#include "utilities/copy.hpp"
#include "utilities/debug.hpp"

31
class VirtualSpaceNode;
32 33 34

const size_t metadata_chunk_initialize = 0xf7f7f7f7;

35 36 37 38 39 40 41
size_t Metachunk::object_alignment() {
  return ARENA_AMALLOC_ALIGNMENT;
}

size_t Metachunk::overhead() {
  return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
}
42 43 44

// Metachunk methods

45
Metachunk::Metachunk(size_t word_size,
46 47
                     VirtualSpaceNode* container)
    : Metabase<Metachunk>(word_size),
48 49 50
    _top(NULL),
    _container(container)
{
51
  _top = initial_top();
52
#ifdef ASSERT
53
  set_is_tagged_free(false);
54
  size_t data_word_size = pointer_delta(end(),
55
                                        _top,
56
                                        sizeof(MetaWord));
57
  Copy::fill_to_words((HeapWord*)_top,
58 59
                      data_word_size,
                      metadata_chunk_initialize);
60 61 62 63 64 65 66 67 68 69 70 71 72 73
#endif
}

MetaWord* Metachunk::allocate(size_t word_size) {
  MetaWord* result = NULL;
  // If available, bump the pointer to allocate.
  if (free_word_size() >= word_size) {
    result = _top;
    _top = _top + word_size;
  }
  return result;
}

// _bottom points to the start of the chunk including the overhead.
74
size_t Metachunk::used_word_size() const {
75
  return pointer_delta(_top, bottom(), sizeof(MetaWord));
76 77
}

78
size_t Metachunk::free_word_size() const {
79
  return pointer_delta(end(), _top, sizeof(MetaWord));
80 81 82 83 84 85
}

void Metachunk::print_on(outputStream* st) const {
  st->print_cr("Metachunk:"
               " bottom " PTR_FORMAT " top " PTR_FORMAT
               " end " PTR_FORMAT " size " SIZE_FORMAT,
86
               bottom(), _top, end(), word_size());
87 88 89 90
  if (Verbose) {
    st->print_cr("    used " SIZE_FORMAT " free " SIZE_FORMAT,
                 used_word_size(), free_word_size());
  }
91 92 93 94 95 96 97
}

#ifndef PRODUCT
void Metachunk::mangle() {
  // Mangle the payload of the chunk and not the links that
  // maintain list of chunks.
  HeapWord* start = (HeapWord*)(bottom() + overhead());
98 99
  size_t size = word_size() - overhead();
  Copy::fill_to_words(start, size, metadata_chunk_initialize);
100 101 102 103 104 105 106
}
#endif // PRODUCT

void Metachunk::verify() {
#ifdef ASSERT
  // Cannot walk through the blocks unless the blocks have
  // headers with sizes.
107 108
  assert(bottom() <= _top &&
         _top <= (MetaWord*)end(),
109 110 111 112
         "Chunk has been smashed");
#endif
  return;
}
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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

/////////////// Unit tests ///////////////

#ifndef PRODUCT

class TestMetachunk {
 public:
  static void test() {
    size_t size = 2 * 1024 * 1024;
    void* memory = malloc(size);
    assert(memory != NULL, "Failed to malloc 2MB");

    Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);

    assert(metachunk->bottom() == (MetaWord*)metachunk, "assert");
    assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert");

    // Check sizes
    assert(metachunk->size() == metachunk->word_size(), "assert");
    assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(),
        sizeof(MetaWord*)), "assert");

    // Check usage
    assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
    assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
    assert(metachunk->top() == metachunk->initial_top(), "assert");
    assert(metachunk->is_empty(), "assert");

    // Allocate
    size_t alloc_size = 64; // Words
    assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert");

    MetaWord* mem = metachunk->allocate(alloc_size);

    // Check post alloc
    assert(mem == metachunk->initial_top(), "assert");
    assert(mem + alloc_size == metachunk->top(), "assert");
    assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert");
    assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
    assert(!metachunk->is_empty(), "assert");

    // Clear chunk
    metachunk->reset_empty();

    // Check post clear
    assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
    assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
    assert(metachunk->top() == metachunk->initial_top(), "assert");
    assert(metachunk->is_empty(), "assert");

    free(memory);
  }
};

void TestMetachunk_test() {
  TestMetachunk::test();
}

#endif