Skip to content

Commit

Permalink
Update version in the pom.xml file to 1.3-SNAPSHOT
Browse files Browse the repository at this point in the history
 * Print memory sizes in a human-readable format with `Pointer.formatBytes()`
 * Map standard `malloc()`, `calloc()`, `realloc()`, and `free()` functions (issue #136)
  • Loading branch information
saudet committed Dec 4, 2016
1 parent 7c0b440 commit a30fa53
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

* Print memory sizes in a human-readable format with `Pointer.formatBytes()`
* Map standard `malloc()`, `calloc()`, `realloc()`, and `free()` functions ([issue #136](https://github.com/bytedeco/javacpp/issues/136))

### November 29, 2016 version 1.2.7
* Fix `Loader` errors that could occur due to recent changes

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.2.7</version>
<version>1.3-SNAPSHOT</version>

<name>JavaCPP</name>
<description>The missing bridge between Java and native C++</description>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/BoolPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public BoolPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new BoolPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new BoolPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/BytePointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public BytePointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new BytePointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new BytePointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/CLongPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public CLongPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new CLongPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new CLongPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/CharPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public CharPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new CharPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new CharPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/DoublePointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public DoublePointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new DoublePointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new DoublePointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/FloatPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public FloatPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new FloatPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new FloatPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/IntPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public IntPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new IntPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new IntPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/LongPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public LongPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new LongPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new LongPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ static class DeallocatorThread extends Thread {
* by a call to {@code Thread.sleep(100)}. */
static final int maxRetries;

static String formatBytes(long bytes) {
if (bytes < 1024) {
return bytes + "";
} else if ((bytes /= 1024) < 1024) {
return bytes + "K";
} else if ((bytes /= 1024) < 1024) {
return bytes + "M";
} else if ((bytes /= 1024) < 1024) {
return bytes + "G";
} else {
return (bytes / 1024) + "T";
}
}

static long parseBytes(String string) throws NumberFormatException {
int i = 0;
while (i < string.length()) {
Expand All @@ -337,7 +351,7 @@ static long parseBytes(String string) throws NumberFormatException {
i++;
}
long size = Long.parseLong(string.substring(0, i));
switch (string.substring(i).toLowerCase()) {
switch (string.substring(i).trim().toLowerCase()) {
case "t": case "tb": size *= 1024; /* no break */
case "g": case "gb": size *= 1024; /* no break */
case "m": case "mb": size *= 1024; /* no break */
Expand Down Expand Up @@ -540,12 +554,12 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
}
if (maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes) {
deallocate();
throw new OutOfMemoryError("Cannot allocate " + DeallocatorReference.totalBytes
+ " + " + r.bytes + " bytes (> Pointer.maxBytes)");
throw new OutOfMemoryError("Too much memory has already been allocated: totalBytes = "
+ formatBytes(DeallocatorReference.totalBytes) + " + " + formatBytes(r.bytes) + " > maxBytes = " + formatBytes(maxBytes));
} else if (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes) {
deallocate();
throw new OutOfMemoryError("Physical memory usage is too high ("
+ lastPhysicalBytes + " > Pointer.maxPhysicalBytes)");
throw new OutOfMemoryError("Physical memory usage is too high: physicalBytes = "
+ formatBytes(lastPhysicalBytes) + " > maxPhysicalBytes = " + formatBytes(maxPhysicalBytes));
}
if (logger.isDebugEnabled()) {
logger.debug("Registering " + this);
Expand Down Expand Up @@ -657,6 +671,11 @@ public Buffer asBuffer() {
return asByteBuffer();
}

public static native Pointer malloc(long size);
public static native Pointer calloc(long n, long size);
public static native Pointer realloc(Pointer p, long size);
public static native void free(Pointer p);

public static native Pointer memchr(Pointer p, int ch, long size);
public static native int memcmp(Pointer p1, Pointer p2, long size);
public static native Pointer memcpy(Pointer dst, Pointer src, long size);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/PointerPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public PointerPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new PointerPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new PointerPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/ShortPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public ShortPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new ShortPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new ShortPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/bytedeco/javacpp/SizeTPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public SizeTPointer(long size) {
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("No native JavaCPP library in memory. (Has Loader.load() been called?)", e);
} catch (OutOfMemoryError e) {
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new SizeTPointer(" + size + "), "
+ "totalBytes = " + totalBytes() + ", physicalBytes = " + physicalBytes());
OutOfMemoryError e2 = new OutOfMemoryError("Cannot allocate new SizeTPointer(" + size + "): "
+ "totalBytes = " + formatBytes(totalBytes()) + ", physicalBytes = " + formatBytes(physicalBytes()));
e2.initCause(e);
throw e2;
}
Expand Down
43 changes: 36 additions & 7 deletions src/test/java/org/bytedeco/javacpp/PointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public class PointerTest {
long physicalBytes = Pointer.physicalBytes();
System.out.println(physicalBytes);
assertTrue(physicalBytes > 0);

p = Pointer.malloc(1000);
assertTrue(!p.isNull());
p = Pointer.calloc(1000, 1000);
assertTrue(!p.isNull());
p = Pointer.realloc(p, 1000);
assertTrue(!p.isNull());
Pointer.free(p);
}

@Test public void testBytePointer() {
Expand Down Expand Up @@ -134,7 +142,10 @@ public class PointerTest {
fieldReference = pointers;
new BytePointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down Expand Up @@ -210,7 +221,10 @@ public class PointerTest {
fieldReference = pointers;
new ShortPointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down Expand Up @@ -286,7 +300,10 @@ public class PointerTest {
fieldReference = pointers;
new IntPointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down Expand Up @@ -362,7 +379,10 @@ public class PointerTest {
fieldReference = pointers;
new LongPointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down Expand Up @@ -438,7 +458,10 @@ public class PointerTest {
fieldReference = pointers;
new FloatPointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down Expand Up @@ -514,7 +537,10 @@ public class PointerTest {
fieldReference = pointers;
new DoublePointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down Expand Up @@ -590,7 +616,10 @@ public class PointerTest {
fieldReference = pointers;
new CharPointer(chunkSize);
fail("OutOfMemoryError should have been thrown.");
} catch (OutOfMemoryError e) { }
} catch (OutOfMemoryError e) {
System.out.println(e);
System.out.println(e.getCause());
}
for (int j = 0; j < chunks; j++) {
pointers[j] = null;
}
Expand Down

0 comments on commit a30fa53

Please sign in to comment.