Prototype of MemoryRegion/Pointer/Reference

Mikael Vidstedt mikael.vidstedt at oracle.com
Fri Apr 22 22:26:08 UTC 2016


All,

I just pushed some prototyping work I've done in the area of 
Pointers/References/MemoryRegions:

http://hg.openjdk.java.net/panama/panama/jdk/rev/42bf13af7c8b

The code is the result of lots of brainstorming and exploratory coding 
to see how we can provide safe, simple and performant ways of handling 
allocation, updates, checking and life-cycle management of data both on 
and off the Java heap. Please note that this code is in no way final. I 
have taken plenty of shortcuts (esp. wrt the new LayoutType and its 
uses), but hopefully it's a step in the general direction of where we 
want to go with pointers/references. More work to come!

Some bullets touching on key new/updated types:

* MemoryRegion
** An untyped region of memory
** Responsible for performs various access checking (for example, bounds 
checking as done by BoundedMemoryRegion)

* BoundedMemoryRegion implements MemoryRegion
** Has a base pointer (Object + offset)
** Has a length (from base pointer)
** Checks bounds before reading/writing data

* LayoutType<T>
** Parameterized type (carrier type)
** Reponsible for tying together the Java carrier type and the actual 
native data layout/type

* LayoutTypeImpl<T> implements LayoutType<T>
** Has a carrier type
** Has a layout descriptor (*Note:* currently a 
j.i.n.abi.types.AbstractType, but that will change!)

* Pointer<T>, Reference<T>
** Interfaces to capture a pointer and a reference respectively
** Type parameter is Java carrier type

* BoundedPointer<T> implements Pointer<T>
** Pointer into a bounded memory region
** Has an offset (from the base of the memory region)

* ReferenceImpl<T> implements Reference<T>

Special notes:

** I have removed the special Pointer.int$ptr or Pointer.OfInt types. 
They may be added back later if needed, but hopefully ValueTypes will 
make it redundant
** Likewise, I have removed the Reference.int$ref or Reference.OfInt types
** The Pointer.addr() method now takes a privilege/capability object (an 
instance of PointerToken)
** Pointer.cast() takes a LayoutType and produces a new Pointer which 
has an alternative data layout/carrier type


Other:

With the above types/APIs I was able to reduce the use of Unsafe 
significantly. For example, Scope is now implemented more or less 
without using Unsafe, and so is UpcallHandler which used to rely 
*heavily* on Unsafe. I have to say that I'm especially happy with how 
the new code in UpcallHandler ended up - it's now instead working with 
BoundedPointer and gets all the bounds checking and sanity "for free". 
Note though that the logic in there still needs to be totally rewritten, 
so I'm not saying it's a poster child of code in Panama or code in 
general, just that it looks so much better with Pointer than it did with 
Unsafe :)

Cheers,
Mikael



More information about the panama-dev mailing list