Skip to content

Commit d4149e0

Browse files
committed
Rollback robustness driven changes
1 parent 0868fd0 commit d4149e0

File tree

3 files changed

+51
-75
lines changed

3 files changed

+51
-75
lines changed

src/main/java/org/bytedeco/javacpp/Pointer.java

+13-37
Original file line numberDiff line numberDiff line change
@@ -285,36 +285,12 @@ static class DeallocatorReference extends PhantomReference<Pointer> implements D
285285
volatile DeallocatorReference prev = null, next = null;
286286
Deallocator deallocator;
287287

288-
private static long totalBytes = 0;
289-
private static long totalCount = 0;
288+
static volatile long totalBytes = 0;
289+
static AtomicLong totalCount = new AtomicLong();
290290
long bytes;
291291

292292
AtomicInteger count;
293293

294-
public static long getTotalBytes() {
295-
synchronized (DeallocatorReference.class) {
296-
return DeallocatorReference.totalBytes;
297-
}
298-
}
299-
300-
static void incrementTotalBytes(long increment) {
301-
synchronized (DeallocatorReference.class) {
302-
DeallocatorReference.totalBytes += increment;
303-
}
304-
}
305-
306-
static void decrementTotalBytes(long decrement) {
307-
synchronized (DeallocatorReference.class) {
308-
DeallocatorReference.totalBytes -= decrement;
309-
}
310-
}
311-
312-
public static long getTotalCount() {
313-
synchronized (DeallocatorReference.class) {
314-
return DeallocatorReference.totalCount;
315-
}
316-
}
317-
318294
final void add() {
319295
synchronized (DeallocatorReference.class) {
320296
if (head == null) {
@@ -323,8 +299,8 @@ final void add() {
323299
next = head;
324300
next.prev = head = this;
325301
}
326-
incrementTotalBytes(bytes);
327-
totalCount++;
302+
totalBytes += bytes;
303+
totalCount.incrementAndGet();
328304
}
329305
}
330306

@@ -342,8 +318,8 @@ final void remove() {
342318
next.prev = prev;
343319
}
344320
prev = next = this;
345-
decrementTotalBytes(bytes);
346-
totalCount--;
321+
totalBytes -= bytes;
322+
totalCount.decrementAndGet();
347323
}
348324
}
349325

@@ -555,14 +531,14 @@ public static long maxBytes() {
555531
return maxBytes;
556532
}
557533

558-
/** Returns {@link DeallocatorReference#getTotalBytes()}, current amount of memory tracked by deallocators. */
534+
/** Returns {@link DeallocatorReference#totalBytes}, current amount of memory tracked by deallocators. */
559535
public static long totalBytes() {
560-
return DeallocatorReference.getTotalBytes();
536+
return DeallocatorReference.totalBytes;
561537
}
562538

563-
/** Returns {@link DeallocatorReference#getTotalCount()}, current number of pointers tracked by deallocators. */
539+
/** Returns {@link DeallocatorReference#totalCount}, current number of pointers tracked by deallocators. */
564540
public static long totalCount() {
565-
return DeallocatorReference.getTotalCount();
541+
return DeallocatorReference.totalCount.get();
566542
}
567543

