src/share/vm/memory/metachunk.cpp

Tue, 15 Oct 2013 14:28:51 +0200

author
stefank
date
Tue, 15 Oct 2013 14:28:51 +0200
changeset 5941
bdfbb1fb19ca
parent 5007
c23dbf0e8ab7
child 5942
ec2e26e26183
permissions
-rw-r--r--

8026391: The Metachunk header wastes memory
Reviewed-by: coleenp, jmasa

     1 /*
     2  * Copyright (c) 2012, 2013, 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 class VirtualSpaceNode;
    33 const size_t metadata_chunk_initialize = 0xf7f7f7f7;
    35 size_t Metachunk::object_alignment() {
    36   return ARENA_AMALLOC_ALIGNMENT;
    37 }
    39 size_t Metachunk::overhead() {
    40   return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
    41 }
    43 // Metachunk methods
    45 Metachunk::Metachunk(size_t word_size,
    46                      VirtualSpaceNode* container)
    47     : Metabase<Metachunk>(word_size),
    48     _top(NULL),
    49     _container(container)
    50 {
    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                       data_word_size,
    59                       metadata_chunk_initialize);
    60 #endif
    61 }
    63 MetaWord* Metachunk::allocate(size_t word_size) {
    64   MetaWord* result = NULL;
    65   // If available, bump the pointer to allocate.
    66   if (free_word_size() >= word_size) {
    67     result = _top;
    68     _top = _top + word_size;
    69   }
    70   return result;
    71 }
    73 // _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 }
    78 size_t Metachunk::free_word_size() const {
    79   return pointer_delta(end(), _top, sizeof(MetaWord));
    80 }
    82 void Metachunk::print_on(outputStream* st) const {
    83   st->print_cr("Metachunk:"
    84                " bottom " PTR_FORMAT " top " PTR_FORMAT
    85                " end " PTR_FORMAT " size " SIZE_FORMAT,
    86                bottom(), _top, end(), word_size());
    87   if (Verbose) {
    88     st->print_cr("    used " SIZE_FORMAT " free " SIZE_FORMAT,
    89                  used_word_size(), free_word_size());
    90   }
    91 }
    93 #ifndef PRODUCT
    94 void Metachunk::mangle() {
    95   // Mangle the payload of the chunk and not the links that
    96   // maintain list of chunks.
    97   HeapWord* start = (HeapWord*)(bottom() + overhead());
    98   size_t size = word_size() - overhead();
    99   Copy::fill_to_words(start, size, metadata_chunk_initialize);
   100 }
   101 #endif // PRODUCT
   103 void Metachunk::verify() {
   104 #ifdef ASSERT
   105   // Cannot walk through the blocks unless the blocks have
   106   // headers with sizes.
   107   assert(bottom() <= _top &&
   108          _top <= (MetaWord*)end(),
   109          "Chunk has been smashed");
   110 #endif
   111   return;
   112 }
   114 /////////////// Unit tests ///////////////
   116 #ifndef PRODUCT
   118 class TestMetachunk {
   119  public:
   120   static void test() {
   121     size_t size = 2 * 1024 * 1024;
   122     void* memory = malloc(size);
   123     assert(memory != NULL, "Failed to malloc 2MB");
   125     Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
   127     assert(metachunk->bottom() == (MetaWord*)metachunk, "assert");
   128     assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert");
   130     // Check sizes
   131     assert(metachunk->size() == metachunk->word_size(), "assert");
   132     assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(),
   133         sizeof(MetaWord*)), "assert");
   135     // Check usage
   136     assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
   137     assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
   138     assert(metachunk->top() == metachunk->initial_top(), "assert");
   139     assert(metachunk->is_empty(), "assert");
   141     // Allocate
   142     size_t alloc_size = 64; // Words
   143     assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert");
   145     MetaWord* mem = metachunk->allocate(alloc_size);
   147     // Check post alloc
   148     assert(mem == metachunk->initial_top(), "assert");
   149     assert(mem + alloc_size == metachunk->top(), "assert");
   150     assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert");
   151     assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
   152     assert(!metachunk->is_empty(), "assert");
   154     // Clear chunk
   155     metachunk->reset_empty();
   157     // Check post clear
   158     assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
   159     assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
   160     assert(metachunk->top() == metachunk->initial_top(), "assert");
   161     assert(metachunk->is_empty(), "assert");
   163     free(memory);
   164   }
   165 };
   167 void TestMetachunk_test() {
   168   TestMetachunk::test();
   169 }
   171 #endif

mercurial