1 /*
  2  *  Copyright (c) 2019, 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.  Oracle designates this
  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 }