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; use of identity-sensitive operations (including reference equality 40 * ({@code ==}), identity hash code, or synchronization) on instances of 41 * {@code PaddingLayout} may have unpredictable results and should be avoided. 42 * The {@code equals} method should be used for comparisons. 43 * 44 * @implSpec 45 * This class is immutable and thread-safe. 46 */ 47 /* package-private */ final class PaddingLayout extends AbstractLayout implements MemoryLayout { 48 49 PaddingLayout(long size) { 50 this(size, size, Optional.empty()); 51 } 52 53 PaddingLayout(long size, long alignment, Optional<String> name) { 54 super(OptionalLong.of(size), alignment, name); 55 } 56 57 @Override 58 public String toString() { 59 return decorateLayoutString("x" + bitSize()); 60 } 61 62 @Override 63 public boolean equals(Object other) { 64 if (this == other) { 65 return true; 66 } 67 if (!super.equals(other)) { 68 return false; 69 } 70 if (!(other instanceof PaddingLayout)) { 71 return false; 72 } 73 PaddingLayout p = (PaddingLayout)other; 74 return bitSize() == p.bitSize(); 75 } 76 77 @Override 78 public int hashCode() { 79 return Objects.hash(super.hashCode(), bitSize()); 80 } 81 82 @Override 83 PaddingLayout dup(long alignment, Optional<String> name) { 84 return new PaddingLayout(bitSize(), alignment, name); 85 } 86 87 @Override 88 public Optional<DynamicConstantDesc<MemoryLayout>> describeConstable() { 89 return Optional.of(DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE, "padding", 90 CD_LAYOUT, MH_PADDING, bitSize())); 91 } 92 93 //hack: the declarations below are to make javadoc happy; we could have used generics in AbstractLayout 94 //but that causes issues with javadoc, see JDK-8224052 95 96 /** 97 * {@inheritDoc} 98 */ 99 @Override 100 public PaddingLayout withName(String name) { 101 return (PaddingLayout)super.withName(name); 102 } 103 104 /** 105 * {@inheritDoc} 106 */ 107 @Override 108 public PaddingLayout withBitAlignment(long alignmentBits) { 109 return (PaddingLayout)super.withBitAlignment(alignmentBits); 110 } 111 }