# HG changeset patch # User igerasim # Date 1531760842 25200 # Node ID e9b5be71583721420b8890338bd02ccb4a5b809f # Parent 9bad3472ee2c873647929d1924af43c386de3011 8207016: Avoid redundant native memory allocation in getFinalPath() Reviewed-by: alanb diff -r 9bad3472ee2c -r e9b5be715837 src/java.base/windows/native/libjava/WinNTFileSystem_md.c --- a/src/java.base/windows/native/libjava/WinNTFileSystem_md.c Mon Jul 16 08:59:39 2018 -0700 +++ b/src/java.base/windows/native/libjava/WinNTFileSystem_md.c Mon Jul 16 10:07:22 2018 -0700 @@ -36,6 +36,7 @@ #include #include #include +#include #include "jni.h" #include "io_util.h" @@ -137,28 +138,10 @@ result[5] == L'N' && result[6] == L'C'); int prefixLen = (isUnc) ? 7 : 4; - /* actual result length (includes terminator) */ - int resultLen = len - prefixLen + (isUnc ? 1 : 0) + 1; - - /* copy result without prefix into new buffer */ - WCHAR *tmp = (WCHAR*)malloc(resultLen * sizeof(WCHAR)); - if (tmp == NULL) { - JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); - len = 0; - } else { - WCHAR *p = result; - p += prefixLen; - if (isUnc) { - WCHAR *p2 = tmp; - p2[0] = L'\\'; - p2++; - wcscpy(p2, p); - } else { - wcscpy(tmp, p); - } - free(result); - result = tmp; - } + int prefixToKeep = (isUnc) ? 1 : 0; + // the amount to copy includes terminator + int amountToCopy = len - prefixLen + 1; + wmemmove(result + prefixToKeep, result + prefixLen, amountToCopy); } }