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