test/hotspot/gtest/oops/test_oop.cpp
author valeriep
Fri, 22 Jun 2018 23:18:07 +0000
changeset 50732 f82821dc7aba
parent 50191 c87fba18f2b9
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50191
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     1
/*
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     4
 *
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     8
 *
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    13
 * accompanied this code).
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    14
 *
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    18
 *
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    21
 * questions.
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    22
 */
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    23
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    24
#include "precompiled.hpp"
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    25
#include "oops/oop.inline.hpp"
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    26
#include "unittest.hpp"
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    27
#include "utilities/globalDefinitions.hpp"
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    28
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    29
static char memory[32];
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    30
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    31
oop fake_object() {
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    32
  return cast_to_oop(memory);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    33
}
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    34
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    35
template <typename T>
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    36
T* fake_field_addr() {
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    37
  return reinterpret_cast<T*>(&memory[16]);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    38
}
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    39
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    40
TEST_VM(oopDesc, field_offset_oop) {
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    41
  // Fake object
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    42
  oop obj = fake_object();
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    43
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    44
  // Fake oop field
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    45
  oop* oop_field_addr = fake_field_addr<oop>();
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    46
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    47
  // Check offset
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    48
  size_t oop_offset = obj->field_offset(oop_field_addr);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    49
  ASSERT_EQ(16u, oop_offset);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    50
}
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    51
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    52
TEST_VM(oopDesc, field_offset_narrowOop) {
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    53
  // Fake object
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    54
  oop obj = fake_object();
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    55
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    56
  // Fake narrowOop field
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    57
  narrowOop* narrowOop_field_addr = fake_field_addr<narrowOop>();
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    58
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    59
  size_t narrowOop_offset = obj->field_offset(narrowOop_field_addr);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    60
  ASSERT_EQ(16u, narrowOop_offset);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    61
}
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    62
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    63
TEST_VM(oopDesc, field_offset_primitive) {
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    64
  // Fake object
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    65
  oop obj = fake_object();
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    66
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    67
  // Fake primitive field
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    68
  char* char_field_addr = fake_field_addr<char>();
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    69
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    70
  size_t char_offset = obj->field_offset(char_field_addr);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    71
  ASSERT_EQ(16u, char_offset);
c87fba18f2b9 8203339: Add oopDesc::field_offset()
stefank
parents:
diff changeset
    72
}