< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/PaddingLayout.java

Print this page

  8  *  particular file as subject to the "Classpath" exception as provided
  9  *  by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  *  This code is distributed in the hope that it will be useful, but WITHOUT
 12  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  *  version 2 for more details (a copy is included in the LICENSE file that
 15  *  accompanied this code).
 16  *
 17  *  You should have received a copy of the GNU General Public License version
 18  *  2 along with this work; if not, write to the Free Software Foundation,
 19  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  *  or visit www.oracle.com if you need additional information or have any
 23  *  questions.
 24  *
 25  */
 26 package jdk.incubator.foreign;
 27 
 28 import java.lang.constant.Constable;
 29 import java.lang.constant.ConstantDescs;
 30 import java.lang.constant.DynamicConstantDesc;
 31 import java.util.Map;
 32 import java.util.Objects;
 33 import java.util.Optional;
 34 import java.util.OptionalLong;
 35 
 36 /**
 37  * A padding layout. A padding layout specifies the size of extra space which is typically not accessed by applications,
 38  * and is typically used for aligning member layouts around word boundaries.
 39  * <p>
 40  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 41  * class; programmers should treat instances that are
 42  * {@linkplain #equals(Object) equal} as interchangeable and should not
 43  * use instances for synchronization, or unpredictable behavior may
 44  * occur. For example, in a future release, synchronization may fail.
 45  * The {@code equals} method should be used for comparisons.
 46  *
 47  * <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null}
 48  * elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p>
 49  *
 50  * @implSpec
 51  * This class is immutable and thread-safe.
 52  */
 53 /* package-private */ final class PaddingLayout extends AbstractLayout implements MemoryLayout {
 54 
 55     PaddingLayout(long size) {
 56         this(size, 1, Map.of());
 57     }
 58 
 59     PaddingLayout(long size, long alignment, Map<String, Constable> attributes) {
 60         super(OptionalLong.of(size), alignment, attributes);
 61     }
 62 
 63     @Override
 64     public String toString() {
 65         return decorateLayoutString("x" + bitSize());
 66     }
 67 
 68     @Override
 69     public boolean equals(Object other) {
 70         if (this == other) {
 71             return true;
 72         }
 73         if (!super.equals(other)) {
 74             return false;
 75         }
 76         if (!(other instanceof PaddingLayout)) {
 77             return false;
 78         }
 79         PaddingLayout p = (PaddingLayout)other;
 80         return bitSize() == p.bitSize();
 81     }
 82 
 83     @Override
 84     public int hashCode() {
 85         return Objects.hash(super.hashCode(), bitSize());
 86     }
 87 
 88     @Override
 89     PaddingLayout dup(long alignment, Map<String, Constable> attributes) {
 90         return new PaddingLayout(bitSize(), alignment, attributes);
 91     }
 92 
 93     @Override
 94     public boolean hasNaturalAlignment() {
 95         return true;
 96     }
 97 
 98     @Override
 99     public Optional<DynamicConstantDesc<MemoryLayout>> describeConstable() {
100         return Optional.of(decorateLayoutConstant(DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE, "padding",
101                 CD_MEMORY_LAYOUT, MH_PADDING, bitSize())));
102     }
103 
104     //hack: the declarations below are to make javadoc happy; we could have used generics in AbstractLayout
105     //but that causes issues with javadoc, see JDK-8224052
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public PaddingLayout withName(String name) {
112         return (PaddingLayout)super.withName(name);
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public PaddingLayout withBitAlignment(long alignmentBits) {
120         return (PaddingLayout)super.withBitAlignment(alignmentBits);
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public PaddingLayout withAttribute(String name, Constable value) {
128         return (PaddingLayout)super.withAttribute(name, value);
129     }
130 }

  8  *  particular file as subject to the "Classpath" exception as provided
  9  *  by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  *  This code is distributed in the hope that it will be useful, but WITHOUT
 12  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  *  version 2 for more details (a copy is included in the LICENSE file that
 15  *  accompanied this code).
 16  *
 17  *  You should have received a copy of the GNU General Public License version
 18  *  2 along with this work; if not, write to the Free Software Foundation,
 19  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  *  or visit www.oracle.com if you need additional information or have any
 23  *  questions.
 24  *
 25  */
 26 package jdk.incubator.foreign;
 27 

 28 import java.lang.constant.ConstantDescs;
 29 import java.lang.constant.DynamicConstantDesc;

 30 import java.util.Objects;
 31 import java.util.Optional;
 32 import java.util.OptionalLong;
 33 
 34 /**
 35  * A padding layout. A padding layout specifies the size of extra space which is typically not accessed by applications,
 36  * and is typically used for aligning member layouts around word boundaries.
 37  * <p>
 38  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 39  * class; programmers should treat instances that are
 40  * {@linkplain #equals(Object) equal} as interchangeable and should not
 41  * use instances for synchronization, or unpredictable behavior may
 42  * occur. For example, in a future release, synchronization may fail.
 43  * The {@code equals} method should be used for comparisons.
 44  *
 45  * <p> Unless otherwise specified, passing a {@code null} argument, or an array argument containing one or more {@code null}
 46  * elements to a method in this class causes a {@link NullPointerException NullPointerException} to be thrown. </p>
 47  *
 48  * @implSpec
 49  * This class is immutable and thread-safe.
 50  */
 51 /* package-private */ final class PaddingLayout extends AbstractLayout implements MemoryLayout {
 52 
 53     PaddingLayout(long size) {
 54         this(size, 1, Optional.empty());
 55     }
 56 
 57     PaddingLayout(long size, long alignment, Optional<String> name) {
 58         super(OptionalLong.of(size), alignment, name);
 59     }
 60 
 61     @Override
 62     public String toString() {
 63         return decorateLayoutString("x" + bitSize());
 64     }
 65 
 66     @Override
 67     public boolean equals(Object other) {
 68         if (this == other) {
 69             return true;
 70         }
 71         if (!super.equals(other)) {
 72             return false;
 73         }
 74         if (!(other instanceof PaddingLayout p)) {
 75             return false;
 76         }

 77         return bitSize() == p.bitSize();
 78     }
 79 
 80     @Override
 81     public int hashCode() {
 82         return Objects.hash(super.hashCode(), bitSize());
 83     }
 84 
 85     @Override
 86     PaddingLayout dup(long alignment, Optional<String> name) {
 87         return new PaddingLayout(bitSize(), alignment, name);
 88     }
 89 
 90     @Override
 91     public boolean hasNaturalAlignment() {
 92         return true;
 93     }
 94 
 95     @Override
 96     public Optional<DynamicConstantDesc<MemoryLayout>> describeConstable() {
 97         return Optional.of(decorateLayoutConstant(DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE, "padding",
 98                 CD_MEMORY_LAYOUT, MH_PADDING, bitSize())));
 99     }
100 
101     //hack: the declarations below are to make javadoc happy; we could have used generics in AbstractLayout
102     //but that causes issues with javadoc, see JDK-8224052
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public PaddingLayout withName(String name) {
109         return (PaddingLayout)super.withName(name);
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public PaddingLayout withBitAlignment(long alignmentBits) {
117         return (PaddingLayout)super.withBitAlignment(alignmentBits);
118     }








119 }
< prev index next >