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.internal.foreign;
  27 
  28 import jdk.internal.access.foreign.MemoryAddressProxy;
  29 import jdk.internal.misc.Unsafe;
  30 
  31 import jdk.incubator.foreign.MemoryAddress;
  32 import jdk.incubator.foreign.MemorySegment;
  33 
  34 import java.lang.ref.Reference;
  35 import java.util.Objects;
  36 
  37 /**
  38  * This class provides an immutable implementation for the {@code MemoryAddress} interface. This class contains information
  39  * about the segment this address is associated with, as well as an offset into such segment.
  40  */
  41 public final class MemoryAddressImpl implements MemoryAddress, MemoryAddressProxy {
  42 
  43     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
  44 
  45     private final MemorySegmentImpl segment;
  46     private final long offset;
  47 
  48     public MemoryAddressImpl(MemorySegmentImpl segment, long offset) {
  49         this.segment = Objects.requireNonNull(segment);
  50         this.offset = offset;
  51     }
  52 
  53     public static void copy(MemoryAddressImpl src, MemoryAddressImpl dst, long size) {
  54         src.checkAccess(0, size, true);
  55         dst.checkAccess(0, size, false);
  56         //check disjoint
  57         long offsetSrc = src.unsafeGetOffset();
  58         long offsetDst = dst.unsafeGetOffset();
  59         Object baseSrc = src.unsafeGetBase();
  60         Object baseDst = dst.unsafeGetBase();
  61         if (baseSrc == baseDst &&
  62                 (offsetSrc - size) < offsetDst &&
  63                 (offsetDst - size) < offsetSrc) {
  64             throw new IllegalArgumentException("Attempt to perform a memory copy with overlapping source/destination address ranges");
  65         }
  66         UNSAFE.copyMemory(baseSrc, offsetSrc, baseDst, offsetDst, size);
  67     }
  68 
  69     // MemoryAddress methods
  70 
  71     @Override
  72     public long offset() {
  73         return offset;
  74     }
  75 
  76     @Override
  77     public MemorySegment segment() {
  78         return segment;
  79     }
  80 
  81     @Override
  82     public MemoryAddress offset(long bytes) {
  83         return new MemoryAddressImpl(segment, offset + bytes);
  84     }
  85 
  86     // MemoryAddressProxy methods
  87 
  88     public void checkAccess(long offset, long length, boolean readOnly) {
  89         segment.checkRange(this.offset + offset, length, !readOnly);
  90     }
  91 
  92     public long unsafeGetOffset() {
  93         return segment.min + offset;
  94     }
  95 
  96     public Object unsafeGetBase() {
  97         return segment.base();
  98     }
  99 
 100     // Object methods
 101 
 102     @Override
 103     public int hashCode() {
 104         return Objects.hash(unsafeGetBase(), unsafeGetOffset());
 105     }
 106 
 107     @Override
 108     public boolean equals(Object that) {
 109         if (that instanceof MemoryAddressImpl) {
 110             MemoryAddressImpl addr = (MemoryAddressImpl)that;
 111             return Objects.equals(unsafeGetBase(), ((MemoryAddressImpl) that).unsafeGetBase()) &&
 112                     unsafeGetOffset() == addr.unsafeGetOffset();
 113         } else {
 114             return false;
 115         }
 116     }
 117 
 118     @Override
 119     public String toString() {
 120         return "MemoryAddress{ region: " + segment + " offset=0x" + Long.toHexString(offset) + " }";
 121     }
 122 }