JDK 14 Foreign-Memory Access API Overview

MemoryAddress & MemorySegment

import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
public class PanamaMain
{
public static void main(String[] args)
{
MemoryAddress address = MemorySegment.allocateNative(4).baseAddress();
}
}

MemoryHandles

  • byte.class
  • short.class
  • char.class
  • int.class
  • double.class
  • long.class
import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemorySegment;
public class PanamaMain
{
public static void main(String[] args)
{
MemoryAddress address = MemorySegment.allocateNative(4).baseAddress();

VarHandle handle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());

int value = (int)handle.get(address);

System.out.println("Memory Value: " + value);
}
}
import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemorySegment;
public class PanamaMain
{
public static void main(String[] args)
{
MemoryAddress address = MemorySegment.allocateNative(4).baseAddress();

VarHandle handle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());

handle.set(address, 10);

int value = (int)handle.get(address);

System.out.println("Memory Value: " + value);
}
}

MemoryLayout & MemoryLayouts

import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemoryLayouts;
import jdk.incubator.foreign.MemorySegment;
public class PanamaMain
{
public static void main(String[] args)
{
MemoryAddress address = MemorySegment.allocateNative(MemoryLayouts.JAVA_INT).baseAddress();

VarHandle handle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());

handle.set(address, 10);

int value = (int)handle.get(address);

System.out.println("Memory Value: " + value);
}
}
  • ValueLayout
  • SequenceLayout
  • GroupLayout

Slicing & Arrays

import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemorySegment;
public class PanamaMain
{
public static void main(String[] args)
{
MemoryAddress address = MemorySegment.allocateNative(24).baseAddress();

MemoryAddress address1 = address.segment().asSlice(0, 8).baseAddress();
MemoryAddress address2 = address.segment().asSlice(8, 8).baseAddress();
MemoryAddress address3 = address.segment().asSlice(16, 8).baseAddress();

VarHandle handle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
handle.set(address1, Long.MIN_VALUE);
handle.set(address2, 0);
handle.set(address3, Long.MAX_VALUE);

long value1 = (long)handle.get(address1);
long value2 = (long)handle.get(address2);
long value3 = (long)handle.get(address3);


System.out.println("Memory Value 1: " + value1);
System.out.println("Memory Value 2: " + value2);
System.out.println("Memory Value 3: " + value3);
}
}
import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemorySegment;
public class PanamaMain
{
public static void main(String[] args)
{
MemoryAddress address = MemorySegment.allocateNative(24).baseAddress();

VarHandle handle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());

for(int i = 0; i <= 2; i++)
{
MemoryAddress slice = address.segment().asSlice(i*8, 8).baseAddress();

handle.set(slice, i*8);

System.out.println("Long slice at location " + handle.get(slice));
}
}
}
import java.lang.invoke.VarHandle;
import java.nio.ByteOrder;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryHandles;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemoryLayouts;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.SequenceLayout;
public class PanamaMain
{
public static void main(String[] args)
{
SequenceLayout layout = MemoryLayout.ofSequence(3, MemoryLayouts.JAVA_LONG);
MemoryAddress address = MemorySegment.allocateNative(layout).baseAddress();

VarHandle handle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());

for(int i = 0; i < layout.elementCount().getAsLong(); i++)
{
MemoryAddress slice = address.segment().asSlice(i*layout.elementLayout().byteSize(), layout.elementLayout().byteSize()).baseAddress();

handle.set(slice, i*layout.elementLayout().byteSize());

System.out.println("Long slice at location " + handle.get(slice));
}
}
}

What’s Not Included

  • jextract
  • Library Lookups
  • ABI specific ValueLayout
  • Runtime ABI layouts
  • FunctionDescriptor interface
  • ForeignUnsafe

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Unit Testing: Tell Your Doubles Apart

GitOps, or Why the Future Has No Dashboards

Flutter hands on: Building a News App

CS Glossary Ep.0 | Foreword

The importance of input manipulation

Ball allow indeed space.

Unraveling Service Meshes: When and why do you need them?

Getting Started with .NET Core 5 on MacOS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ty Young

Ty Young

More from Medium

CS371p Spring 2022: Winnie Chang

Things to know about Java 18 New Release

API — definition, mechanism, status code, security

CS373 Spring 2022: Tanner Dreiling, Week 8