1 /*
   2  * Copyright (c) 2016, 2018 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.internal.foreign;
  24 
  25 import jdk.internal.access.SharedSecrets;
  26 import jdk.internal.misc.Unsafe;
  27 import jdk.internal.vm.annotation.ForceInline;
  28 
  29 import java.foreign.memory.MemoryAddress;
  30 import java.foreign.memory.MemoryScope;
  31 import java.nio.ByteBuffer;
  32 import java.util.Objects;
  33 
  34 public class MemoryAddressImpl implements MemoryAddress {
  35 
  36     static Unsafe UNSAFE;
  37 
  38     public static MemoryAddress NULL_ADDR = new MemoryAddressImpl(MemoryScopeImpl.UNCHECKED, MemoryBoundInfo.NOTHING, 0);
  39 
  40     static {
  41         if (MemoryAddressImpl.class.getClassLoader() != null) {
  42             throw new IllegalStateException();
  43         }
  44         UNSAFE = Unsafe.getUnsafe();
  45     }
  46 
  47     private final MemoryBoundInfo boundInfo;
  48     private final long offset;
  49     private final MemoryScope scope;
  50 
  51     public MemoryAddressImpl(MemoryScope scope, MemoryBoundInfo boundInfo) {
  52         this(scope, boundInfo, 0);
  53     }
  54 
  55     public MemoryAddressImpl(MemoryScope scope, MemoryBoundInfo boundInfo, long offset) {
  56         this.boundInfo = Objects.requireNonNull(boundInfo);
  57         this.offset = offset;
  58         this.scope = scope;
  59     }
  60 
  61     public static MemoryAddress ofNull() {
  62         return NULL_ADDR;
  63     }
  64 
  65     public static MemoryAddress ofNative(long addr) {
  66         return ofNative(MemoryScopeImpl.UNCHECKED, addr);
  67     }
  68 
  69     public static MemoryAddress ofNative(MemoryScope scope, long addr) {
  70         return new MemoryAddressImpl(scope, MemoryBoundInfo.EVERYTHING, addr);
  71     }
  72 
  73     public static MemoryAddress ofArray(Object array) {
  74         int size = java.lang.reflect.Array.getLength(array);
  75         long base = UNSAFE.arrayBaseOffset(array.getClass());
  76         long scale = UNSAFE.arrayIndexScale(array.getClass());
  77         return new MemoryAddressImpl(MemoryScopeImpl.UNCHECKED, MemoryBoundInfo.ofHeap(array, base, size * scale));
  78     }
  79 
  80     public static void copy(MemoryAddressImpl src, MemoryAddressImpl dst, long size) {
  81         ((MemoryScopeImpl)src.scope()).checkAlive();
  82         ((MemoryScopeImpl)dst.scope()).checkAlive();
  83         src.checkAccess(0, size);
  84         dst.checkAccess(0, size);
  85         UNSAFE.copyMemory(
  86                 src.unsafeGetBase(), src.unsafeGetOffset(),
  87                 dst.unsafeGetBase(), dst.unsafeGetOffset(),
  88                 size);
  89     }
  90 
  91     public long size() {
  92         return boundInfo.length;
  93     }
  94 
  95     public long offset() {
  96         return offset;
  97     }
  98 
  99     @Override
 100     public MemoryScope scope() {
 101         return scope;
 102     }
 103 
 104     @Override
 105     @ForceInline
 106     public MemoryAddress narrow(long newSize) {
 107         return new MemoryAddressImpl(scope, boundInfo.limit(offset, offset + newSize), 0);
 108     }
 109 
 110     @Override
 111     public MemoryAddress offset(long bytes) {
 112         return new MemoryAddressImpl(scope, boundInfo, offset + bytes);
 113     }
 114 
 115     public void checkAccess(long offset, long length) {
 116         if (scope != null) {
 117             ((MemoryScopeImpl)scope).checkAlive();
 118         }
 119         boundInfo.checkRange(this.offset + offset, length);
 120     }
 121 
 122     public long unsafeGetOffset() {
 123         return boundInfo.min + offset;
 124     }
 125 
 126     public Object unsafeGetBase() {
 127         return boundInfo.base;
 128     }
 129 
 130     @Override
 131     public ByteBuffer asDirectByteBuffer(int bytes) throws IllegalAccessException {
 132         boundInfo.checkRange(offset, bytes);
 133         return SharedSecrets.getJavaNioAccess()
 134                 .newDirectByteBuffer(unsafeGetOffset(), bytes, null);
 135     }
 136 
 137     public static long addressof(MemoryAddress address) {
 138         if (address.equals(NULL_ADDR)) {
 139             return 0L;
 140         } else {
 141             MemoryAddressImpl addressImpl = (MemoryAddressImpl)address;
 142             addressImpl.checkAccess(0L, 1);
 143             if (addressImpl.unsafeGetBase() != null) {
 144                 throw new IllegalStateException("Heap address!");
 145             }
 146             return addressImpl.unsafeGetOffset();
 147         }
 148     }
 149 
 150     @Override
 151     public int hashCode() {
 152         return Objects.hash(unsafeGetBase(), unsafeGetOffset());
 153     }
 154 
 155     @Override
 156     public boolean equals(Object that) {
 157         if (that instanceof MemoryAddressImpl) {
 158             MemoryAddressImpl addr = (MemoryAddressImpl)that;
 159             return Objects.equals(unsafeGetBase(), ((MemoryAddressImpl) that).unsafeGetBase()) &&
 160                     unsafeGetOffset() == addr.unsafeGetOffset();
 161         } else {
 162             return false;
 163         }
 164     }
 165 
 166     @Override
 167     public String toString() {
 168         return "MemoryAddress{ region: " + boundInfo + " offset=0x" + Long.toHexString(offset) + " }";
 169     }
 170 }