568544
/** Returns {@link #maxPhysicalBytes}, the maximum amount of physical memory that should be used. */
@@ -690,7 +666,7 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
690666
long lastPhysicalBytes = maxPhysicalBytes > 0 ? physicalBytes() : 0;
691667
synchronized (DeallocatorThread.class) {
692668
try {
693-
while (count++ < maxRetries && ((maxBytes > 0 && DeallocatorReference.getTotalBytes() + r.bytes > maxBytes)
669+
while (count++ < maxRetries && ((maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes)
694670
|| (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes))) {
695671
if (logger.isDebugEnabled()) {
696672
logger.debug("Calling System.gc() and Pointer.trimMemory() in " + this);
@@ -710,10 +686,10 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
710686
logger.debug(e.getMessage());
711687
}
712688
}
713-
if (maxBytes > 0 && DeallocatorReference.getTotalBytes() + r.bytes > maxBytes) {
689+
if (maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes) {
714690
deallocate();
715691
throw new OutOfMemoryError("Failed to allocate memory within limits: totalBytes ("
716-
+ formatBytes(DeallocatorReference.getTotalBytes()) + " + " + formatBytes(r.bytes) + ") > maxBytes (" + formatBytes(maxBytes) + ")");
692+
+ formatBytes(DeallocatorReference.totalBytes) + " + " + formatBytes(r.bytes) + ") > maxBytes (" + formatBytes(maxBytes) + ")");
717693
} else if (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes) {
718694
deallocate();
719695
throw new OutOfMemoryError("Physical memory usage is too high: physicalBytes ("

src/test/java/org/bytedeco/javacpp/IndexerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public class IndexerTest {
7575
Loader.load(c);
7676

7777
// work around OutOfMemoryError when testing long indexing
78-
Pointer.DeallocatorReference.decrementTotalBytes(1L << 48);
78+
Pointer.DeallocatorReference.totalBytes -= 1L << 48;
7979
}
8080

8181
@AfterClass public static void tearDownClass() throws Exception {
82-
Pointer.DeallocatorReference.incrementTotalBytes(1L << 48);
82+
Pointer.DeallocatorReference.totalBytes += 1L << 48;
8383
}
8484

8585
static class TestIndexer extends Indexer {

src/test/java/org/bytedeco/javacpp/PointerTest.java

+36-36
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static class TestFunction extends FunctionPointer {
233233
for (int j = 0; j < chunks - 1; j++) {
234234
pointers[j] = new BytePointer(chunkSize);
235235
}
236-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * byteSize);
236+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * byteSize);
237237
try {
238238
fieldReference = pointers;
239239
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -249,9 +249,9 @@ static class TestFunction extends FunctionPointer {
249249
// make sure garbage collection runs
250250
fieldReference = null;
251251
pointers[0] = new BytePointer(chunkSize);
252-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * byteSize);
253-
assertTrue(Pointer.totalBytes() >= chunkSize * byteSize);
254-
System.out.println(Pointer.totalBytes() + " " + chunkSize * byteSize);
252+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * byteSize);
253+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * byteSize);
254+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * byteSize);
255255
}
256256

257257
@Test public void testShortPointer() {
@@ -316,7 +316,7 @@ static class TestFunction extends FunctionPointer {
316316
for (int j = 0; j < chunks - 1; j++) {
317317
pointers[j] = new ShortPointer(chunkSize);
318318
}
319-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * shortSize);
319+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * shortSize);
320320
try {
321321
fieldReference = pointers;
322322
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -332,9 +332,9 @@ static class TestFunction extends FunctionPointer {
332332
// make sure garbage collection runs
333333
fieldReference = null;
334334
pointers[0] = new ShortPointer(chunkSize);
335-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * shortSize);
336-
assertTrue(Pointer.totalBytes() >= chunkSize * shortSize);
337-
System.out.println(Pointer.totalBytes() + " " + chunkSize * shortSize);
335+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * shortSize);
336+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * shortSize);
337+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * shortSize);
338338
}
339339

340340
@Test public void testIntPointer() {
@@ -399,7 +399,7 @@ static class TestFunction extends FunctionPointer {
399399
for (int j = 0; j < chunks - 1; j++) {
400400
pointers[j] = new IntPointer(chunkSize);
401401
}
402-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * intSize);
402+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * intSize);
403403
try {
404404
fieldReference = pointers;
405405
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -415,9 +415,9 @@ static class TestFunction extends FunctionPointer {
415415
// make sure garbage collection runs
416416
fieldReference = null;
417417
pointers[0] = new IntPointer(chunkSize);
418-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * intSize);
419-
assertTrue(Pointer.totalBytes() >= chunkSize * intSize);
420-
System.out.println(Pointer.totalBytes() + " " + chunkSize * intSize);
418+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * intSize);
419+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * intSize);
420+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * intSize);
421421
}
422422

423423
@Test public void testLongPointer() {
@@ -482,7 +482,7 @@ static class TestFunction extends FunctionPointer {
482482
for (int j = 0; j < chunks - 1; j++) {
483483
pointers[j] = new LongPointer(chunkSize);
484484
}
485-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * longSize);
485+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * longSize);
486486
try {
487487
fieldReference = pointers;
488488
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -498,9 +498,9 @@ static class TestFunction extends FunctionPointer {
498498
// make sure garbage collection runs
499499
fieldReference = null;
500500
pointers[0] = new LongPointer(chunkSize);
501-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * longSize);
502-
assertTrue(Pointer.totalBytes() >= chunkSize * longSize);
503-
System.out.println(Pointer.totalBytes() + " " + chunkSize * longSize);
501+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * longSize);
502+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * longSize);
503+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * longSize);
504504
}
505505

506506
@Test public void testFloatPointer() {
@@ -565,7 +565,7 @@ static class TestFunction extends FunctionPointer {
565565
for (int j = 0; j < chunks - 1; j++) {
566566
pointers[j] = new FloatPointer(chunkSize);
567567
}
568-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * floatSize);
568+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * floatSize);
569569
try {
570570
fieldReference = pointers;
571571
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -581,9 +581,9 @@ static class TestFunction extends FunctionPointer {
581581
// make sure garbage collection runs
582582
fieldReference = null;
583583
pointers[0] = new FloatPointer(chunkSize);
584-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * floatSize);
585-
assertTrue(Pointer.totalBytes() >= chunkSize * floatSize);
586-
System.out.println(Pointer.totalBytes() + " " + chunkSize * floatSize);
584+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * floatSize);
585+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * floatSize);
586+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * floatSize);
587587
}
588588

