From 83a76d21bfdfbf613deab1b5b438daa8ebad3c36 Mon Sep 17 00:00:00 2001 From: Matteo Di Giovinazzo Date: Sat, 1 Aug 2020 10:58:07 +0200 Subject: [PATCH] Now totalCount is volatile as totalBytes. Important: even though their values are changed together in a synchronized block, when reading their values might not be in sync. This has been done to favor speed over correctness. --- src/main/java/org/bytedeco/javacpp/Pointer.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/Pointer.java b/src/main/java/org/bytedeco/javacpp/Pointer.java index 7b7961688..a62bfb6a8 100644 --- a/src/main/java/org/bytedeco/javacpp/Pointer.java +++ b/src/main/java/org/bytedeco/javacpp/Pointer.java @@ -32,7 +32,6 @@ import java.nio.ByteOrder; import java.util.Iterator; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; import org.bytedeco.javacpp.annotation.Name; import org.bytedeco.javacpp.tools.Generator; import org.bytedeco.javacpp.tools.PointerBufferPoolMXBean; @@ -286,7 +285,7 @@ static class DeallocatorReference extends PhantomReference implements D Deallocator deallocator; static volatile long totalBytes = 0; - static AtomicLong totalCount = new AtomicLong(); + static volatile long totalCount = 0; long bytes; AtomicInteger count; @@ -300,7 +299,7 @@ final void add() { next.prev = head = this; } totalBytes += bytes; - totalCount.incrementAndGet(); + totalCount++; } } @@ -319,7 +318,7 @@ final void remove() { } prev = next = this; totalBytes -= bytes; - totalCount.decrementAndGet(); + totalCount--; } } @@ -538,7 +537,7 @@ public static long totalBytes() { /** Returns {@link DeallocatorReference#totalCount}, current number of pointers tracked by deallocators. */ public static long totalCount() { - return DeallocatorReference.totalCount.get(); + return DeallocatorReference.totalCount; } /** Returns {@link #maxPhysicalBytes}, the maximum amount of physical memory that should be used. */