src/share/vm/memory/metachunk.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2012, 2014, 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  */
    25 #include "precompiled.hpp"
    26 #include "memory/allocation.hpp"
    27 #include "memory/metachunk.hpp"
    28 #include "utilities/copy.hpp"
    29 #include "utilities/debug.hpp"
    31 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    33 class VirtualSpaceNode;
    35 const size_t metadata_chunk_initialize = 0xf7f7f7f7;
    37 size_t Metachunk::object_alignment() {
    38   // Must align pointers and sizes to 8,
    39   // so that 64 bit types get correctly aligned.
    40   const size_t alignment = 8;
    42   // Make sure that the Klass alignment also agree.
    43   STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes);
    45   return alignment;
    46 }
    48 size_t Metachunk::overhead() {
    49   return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
    50 }
    52 // Metachunk methods
    54 Metachunk::Metachunk(size_t word_size,
    55                      VirtualSpaceNode* container)
    56     : Metabase<Metachunk>(word_size),
    57     _top(NULL),
    58     _container(container)
    59 {
    60   _top = initial_top();
    61 #ifdef ASSERT
    62   set_is_tagged_free(false);
    63   size_t data_word_size = pointer_delta(end(),
    64                                         _top,
    65                                         sizeof(MetaWord));
    66   Copy::fill_to_words((HeapWord*)_top,
    67                       data_word_size,
    68                       metadata_chunk_initialize);
    69 #endif
    70 }
    72 MetaWord* Metachunk::allocate(size_t word_size) {
    73   MetaWord* result = NULL;
    74   // If available, bump the pointer to allocate.
    75   if (free_word_size() >= word_size) {
    76     result = _top;
    77     _top = _top + word_size;
    78   }
    79   return result;
    80 }
    82 // _bottom points to the start of the chunk including the overhead.
    83 size_t Metachunk::used_word_size() const {
    84   return pointer_delta(_top, bottom(), sizeof(MetaWord));
    85 }
    87 size_t Metachunk::free_word_size() const {
    88   return pointer_delta(end(), _top, sizeof(MetaWord));
    89 }
    91 void Metachunk::print_on(outputStream* st) const {
    92   st->print_cr("Metachunk:"
    93                " bottom " PTR_FORMAT " top " PTR_FORMAT
    94                " end " PTR_FORMAT " size " SIZE_FORMAT,
    95                bottom(), _top, end(), word_size());
    96   if (Verbose) {
    97     st->print_cr("    used " SIZE_FORMAT " free " SIZE_FORMAT,
    98                  used_word_size(), free_word_size());
    99   }
   100 }
   102 #ifndef PRODUCT
   103 void Metachunk::mangle() {
   104   // Mangle the payload of the chunk and not the links that
   105   // maintain list of chunks.
   106   HeapWord* start = (HeapWord*)(bottom() + overhead());
   107   size_t size = word_size() - overhead();
   108   Copy::fill_to_words(start, size, metadata_chunk_initialize);
   109 }
   110 #endif // PRODUCT
   112 void Metachunk::verify() {
   113 #ifdef ASSERT
   114   // Cannot walk through the blocks unless the blocks have
   115   // headers with sizes.
   116   assert(bottom() <= _top &&
   117          _top <= (MetaWord*)end(),
   118          "Chunk has been smashed");
   119 #endif
   120   return;
   121 }
   123 /////////////// Unit tests ///////////////
   125 #ifndef PRODUCT
   127 class TestMetachunk {
   128  public:
   129   static void test() {
   130     size_t size = 2 * 1024 * 1024;
   131     void* memory = malloc(size);
   132     assert(memory != NULL, "Failed to malloc 2MB");
   134     Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
   136     assert(metachunk->bottom() == (MetaWord*)metachunk, "assert");
   137     assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert");
   139     // Check sizes
   140     assert(metachunk->size() == metachunk->word_size(), "assert");
   141     assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(),
   142         sizeof(MetaWord*)), "assert");
   144     // Check usage
   145     assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
   146     assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
   147     assert(metachunk->top() == metachunk->initial_top(), "assert");
   148     assert(metachunk->is_empty(), "assert");
   150     // Allocate
   151     size_t alloc_size = 64; // Words
   152     assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert");
   154     MetaWord* mem = metachunk->allocate(alloc_size);
   156     // Check post alloc
   157     assert(mem == metachunk->initial_top(), "assert");
   158     assert(mem + alloc_size == metachunk->top(), "assert");
   159     assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert");
   160     assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
   161     assert(!metachunk->is_empty(), "assert");
   163     // Clear chunk
   164     metachunk->reset_empty();
   166     // Check post clear
   167     assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
   168     assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
   169     assert(metachunk->top() == metachunk->initial_top(), "assert");
   170     assert(metachunk->is_empty(), "assert");
   172     free(memory);
   173   }
   174 };
   176 void TestMetachunk_test() {
   177   TestMetachunk::test();
   178 }
   180 #endif

mercurial