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

Unsafe.allocateMemory documentation incorrect regarding zero return value

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • core-libs
    • None
    • behavioral
    • minimal
    • The specification verbiage is updated to match existing behavior so no risk.
    • Java API
    • Implementation

      Summary

      Clarify specification of Unsafe.allocateMemory as to when the return value may be zero.

      Problem

      The specification of Unsafe.allocateMemory states

      "The resulting native pointer will never be zero"

      which is not true.

      Solution

      Revise specification of Unsafe.allocateMemory to state that zero is returned if and only if the requested size is zero.

      Specification

      --- a/src/java.base/share/classes/jdk/internal/misc/Unsafe.java
      +++ b/src/java.base/share/classes/jdk/internal/misc/Unsafe.java
      @@ -597,9 +597,10 @@ private long alignToHeapWordSize(long bytes) {
           /**
            * Allocates a new block of native memory, of the given size in bytes.  The
            * contents of the memory are uninitialized; they will generally be
      -     * garbage.  The resulting native pointer will never be zero, and will be
      -     * aligned for all value types.  Dispose of this memory by calling {@link
      -     * #freeMemory}, or resize it with {@link #reallocateMemory}.
      +     * garbage.  The resulting native pointer will be zero if and only if the
      +     * requested size is zero.  The resulting native pointer will be aligned for
      +     * all value types.   Dispose of this memory by calling {@link #freeMemory}
      +     * or resize it with {@link #reallocateMemory}.
            *
            * <em>Note:</em> It is the responsibility of the caller to make
            * sure arguments are checked before the methods are called. While
      
      --- a/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java
      +++ b/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java
      @@ -436,9 +436,10 @@ public void putAddress(long address, long x) {
           /**
            * Allocates a new block of native memory, of the given size in bytes.  The
            * contents of the memory are uninitialized; they will generally be
      -     * garbage.  The resulting native pointer will never be zero, and will be
      -     * aligned for all value types.  Dispose of this memory by calling {@link
      -     * #freeMemory}, or resize it with {@link #reallocateMemory}.
      +     * garbage.  The resulting native pointer will be zero if and only if the
      +     * requested size is zero.  The resulting native pointer will be aligned for
      +     * all value types.   Dispose of this memory by calling {@link #freeMemory}
      +     * or resize it with {@link #reallocateMemory}.
            *
            * <em>Note:</em> It is the responsibility of the caller to make
            * sure arguments are checked before the methods are called. While

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: