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.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 }