Currently, if os::realloc() is called with size=0, it will return NULL.
os::realloc() mimicks the C-Runtime realloc function and, like malloc, has two options of dealing with input size 0: 1) It can either return NULL or 2) return a unique non-NULL pointer which can be passed to free (os::free in this case).
os::realloc() should do (2) because:
- there are callers which do interpret a return value of NULL as an OOM scenario without checking (e.g. ReallocateHeap(), which is used for the REALLOC_C_HEAP_ARRAY macro)
- it would be consistent with the behaviour of os::malloc() in case of size=0
Note that right now the behaviour may be also inconsistent in release builds, because there we call the native ::realloc function and live with whatever way it deals with size=0. So the behaviour may differ between assert- and non-assert builds and also between platforms.
Please note that this only affects the hotspot. os::realloc is exposed to the JDK via Unsafe.reallocateMemory(), but that method explicitly handles an input size of 0 instead of relying on the return value of os::realloc().
os::realloc() mimicks the C-Runtime realloc function and, like malloc, has two options of dealing with input size 0: 1) It can either return NULL or 2) return a unique non-NULL pointer which can be passed to free (os::free in this case).
os::realloc() should do (2) because:
- there are callers which do interpret a return value of NULL as an OOM scenario without checking (e.g. ReallocateHeap(), which is used for the REALLOC_C_HEAP_ARRAY macro)
- it would be consistent with the behaviour of os::malloc() in case of size=0
Note that right now the behaviour may be also inconsistent in release builds, because there we call the native ::realloc function and live with whatever way it deals with size=0. So the behaviour may differ between assert- and non-assert builds and also between platforms.
Please note that this only affects the hotspot. os::realloc is exposed to the JDK via Unsafe.reallocateMemory(), but that method explicitly handles an input size of 0 instead of relying on the return value of os::realloc().