1 /* 2 * Copyright (c) 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 #include "precompiled.hpp" 25 #include "memory/allocation.hpp" 26 #include "memory/metachunk.hpp" 27 #include "unittest.hpp" 28 #include "utilities/align.hpp" 29 #include "utilities/copy.hpp" 30 #include "utilities/debug.hpp" 31 32 class MetachunkTest { 33 public: 34 static MetaWord* initial_top(Metachunk* metachunk) { 35 return metachunk->initial_top(); 36 } 37 static MetaWord* top(Metachunk* metachunk) { 38 return metachunk->top(); 39 } 40 41 }; 42 43 TEST(Metachunk, basic) { 44 const ChunkIndex chunk_type = MediumIndex; 45 const bool is_class = false; 46 const size_t word_size = get_size_for_nonhumongous_chunktype(chunk_type, is_class); 47 // Allocate the chunk with correct alignment. 48 void* memory = malloc(word_size * BytesPerWord * 2); 49 ASSERT_TRUE(NULL != memory) << "Failed to malloc 2MB"; 50 51 void* p_placement = align_up(memory, word_size * BytesPerWord); 52 53 Metachunk* metachunk = ::new (p_placement) Metachunk(chunk_type, is_class, word_size, NULL); 54 55 EXPECT_EQ((MetaWord*) metachunk, metachunk->bottom()); 56 EXPECT_EQ((uintptr_t*) metachunk + metachunk->size(), metachunk->end()); 57 58 // Check sizes 59 EXPECT_EQ(metachunk->size(), metachunk->word_size()); 60 EXPECT_EQ(pointer_delta(metachunk->end(), metachunk->bottom(), 61 sizeof (MetaWord*)), 62 metachunk->word_size()); 63 64 // Check usage 65 EXPECT_EQ(metachunk->used_word_size(), metachunk->overhead()); 66 EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(), 67 metachunk->free_word_size()); 68 EXPECT_EQ(MetachunkTest::top(metachunk), MetachunkTest::initial_top(metachunk)); 69 EXPECT_TRUE(metachunk->is_empty()); 70 71 // Allocate 72 size_t alloc_size = 64; // Words 73 EXPECT_TRUE(is_aligned(alloc_size, Metachunk::object_alignment())); 74 75 MetaWord* mem = metachunk->allocate(alloc_size); 76 77 // Check post alloc 78 EXPECT_EQ(MetachunkTest::initial_top(metachunk), mem); 79 EXPECT_EQ(MetachunkTest::top(metachunk), mem + alloc_size); 80 EXPECT_EQ(metachunk->overhead() + alloc_size, metachunk->used_word_size()); 81 EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(), 82 metachunk->free_word_size()); 83 EXPECT_FALSE(metachunk->is_empty()); 84 85 // Clear chunk 86 metachunk->reset_empty(); 87 88 // Check post clear 89 EXPECT_EQ(metachunk->used_word_size(), metachunk->overhead()); 90 EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(), 91 metachunk->free_word_size()); 92 EXPECT_EQ(MetachunkTest::top(metachunk), MetachunkTest::initial_top(metachunk)); 93 EXPECT_TRUE(metachunk->is_empty()); 94 95 free(memory); 96 }