589589
@Test public void testDoublePointer() {
@@ -648,7 +648,7 @@ static class TestFunction extends FunctionPointer {
648648
for (int j = 0; j < chunks - 1; j++) {
649649
pointers[j] = new DoublePointer(chunkSize);
650650
}
651-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * doubleSize);
651+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * doubleSize);
652652
try {
653653
fieldReference = pointers;
654654
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -664,9 +664,9 @@ static class TestFunction extends FunctionPointer {
664664
// make sure garbage collection runs
665665
fieldReference = null;
666666
pointers[0] = new DoublePointer(chunkSize);
667-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * doubleSize);
668-
assertTrue(Pointer.totalBytes() >= chunkSize * doubleSize);
669-
System.out.println(Pointer.totalBytes() + " " + chunkSize * doubleSize);
667+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * doubleSize);
668+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * doubleSize);
669+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * doubleSize);
670670
}
671671

672672
@Test public void testCharPointer() {
@@ -731,7 +731,7 @@ static class TestFunction extends FunctionPointer {
731731
for (int j = 0; j < chunks - 1; j++) {
732732
pointers[j] = new CharPointer(chunkSize);
733733
}
734-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * charSize);
734+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * charSize);
735735
try {
736736
fieldReference = pointers;
737737
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -747,9 +747,9 @@ static class TestFunction extends FunctionPointer {
747747
// make sure garbage collection runs
748748
fieldReference = null;
749749
pointers[0] = new CharPointer(chunkSize);
750-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * charSize);
751-
assertTrue(Pointer.totalBytes() >= chunkSize * charSize);
752-
System.out.println(Pointer.totalBytes() + " " + chunkSize * charSize);
750+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * charSize);
751+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * charSize);
752+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * charSize);
753753
}
754754

755755
@Test public void testBooleanPointer() {
@@ -813,7 +813,7 @@ static class TestFunction extends FunctionPointer {
813813
for (int j = 0; j < chunks - 1; j++) {
814814
pointers[j] = new BooleanPointer(chunkSize);
815815
}
816-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * booleanSize);
816+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * booleanSize);
817817
try {
818818
fieldReference = pointers;
819819
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -829,9 +829,9 @@ static class TestFunction extends FunctionPointer {
829829
// make sure garbage collection runs
830830
fieldReference = null;
831831
pointers[0] = new BooleanPointer(chunkSize);
832-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * booleanSize);
833-
assertTrue(Pointer.totalBytes() >= chunkSize * booleanSize);
834-
System.out.println(Pointer.totalBytes() + " " + chunkSize * booleanSize);
832+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * booleanSize);
833+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * booleanSize);
834+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * booleanSize);
835835
}
836836

837837
@Test public void testPointerPointer() {
@@ -904,7 +904,7 @@ static class TestFunction extends FunctionPointer {
904904
for (int j = 0; j < chunks - 1; j++) {
905905
pointers[j] = new PointerPointer(chunkSize);
906906
}
907-
assertTrue(Pointer.totalBytes() >= (chunks - 1) * chunkSize * pointerSize);
907+
assertTrue(Pointer.DeallocatorReference.totalBytes >= (chunks - 1) * chunkSize * pointerSize);
908908
try {
909909
fieldReference = pointers;
910910
System.out.println("Note: OutOfMemoryError should get thrown here and printed below.");
@@ -920,9 +920,9 @@ static class TestFunction extends FunctionPointer {
920920
// make sure garbage collection runs
921921
fieldReference = null;
922922
pointers[0] = new PointerPointer(chunkSize);
923-
assertTrue(Pointer.totalBytes() < (chunks - 1) * chunkSize * pointerSize);
924-
assertTrue(Pointer.totalBytes() >= chunkSize * pointerSize);
925-
System.out.println(Pointer.totalBytes() + " " + chunkSize * pointerSize);
923+
assertTrue(Pointer.DeallocatorReference.totalBytes < (chunks - 1) * chunkSize * pointerSize);
924+
assertTrue(Pointer.DeallocatorReference.totalBytes >= chunkSize * pointerSize);
925+
System.out.println(Pointer.DeallocatorReference.totalBytes + " " + chunkSize * pointerSize);
926926
}
927927

928928
@Test public void testDeallocator() throws InterruptedException {

0 commit comments

Comments
 (0)