Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ReadOnlyBufferException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -136,7 +135,7 @@ public final class MemorySegment {
*/
private final boolean allowWrap;

private final AtomicBoolean isFreedAtomic;
private boolean isFreed = false;

/**
* Creates a new memory segment that represents the memory of the byte array.
Expand All @@ -158,7 +157,6 @@ public final class MemorySegment {
this.owner = owner;
this.allowWrap = true;
this.cleaner = null;
this.isFreedAtomic = new AtomicBoolean(false);
}

/**
Expand Down Expand Up @@ -204,7 +202,6 @@ public final class MemorySegment {
this.owner = owner;
this.allowWrap = allowWrap;
this.cleaner = cleaner;
this.isFreedAtomic = new AtomicBoolean(false);
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -237,13 +234,14 @@ public boolean isFreed() {
* segment and will fail. The actual memory (heap or off-heap) will only be released after this
* memory segment object has become garbage collected.
*/
public void free() {
if (isFreedAtomic.getAndSet(true)) {
public synchronized void free() {
if (isFreed) {
// the segment has already been freed
if (checkMultipleFree) {
throw new IllegalStateException("MemorySegment can be freed only once!");
}
} else {
isFreed = true;
// this ensures we can place no more data and trigger
// the checks for the freed segment
address = addressLimit + 1;
Expand Down