1 | /* |
2 | * Copyright (c) 2018, 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 "oops/oop.inline.hpp" |
26 | #include "unittest.hpp" |
27 | #include "utilities/globalDefinitions.hpp" |
28 | |
29 | static char memory[32]; |
30 | |
31 | oop fake_object() { |
32 | return cast_to_oop(memory); |
33 | } |
34 | |
35 | template <typename T> |
36 | T* fake_field_addr() { |
37 | return reinterpret_cast<T*>(&memory[16]); |
38 | } |
39 | |
40 | TEST_VM(oopDesc, field_offset_oop) { |
41 | // Fake object |
42 | oop obj = fake_object(); |
43 | |
44 | // Fake oop field |
45 | oop* oop_field_addr = fake_field_addr<oop>(); |
46 | |
47 | // Check offset |
48 | size_t oop_offset = obj->field_offset(oop_field_addr); |
49 | ASSERT_EQ(16u, oop_offset); |
50 | } |
51 | |
52 | TEST_VM(oopDesc, field_offset_narrowOop) { |
53 | // Fake object |
54 | oop obj = fake_object(); |
55 | |
56 | // Fake narrowOop field |
57 | narrowOop* narrowOop_field_addr = fake_field_addr<narrowOop>(); |
58 | |
59 | size_t narrowOop_offset = obj->field_offset(narrowOop_field_addr); |
60 | ASSERT_EQ(16u, narrowOop_offset); |
61 | } |
62 | |
63 | TEST_VM(oopDesc, field_offset_primitive) { |
64 | // Fake object |
65 | oop obj = fake_object(); |
66 | |
67 | // Fake primitive field |
68 | char* char_field_addr = fake_field_addr<char>(); |
69 | |
70 | size_t char_offset = obj->field_offset(char_field_addr); |
71 | ASSERT_EQ(16u, char_offset); |
72 | } |
73 | |