From 5b1b5219ebec690a66c55b9bee9b2c6b74193795 Mon Sep 17 00:00:00 2001 From: Dmytro Vyazelenko <696855+vyazelenko@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:40:26 +0100 Subject: [PATCH] [Java] Do not use `UnsafeAccess`. --- .../java/io/aeron/status/ReadableCounter.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/aeron-client/src/main/java/io/aeron/status/ReadableCounter.java b/aeron-client/src/main/java/io/aeron/status/ReadableCounter.java index 4541691376..fbf6efcbe3 100644 --- a/aeron-client/src/main/java/io/aeron/status/ReadableCounter.java +++ b/aeron-client/src/main/java/io/aeron/status/ReadableCounter.java @@ -16,8 +16,8 @@ package io.aeron.status; import io.aeron.Aeron; -import org.agrona.UnsafeAccess; import org.agrona.concurrent.AtomicBuffer; +import org.agrona.concurrent.UnsafeBuffer; import org.agrona.concurrent.status.CountersReader; import static org.agrona.BitUtil.SIZE_OF_LONG; @@ -27,15 +27,14 @@ *

* Note:The user should call {@link #isClosed()} and ensure the result is false to avoid a race on reading a * closed {@link io.aeron.Counter}. - * */ + */ public final class ReadableCounter implements AutoCloseable { - private final long addressOffset; + private final CountersReader countersReader; + private final UnsafeBuffer valueBuffer; private final long registrationId; private final int counterId; private volatile boolean isClosed = false; - private final byte[] buffer; - private final CountersReader countersReader; /** * Construct a view of an existing counter. @@ -61,8 +60,7 @@ public ReadableCounter(final CountersReader countersReader, final long registrat final int counterOffset = CountersReader.counterOffset(counterId); valuesBuffer.boundsCheck(counterOffset, SIZE_OF_LONG); - this.buffer = valuesBuffer.byteArray(); - this.addressOffset = valuesBuffer.addressOffset() + counterOffset; + valueBuffer = new UnsafeBuffer(valuesBuffer, counterOffset, SIZE_OF_LONG); } /** @@ -100,11 +98,10 @@ public int counterId() /** * Return the state of the counter. * + * @return state for the counter. * @see CountersReader#RECORD_ALLOCATED * @see CountersReader#RECORD_RECLAIMED * @see CountersReader#RECORD_UNUSED - * - * @return state for the counter. */ public int state() { @@ -131,7 +128,7 @@ public String label() */ public long get() { - return UnsafeAccess.UNSAFE.getLongVolatile(buffer, addressOffset); + return valueBuffer.getLongVolatile(0); } /** @@ -141,7 +138,7 @@ public long get() */ public long getWeak() { - return UnsafeAccess.UNSAFE.getLong(buffer, addressOffset); + return valueBuffer.getLong(0); } /**