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.incubator.foreign.Addressable;
 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.incubator.foreign.ValueLayout;
 33 import jdk.internal.foreign.abi.SharedUtils;
 34 import jdk.internal.reflect.CallerSensitive;
 35 import jdk.internal.reflect.Reflection;
 36 
 37 import jdk.internal.vm.annotation.ForceInline;
 38 
 39 /**
 40  * This class provides an immutable implementation for the {@code MemoryAddress} interface. This class contains information
 41  * about the segment this address is associated with, as well as an offset into such segment.
 42  */
 43 public final class MemoryAddressImpl implements MemoryAddress, Scoped {
 44 
 45     private final long offset;
 46 
 47     public MemoryAddressImpl(long offset) {
 48         this.offset = offset;
 49     }
 50 
 51     // MemoryAddress methods
 52 
 53     @Override
 54     public MemoryAddress addOffset(long offset) {
 55         return new MemoryAddressImpl(this.offset + offset);
 56     }
 57 
 58     @Override
 59     public long toRawLongValue() {
 60         return offset;
 61     }
 62 
 63     @Override
 64     public final MemoryAddress address() {
 65         return this;
 66     }
 67 
 68     // Object methods
 69 
 70     @Override
 71     public int hashCode() {
 72         return (int) toRawLongValue();
 73     }
 74 
 75     @Override
 76     public boolean equals(Object that) {
 77         return (that instanceof MemoryAddressImpl addressImpl &&
 78             offset == addressImpl.offset);
 79     }
 80 
 81     @Override
 82     public String toString() {
 83         return "MemoryAddress{ offset=0x" + Long.toHexString(offset) + " }";
 84     }
 85 
 86     public static MemorySegment ofLongUnchecked(long value) {
 87         return ofLongUnchecked(value, Long.MAX_VALUE);
 88     }
 89 
 90     public static MemorySegment ofLongUnchecked(long value, long byteSize, ResourceScopeImpl resourceScope) {
 91         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, resourceScope);
 92     }
 93 
 94     public static MemorySegment ofLongUnchecked(long value, long byteSize) {
 95         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, ResourceScopeImpl.GLOBAL);
 96     }
 97 
 98     @Override
 99     public ResourceScope scope() {
100         return ResourceScopeImpl.GLOBAL;
101     }
102 
103     @Override
104     @CallerSensitive
105     @ForceInline
106     public String getUtf8String(long offset) {
107         Reflection.ensureNativeAccess(Reflection.getCallerClass());
108         SharedUtils.checkAddress(this);
109         return NativeMemorySegmentImpl.EVERYTHING.getUtf8String(toRawLongValue() + offset);
110     }
111 
112     @Override
113     @CallerSensitive
114     @ForceInline
115     public void setUtf8String(long offset, String str) {
116         Reflection.ensureNativeAccess(Reflection.getCallerClass());
117         SharedUtils.checkAddress(this);
118         NativeMemorySegmentImpl.EVERYTHING.setUtf8String(toRawLongValue() + offset, str);
119     }
120 
121     @Override
122     @ForceInline
123     @CallerSensitive
124     public byte get(ValueLayout.OfByte layout, long offset) {
125         Reflection.ensureNativeAccess(Reflection.getCallerClass());
126         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
127     }
128 
129     @Override
130     @ForceInline
131     @CallerSensitive
132     public void set(ValueLayout.OfByte layout, long offset, byte value) {
133         Reflection.ensureNativeAccess(Reflection.getCallerClass());
134         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
135     }
136 
137     @Override
138     @ForceInline
139     @CallerSensitive
140     public boolean get(ValueLayout.OfBoolean layout, long offset) {
141         Reflection.ensureNativeAccess(Reflection.getCallerClass());
142         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
143     }
144 
145     @Override
146     @ForceInline
147     @CallerSensitive
148     public void set(ValueLayout.OfBoolean layout, long offset, boolean value) {
149         Reflection.ensureNativeAccess(Reflection.getCallerClass());
150         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
151     }
152 
153     @Override
154     @ForceInline
155     @CallerSensitive
156     public char get(ValueLayout.OfChar layout, long offset) {
157         Reflection.ensureNativeAccess(Reflection.getCallerClass());
158         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
159     }
160 
161     @Override
162     @ForceInline
163     @CallerSensitive
164     public void set(ValueLayout.OfChar layout, long offset, char value) {
165         Reflection.ensureNativeAccess(Reflection.getCallerClass());
166         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
167     }
168 
169     @Override
170     @ForceInline
171     @CallerSensitive
172     public short get(ValueLayout.OfShort layout, long offset) {
173         Reflection.ensureNativeAccess(Reflection.getCallerClass());
174         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
175     }
176 
177     @Override
178     @ForceInline
179     @CallerSensitive
180     public void set(ValueLayout.OfShort layout, long offset, short value) {
181         Reflection.ensureNativeAccess(Reflection.getCallerClass());
182         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
183     }
184 
185     @Override
186     @ForceInline
187     @CallerSensitive
188     public int get(ValueLayout.OfInt layout, long offset) {
189         Reflection.ensureNativeAccess(Reflection.getCallerClass());
190         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
191     }
192 
193     @Override
194     @ForceInline
195     @CallerSensitive
196     public void set(ValueLayout.OfInt layout, long offset, int value) {
197         Reflection.ensureNativeAccess(Reflection.getCallerClass());
198         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
199     }
200 
201     @Override
202     @ForceInline
203     @CallerSensitive
204     public float get(ValueLayout.OfFloat layout, long offset) {
205         Reflection.ensureNativeAccess(Reflection.getCallerClass());
206         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
207     }
208 
209     @Override
210     @ForceInline
211     @CallerSensitive
212     public void set(ValueLayout.OfFloat layout, long offset, float value) {
213         Reflection.ensureNativeAccess(Reflection.getCallerClass());
214         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
215     }
216 
217     @Override
218     @ForceInline
219     @CallerSensitive
220     public long get(ValueLayout.OfLong layout, long offset) {
221         Reflection.ensureNativeAccess(Reflection.getCallerClass());
222         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
223     }
224 
225     @Override
226     @ForceInline
227     @CallerSensitive
228     public void set(ValueLayout.OfLong layout, long offset, long value) {
229         Reflection.ensureNativeAccess(Reflection.getCallerClass());
230         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
231     }
232 
233     @Override
234     @ForceInline
235     @CallerSensitive
236     public double get(ValueLayout.OfDouble layout, long offset) {
237         Reflection.ensureNativeAccess(Reflection.getCallerClass());
238         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
239     }
240 
241     @Override
242     @ForceInline
243     @CallerSensitive
244     public void set(ValueLayout.OfDouble layout, long offset, double value) {
245         Reflection.ensureNativeAccess(Reflection.getCallerClass());
246         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
247     }
248 
249     @Override
250     @ForceInline
251     @CallerSensitive
252     public MemoryAddress get(ValueLayout.OfAddress layout, long offset) {
253         Reflection.ensureNativeAccess(Reflection.getCallerClass());
254         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
255     }
256 
257     @Override
258     @ForceInline
259     @CallerSensitive
260     public void set(ValueLayout.OfAddress layout, long offset, Addressable value) {
261         Reflection.ensureNativeAccess(Reflection.getCallerClass());
262         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value.address());
263     }
264 
265     @Override
266     @ForceInline
267     @CallerSensitive
268     public char getAtIndex(ValueLayout.OfChar layout, long index) {
269         Reflection.ensureNativeAccess(Reflection.getCallerClass());
270         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
271     }
272 
273     @Override
274     @ForceInline
275     @CallerSensitive
276     public void setAtIndex(ValueLayout.OfChar layout, long index, char value) {
277         Reflection.ensureNativeAccess(Reflection.getCallerClass());
278         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
279     }
280 
281     @Override
282     @ForceInline
283     @CallerSensitive
284     public short getAtIndex(ValueLayout.OfShort layout, long index) {
285         Reflection.ensureNativeAccess(Reflection.getCallerClass());
286         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
287     }
288 
289     @Override
290     @ForceInline
291     @CallerSensitive
292     public void setAtIndex(ValueLayout.OfShort layout, long index, short value) {
293         Reflection.ensureNativeAccess(Reflection.getCallerClass());
294         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
295     }
296 
297     @Override
298     @ForceInline
299     @CallerSensitive
300     public int getAtIndex(ValueLayout.OfInt layout, long index) {
301         Reflection.ensureNativeAccess(Reflection.getCallerClass());
302         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
303     }
304 
305     @Override
306     @ForceInline
307     @CallerSensitive
308     public void setAtIndex(ValueLayout.OfInt layout, long index, int value) {
309         Reflection.ensureNativeAccess(Reflection.getCallerClass());
310         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
311     }
312 
313     @Override
314     @ForceInline
315     @CallerSensitive
316     public float getAtIndex(ValueLayout.OfFloat layout, long index) {
317         Reflection.ensureNativeAccess(Reflection.getCallerClass());
318         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
319     }
320 
321     @Override
322     @ForceInline
323     @CallerSensitive
324     public void setAtIndex(ValueLayout.OfFloat layout, long index, float value) {
325         Reflection.ensureNativeAccess(Reflection.getCallerClass());
326         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
327     }
328 
329     @Override
330     @ForceInline
331     @CallerSensitive
332     public long getAtIndex(ValueLayout.OfLong layout, long index) {
333         Reflection.ensureNativeAccess(Reflection.getCallerClass());
334         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
335     }
336 
337     @Override
338     @ForceInline
339     @CallerSensitive
340     public void setAtIndex(ValueLayout.OfLong layout, long index, long value) {
341         Reflection.ensureNativeAccess(Reflection.getCallerClass());
342         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
343     }
344 
345     @Override
346     @ForceInline
347     @CallerSensitive
348     public double getAtIndex(ValueLayout.OfDouble layout, long index) {
349         Reflection.ensureNativeAccess(Reflection.getCallerClass());
350         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
351     }
352 
353     @Override
354     @ForceInline
355     @CallerSensitive
356     public void setAtIndex(ValueLayout.OfDouble layout, long index, double value) {
357         Reflection.ensureNativeAccess(Reflection.getCallerClass());
358         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
359     }
360 
361     @Override
362     @ForceInline
363     @CallerSensitive
364     public MemoryAddress getAtIndex(ValueLayout.OfAddress layout, long index) {
365         Reflection.ensureNativeAccess(Reflection.getCallerClass());
366         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
367     }
368 
369     @Override
370     @ForceInline
371     @CallerSensitive
372     public void setAtIndex(ValueLayout.OfAddress layout, long index, Addressable value) {
373         Reflection.ensureNativeAccess(Reflection.getCallerClass());
374         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value.address());
375     }
376 }