test/hotspot/gtest/memory/test_metachunk.cpp
author jwilhelm
Thu, 01 Feb 2018 02:05:35 +0100
changeset 48829 93de07994202
parent 47216 71c04702a3d5
child 49365 825f006619e5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42017
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     1
/*
44997
3f875168ce21 8179889: Fix typographic errors in copyright headers
ihse
parents: 42017
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
42017
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     4
 *
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     8
 *
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    13
 * accompanied this code).
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    14
 *
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    18
 *
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    21
 * questions.
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    22
 */
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    23
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    24
#include "precompiled.hpp"
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    25
#include "memory/allocation.hpp"
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    26
#include "memory/metachunk.hpp"
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    27
#include "unittest.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    28
#include "utilities/align.hpp"
42017
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    29
#include "utilities/copy.hpp"
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    30
#include "utilities/debug.hpp"
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    31
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    32
class MetachunkTest {
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    33
 public:
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    34
  static MetaWord* initial_top(Metachunk* metachunk) {
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    35
    return metachunk->initial_top();
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    36
  }
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    37
  static MetaWord* top(Metachunk* metachunk) {
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    38
    return metachunk->top();
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    39
  }
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    40
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    41
};
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    42
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    43
TEST(Metachunk, basic) {
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    44
  size_t size = 2 * 1024 * 1024;
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    45
  void* memory = malloc(size);
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    46
  ASSERT_TRUE(NULL != memory) << "Failed to malloc 2MB";
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    47
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    48
  Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    49
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    50
  EXPECT_EQ((MetaWord*) metachunk, metachunk->bottom());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    51
  EXPECT_EQ((uintptr_t*) metachunk + metachunk->size(), metachunk->end());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    52
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    53
  // Check sizes
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    54
  EXPECT_EQ(metachunk->size(), metachunk->word_size());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    55
  EXPECT_EQ(pointer_delta(metachunk->end(), metachunk->bottom(),
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    56
                sizeof (MetaWord*)),
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    57
            metachunk->word_size());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    58
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    59
  // Check usage
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    60
  EXPECT_EQ(metachunk->used_word_size(), metachunk->overhead());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    61
  EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(),
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    62
            metachunk->free_word_size());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    63
  EXPECT_EQ(MetachunkTest::top(metachunk), MetachunkTest::initial_top(metachunk));
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    64
  EXPECT_TRUE(metachunk->is_empty());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    65
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    66
  // Allocate
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    67
  size_t alloc_size = 64; // Words
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 44997
diff changeset
    68
  EXPECT_TRUE(is_aligned(alloc_size, Metachunk::object_alignment()));
42017
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    69
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    70
  MetaWord* mem = metachunk->allocate(alloc_size);
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    71
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    72
  // Check post alloc
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    73
  EXPECT_EQ(MetachunkTest::initial_top(metachunk), mem);
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    74
  EXPECT_EQ(MetachunkTest::top(metachunk), mem + alloc_size);
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    75
  EXPECT_EQ(metachunk->overhead() + alloc_size, metachunk->used_word_size());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    76
  EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(),
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    77
            metachunk->free_word_size());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    78
  EXPECT_FALSE(metachunk->is_empty());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    79
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    80
  // Clear chunk
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    81
  metachunk->reset_empty();
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    82
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    83
  // Check post clear
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    84
  EXPECT_EQ(metachunk->used_word_size(), metachunk->overhead());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    85
  EXPECT_EQ(metachunk->word_size() - metachunk->used_word_size(),
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    86
            metachunk->free_word_size());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    87
  EXPECT_EQ(MetachunkTest::top(metachunk), MetachunkTest::initial_top(metachunk));
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    88
  EXPECT_TRUE(metachunk->is_empty());
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    89
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    90
  free(memory);
ed85071e7d9d 8166804: Convert TestMetachunk_test to GTest
kzhaldyb
parents:
diff changeset
    91
}