Aligned long views over byte arrays and heap ByteBuffers

Paul Sandoz paul.sandoz at
Fri Jan 15 12:47:12 UTC 2016

> On 15 Jan 2016, at 12:34, MacGregor, Duncan (GE Energy Management) <duncan.macgregor at> wrote:
> Can we guarantee that even direct byte buffers are aligned?

No, the address is not always guaranteed to be aligned, for example:

  // Address not aligned
  ByteBuffer bb = ByteBuffer.allocateDirect(…).position(1).slice();

For the JDK implementation when a new DDB is allocated the address is guaranteed to be aligned (see Unsafe.allocateMemory), and the implementation takes advantage of that when viewing a DBB as a LongBuffer.

The VH aligned view implementations perform a runtime alignment check and throw an exception on failure.


> While we can
> control the creation of them by the JVM itself is it reasonable to force
> users of JNI to ensure that the address passed into NewDirectByteBuffer
> meets the VM¹s alignment rules?
> There¹s probably also some hideous interaction with views onto an array
> byte buffer and how the aligned access should interact with those.
> Duncan.

More information about the valhalla-dev mailing list