Project Panama bekommt Memory Regions

Hey Leute,

bin gerade dabei mein aktuelles Slidedeck zu aktualisieren. Dabei habe ich mal ein Code-Example für die derzeitige Memory Region API in Project Panama gebastelt.

Mit Memory Regions soll es unnötig werden sun.misc.Unsafe::allocate zu nutzen und als Bestandteil von Project Panama wird es auch zur Interaktion mit Nicht-Java Anwendungen genutzt werden. Was interessant ist, dass ich einer nativen Region das Aussehen eines Arrays (in diesem Fall long[]) oder eines Structs verpassen kann. In Kombination mit den ValueTypes wird dann später auch Pointer<int> und Reference<int> möglich :slight_smile:

public long memory(long index) {
     // The scope where the memory region is available
     // Implements AutoClosable but `close` can be called manually as well
     try (Scope scope = new NativeScope()) {
        // Allocate the actual memory area, in this case in the style of a "long-array"
        Pointer<Long> ptr = scope.allocate(
                     NativeLibrary.createLayout(long.class), numElements);

        // Get the reference to a certain element
        Reference<Long> ref = ptr.offset(index).deref();

        // Set a value to this element through the reference
        ref.set(Long.MAX_VALUE);

         // Read the value of an element
         return ref.get();
    }
}

Die Commits dazu sind im Panama Repository zu finden: panama/panama/jdk: 42bf13af7c8b, panama/panama/jdk: b6db50e8621b, panama/panama/jdk: 7b9181011bf7, panama/panama/jdk: 443cb527b092

Na DAS sind mal gute Nachrichten. (Ich krieg’ zwar die Mail-Updates von Panama aber … das sind nicht die einzigen, und sie alle zu lesen und nachzuvollziehen ist schwierig :rolleyes: ). Mal reinschauen wie sie das absichern. „Unsafe“ ist es ja schon fast prinzipbedingt, auch wenn es nicht mehr so heißt :smiley: