Skip to content
Permalink
Browse files
More Exceptions cleanup.
  • Loading branch information
leerho committed Jun 7, 2021
1 parent 7421e27 commit 8eb133eff4a715f26ac2761278ca80842437f603
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 44 deletions.
@@ -129,37 +129,32 @@ static boolean isPageAligned() {
// -XX:MaxDirectMemorySize limits the total capacity rather than the
// actual memory usage, which will differ when buffers are page aligned.
static void reserveMemory(final long allocationSize, final long capacity) {
try {
reserveUnreserve(allocationSize, capacity, NIO_BITS_RESERVE_MEMORY_METHOD);
} catch (final Exception e) {
throw new RuntimeException("Could not invoke java.nio.Bits.reserveMemory(...): "
+ "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
}
reserveUnreserve(allocationSize, capacity, NIO_BITS_RESERVE_MEMORY_METHOD);
}

static void unreserveMemory(final long allocationSize, final long capacity) {
try {
reserveUnreserve(allocationSize, capacity, NIO_BITS_UNRESERVE_MEMORY_METHOD);
} catch (final Exception e) {
throw new RuntimeException("Could not invoke java.nio.Bits.unreserveMemory(...): "
+ "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
}
reserveUnreserve(allocationSize, capacity, NIO_BITS_UNRESERVE_MEMORY_METHOD);
}

private static void reserveUnreserve(long allocationSize, long capacity, final Method method)
throws Exception {
private static void reserveUnreserve(long allocationSize, long capacity, final Method method) {
Util.zeroCheck(capacity, "capacity");
// 1GB is a pretty "safe" limit.
final long chunkSizeLimit = 1L << 30;
while (capacity > 0) {
final long chunk = Math.min(capacity, chunkSizeLimit);
if (capacity == chunk) {
method.invoke(null, allocationSize, (int) capacity); //JDK 16 remove cast to int
} else {
method.invoke(null, chunk, (int) chunk); //JDK 16 remove cast to int
try {
while (capacity > 0) {
final long chunk = Math.min(capacity, chunkSizeLimit);
if (capacity == chunk) {
method.invoke(null, allocationSize, (int) capacity); //JDK 16 remove cast to int
} else {
method.invoke(null, chunk, (int) chunk); //JDK 16 remove cast to int
}
capacity -= chunk;
allocationSize -= chunk;
}
capacity -= chunk;
allocationSize -= chunk;
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(
"Could not invoke java.nio.Bits.unreserveMemory(...) OR java.nio.Bits.reserveMemory(...): "
+ "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
}
}
}
@@ -136,10 +136,10 @@ public static int[] parseJavaVersion(final String jdkVer) {
parts = parts[0].split("\\."); //split out the number groups
p0 = Integer.parseInt(parts[0]); //the first number group
p1 = (parts.length > 1) ? Integer.parseInt(parts[1]) : 0; //2nd number group, or 0
} catch (final NumberFormatException e) {
throw new RuntimeException("Improper Java -version string: " + jdkVer + "\n" + e);
} catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Improper Java -version string: " + jdkVer + "\n" + e);
}
//checkJavaVersion(jdkVer, p0, p1);
//checkJavaVersion(jdkVer, p0, p1); //TODO Optional to omit this.
return new int[] {p0, p1};
}

@@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteOrder;

@@ -63,7 +64,9 @@ public class AllocateDirectWritableMapMemoryTest {
private static boolean isFileReadOnly(final File file) {
try {
return (boolean) IS_FILE_READ_ONLY.invoke(null, file);
} catch (Exception e) { throw new RuntimeException(e); }
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

@BeforeClass
@@ -322,33 +322,25 @@ public void checkWRegionsReverseBO() {
}

@Test(expectedExceptions = AssertionError.class)
public void checkParentUseAfterFree() {
public void checkParentUseAfterFree() throws Exception {
int bytes = 64 * 8;
@SuppressWarnings("resource") //intentionally not using try-with-resouces here
WritableHandle wh = WritableMemory.allocateDirect(bytes);
WritableMemory wmem = wh.getWritable();
try {
wh.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
wh.close();
//with -ea assert: Memory not valid.
//with -da sometimes segfaults, sometimes passes!
wmem.getLong(0);
}

@Test(expectedExceptions = AssertionError.class)
public void checkRegionUseAfterFree() {
public void checkRegionUseAfterFree() throws Exception {
int bytes = 64;
@SuppressWarnings("resource") //intentionally not using try-with-resouces here
WritableHandle wh = WritableMemory.allocateDirect(bytes);
Memory wmem = wh.get();
Memory region = wmem.region(0L, bytes);
try {
wh.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
wh.close();
//with -ea assert: Memory not valid.
//with -da sometimes segfaults, sometimes passes!
region.getByte(0);
@@ -58,7 +58,7 @@ public void checkJdkString() {
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
fail();
} catch (Exception e) {
} catch (IllegalArgumentException e) {
println("" + e);
}

@@ -67,31 +67,31 @@ public void checkJdkString() {
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
fail();
} catch (Exception e) {
} catch (IllegalArgumentException e) {
println("" + e);
}
try {
jdkVer = "1.6.0_65"; //valid string but < 1.7
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
fail();
} catch (Exception e) {
} catch (IllegalArgumentException e) {
println("" + e);
}
try {
jdkVer = "b"; //invalid string
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
fail();
} catch (Exception e) {
} catch (IllegalArgumentException e) {
println("" + e);
}
try {
jdkVer = ""; //invalid string
p = UnsafeUtil.parseJavaVersion(jdkVer);
UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
fail();
} catch (Exception e) {
} catch (IllegalArgumentException e) {
println("" + e);
}
}
@@ -212,7 +212,7 @@ under the License.
<module name="JavadocTagContinuationIndentation">
<property name="severity" value="ignore"/>
<property name="offset" value="0"/>
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
<!-- <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/> -->
</module>

<module name="NonEmptyAtclauseDescription"/>

0 comments on commit 8eb133e

Please sign in to comment.