1 /* 2 * Copyright (c) 2021, 2022, 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 24 package org.openjdk.bench.java.lang.foreign.pointers; 25 26 27 import java.lang.foreign.MemorySegment; 28 import java.lang.foreign.SegmentAllocator; 29 30 public class Pointer<X> { 31 32 final MemorySegment segment; 33 34 Pointer(MemorySegment segment) { 35 this.segment = segment; 36 } 37 38 public <Z extends NativeType.OfInt<X>> int get(Z type, long index) { 39 return segment.getAtIndex(type.layout(), index); 40 } 41 42 public <Z extends NativeType.OfDouble<X>> double get(Z type, long index) { 43 return segment.getAtIndex(type.layout(), index); 44 } 45 46 public <Z extends NativeType.OfStruct<X>> X get(Z type, long index) { 47 return type.make(addOffset(index * type.layout().byteSize())); 48 } 49 50 public Pointer<X> addOffset(long offset) { 51 return new Pointer<>(segment.asSlice(offset)); 52 } 53 54 @SuppressWarnings("unchecked") 55 public <Z extends NativeType.OfPointer<X>> X get(Z type, long index) { 56 MemorySegment address = segment.getAtIndex(type.layout(), index); 57 return (X)new Pointer<>(address); 58 } 59 60 @SuppressWarnings("unchecked") 61 public X get(NativeType<X> type, long offset) { 62 if (type instanceof NativeType.OfInt intType) { 63 return (X) (Object) get(intType, offset); 64 } else if (type instanceof NativeType.OfDouble doubleType) { 65 return (X) (Object) get(doubleType, offset); 66 } else { 67 throw new UnsupportedOperationException(); 68 } 69 } 70 71 public MemorySegment segment() { 72 return segment; 73 } 74 75 public static <X> Pointer<X> allocate(NativeType<X> type, SegmentAllocator allocator) { 76 MemorySegment segment = allocator.allocate(type.layout()); 77 return new Pointer<>(segment); 78 } 79 80 public static <X> Pointer<X> allocate(NativeType<X> type, long size, SegmentAllocator allocator) { 81 MemorySegment segment = allocator.allocate(type.layout(), size); 82 return new Pointer<>(segment); 83 } 84 85 public static <X> Pointer<X> wrap(NativeType<X> type, MemorySegment segment) { 86 return new Pointer<>(segment); 87 } 88 }