Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8297053

Specify new default byte alignment for native allocators

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • core-libs

      The default byte alignment for MemorySegment.allocateNative(byteSize, scope) is 1 but the actual alignment is platform dependent and in reality controlled by `malloc()` which have different alignment defaults on different platforms.

      This means an application that *happens* to work on platform A (e.g. having a default alignment of 8 bytes) might fail on another platform B (e.g. having a default alignment of just 4 bytes).

      There might also be migration problems on any given platform so that an application works in version N but not in N+1 even though MemorySegment::allocate fulfils the specifications in both versions N and N+1.

      It is also possible to infer platform specific allocation alignment by allocating a large number of segments and observe the actual MemorySegment::address. Applications relying on such information are unstable across platforms and versions.

      One way to avoid this problem is to specify that all overloads of MemorySegment::allocateNative will always be byte aligned to Math.max(16, byteAlignment) (because 16 is the largest known value for default allocation alignment). The cost of this is zero on most platforms as they are aligned to 16 bytes by default anyhow.

            Unassigned Unassigned
            pminborg Per-Ake Minborg
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: