Skip to content

Commit 83a76d2

Browse files
committed
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.
1 parent d4149e0 commit 83a76d2

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.nio.ByteOrder;
3333
import java.util.Iterator;
3434
import java.util.concurrent.atomic.AtomicInteger;
35-
import java.util.concurrent.atomic.AtomicLong;
3635
import org.bytedeco.javacpp.annotation.Name;
3736
import org.bytedeco.javacpp.tools.Generator;
3837
import org.bytedeco.javacpp.tools.PointerBufferPoolMXBean;
@@ -286,7 +285,7 @@ static class DeallocatorReference extends PhantomReference<Pointer> implements D
286285
Deallocator deallocator;
287286

288287
static volatile long totalBytes = 0;
289-
static AtomicLong totalCount = new AtomicLong();
288+
static volatile long totalCount = 0;
290289
long bytes;
291290

292291
AtomicInteger count;
@@ -300,7 +299,7 @@ final void add() {
300299
next.prev = head = this;
301300
}
302301
totalBytes += bytes;
303-
totalCount.incrementAndGet();
302+
totalCount++;
304303
}
305304
}
306305

@@ -319,7 +318,7 @@ final void remove() {
319318
}
320319
prev = next = this;
321320
totalBytes -= bytes;
322-
totalCount.decrementAndGet();
321+
totalCount--;
323322
}
324323
}
325324

@@ -538,7 +537,7 @@ public static long totalBytes() {
538537

539538
/** Returns {@link DeallocatorReference#totalCount}, current number of pointers tracked by deallocators. */
540539
public static long totalCount() {
541-
return DeallocatorReference.totalCount.get();
540+
return DeallocatorReference.totalCount;
542541
}
543542

544543
/** Returns {@link #maxPhysicalBytes}, the maximum amount of physical memory that should be used. */

0 commit comments

Comments
 (0)