1 /*
 2  * Copyright (c) 2025, 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 
25 #ifndef SHARE_OOPS_LAYOUTKIND_HPP
26 #define SHARE_OOPS_LAYOUTKIND_HPP
27 
28 #include "memory/allStatic.hpp"
29 #include "utilities/globalDefinitions.hpp"
30 
31 // LayoutKind is an enum used to indicate which layout has been used for a given value field.
32 // Each layout has its own properties and its own access protocol that is detailed below.
33 //
34 // REFERENCE : this layout uses a pointer to a heap allocated instance (no flattening).
35 //             When used, field_flags().is_flat() is false . The field can be nullable or
36 //             null-restricted, in the later case, field_flags().is_null_free_inline_type() is true.
37 //             In case of a null-restricted field, putfield  and putstatic  must perform a null-check
38 //             before writing a new value. Still for null-restricted fields, if getfield reads a null pointer
39 //             from the receiver, it means that the field was not initialized yet, and getfield must substitute
40 //             the null reference with the default value of the field's class.
41 // NON_ATOMIC_FLAT : this layout is the simplest form of flattening. Any field embedded inside the flat field
42 //             can be accessed independently. The field is null-restricted, meaning putfield must perform a
43 //             null-check before performing a field update.
44 // ATOMIC_FLAT : this flat layout is designed for atomic updates, with size and alignment that make use of
45 //             atomic instructions possible. All accesses, reads and writes, must be performed atomically.
46 //             The field is null-restricted, meaning putfield must perform a null-check before performing a
47 //             field update.
48 // NULLABLE_ATOMIC_FLAT : this is the flat layout designed for JEP 401. It is designed for atomic updates,
49 //             with size and alignment that make use of atomic instructions possible. All accesses, reads and
50 //             writes, must be performed atomically. The layout includes a null marker which indicates if the
51 //             field's value must be considered as null or not. The null marker is a byte, with the value zero
52 //             meaning the field's value is null, and a non-zero value meaning the field's value is not null.
53 //             A getfield must check the value of the null marker before returning a value. If the null marker
54 //             is zero, getfield  must return the null reference, otherwise it returns the field's value read
55 //             from the receiver. When a putfield writes a non-null value to such field, the update, including
56 //             the field's value and the null marker, must be performed in a single atomic operation. If the
57 //             source of the value is a heap allocated instance of the field's class, it is allowed to set the
58 //             null marker to non-zero in the heap allocated instance before copying the value to the receiver
59 //             (the BUFFERED layout used in heap allocated values guarantees that the space for the null marker
60 //             is included, but has no meaning for the heap allocated instance which is always non-null, and that
61 //             the whole payload is correctly aligned for atomic operations). When a putfield writes null to such
62 //             field, the null marker must be set to zero. However, if the field contains oops, those oops must be
63 //             cleared too in order to prevent memory leaks. In order to simplify such operation, value classes
64 //             supporting a NULLABLE_ATOMIC_FLAT layout have a pre-allocated reset value instance, filled with
65 //             zeros, which can be used to simply overwrite the whole flat field and reset everything (oops and
66 //             null marker). The reset value instance is needed because the VM needs an instance guaranteed to
67 //             always be filled with zeros, and the default value could have its null marker set to non-zero if
68 //             it is used as a source to update a NULLABLE_ATOMIC_FLAT field.
69 // BUFFERED: this layout is only used in heap buffered instances of a value class. It is computed to be compatible
70 //             to be compatible in size and alignment with all other flat layouts supported by the value class.
71 //
72 //
73 // IMPORTANT: The REFERENCE layout must always be associated with the numerical value zero, because the implementation
74 // of the lava.lang.invoke.MemberName class relies on this property.
75 
76 enum class LayoutKind : uint32_t {
77   REFERENCE            = 0,    // indirection to a heap allocated instance
78   BUFFERED             = 1,    // layout used in heap allocated standalone instances
79   NON_ATOMIC_FLAT      = 2,    // flat, no guarantee of atomic updates, no null marker
80   ATOMIC_FLAT          = 3,    // flat, size compatible with atomic updates, alignment requirement is equal to the size
81   NULLABLE_ATOMIC_FLAT = 4,    // flat, include a null marker, plus same properties as ATOMIC layout
82   UNKNOWN              = 5     // used for uninitialized fields of type LayoutKind
83 };
84 
85 class LayoutKindHelper : AllStatic {
86  public:
87   static bool is_flat(LayoutKind lk) {
88     return lk == LayoutKind::NON_ATOMIC_FLAT || lk == LayoutKind::ATOMIC_FLAT || lk == LayoutKind::NULLABLE_ATOMIC_FLAT;
89   }
90   static bool is_atomic_flat(LayoutKind lk) {
91     return lk == LayoutKind::ATOMIC_FLAT || lk == LayoutKind::NULLABLE_ATOMIC_FLAT;
92   }
93   static bool is_nullable_flat(LayoutKind lk) {
94     return lk == LayoutKind::NULLABLE_ATOMIC_FLAT;
95   }
96 };
97 
98 #endif // SHARE_OOPS_LAYOUTKIND_HPP