diff --git a/buffer/src/main/java/io/netty/buffer/b2/Allocator.java b/buffer/src/main/java/io/netty/buffer/b2/Allocator.java index 2543143..6c2fb00 100644 --- a/buffer/src/main/java/io/netty/buffer/b2/Allocator.java +++ b/buffer/src/main/java/io/netty/buffer/b2/Allocator.java @@ -74,6 +74,18 @@ public interface Allocator extends AutoCloseable { }; } + static Allocator directWithCleaner() { + return new Allocator() { + @Override + public Buf allocate(long size) { + checkSize(size); + var segment = allocateNative(size); + segment.registerCleaner(Statics.CLEANER); + return new BBuf(segment, SEGMENT_CLOSE); + } + }; + } + static Allocator pooledHeap() { return new SizeClassedMemoryPool() { @Override diff --git a/buffer/src/test/java/io/netty/buffer/b2/DirectBBufWithCleanerTest.java b/buffer/src/test/java/io/netty/buffer/b2/DirectBBufWithCleanerTest.java new file mode 100644 index 0000000..8985e54 --- /dev/null +++ b/buffer/src/test/java/io/netty/buffer/b2/DirectBBufWithCleanerTest.java @@ -0,0 +1,33 @@ +package io.netty.buffer.b2; + +import org.junit.Test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +public class DirectBBufWithCleanerTest extends DirectBBufTest { + @Override + protected Allocator createAllocator() { + return Allocator.directWithCleaner(); + } + + @Test + public void bufferMustBeClosedByCleaner() throws InterruptedException { + var allocator = createAllocator(); + allocator.close(); + int iterations = 100; + int allocationSize = 1024; + for (int i = 0; i < iterations; i++) { + allocateAndForget(allocator, allocationSize); + System.gc(); + System.runFinalization(); + } + var sum = Statics.MEM_USAGE_NATIVE.sum(); + var totalAllocated = (long) allocationSize * iterations; + assertThat(sum, lessThan(totalAllocated)); + } + + protected void allocateAndForget(Allocator allocator, long size) { + allocator.allocate(size); + } +} diff --git a/buffer/src/test/java/io/netty/buffer/b2/PooledDirectBBufWithCleanerTest.java b/buffer/src/test/java/io/netty/buffer/b2/PooledDirectBBufWithCleanerTest.java index 5ad18ac..6c4388f 100644 --- a/buffer/src/test/java/io/netty/buffer/b2/PooledDirectBBufWithCleanerTest.java +++ b/buffer/src/test/java/io/netty/buffer/b2/PooledDirectBBufWithCleanerTest.java @@ -20,28 +20,12 @@ import org.junit.Test; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -public class PooledDirectBBufWithCleanerTest extends DirectBBufTest { +public class PooledDirectBBufWithCleanerTest extends DirectBBufWithCleanerTest { @Override protected Allocator createAllocator() { return Allocator.pooledDirectWithCleaner(); } - @Test - public void bufferMustBeClosedByCleaner() throws InterruptedException { - var allocator = createAllocator(); - allocator.close(); - int iterations = 100; - int allocationSize = 1024; - for (int i = 0; i < iterations; i++) { - allocateAndForget(allocator, allocationSize); - System.gc(); - System.runFinalization(); - } - var sum = Statics.MEM_USAGE_NATIVE.sum(); - var totalAllocated = (long) allocationSize * iterations; - assertThat(sum, lessThan(totalAllocated)); - } - @Test public void buffersMustBeReusedByPoolingAllocatorEvenWhenDroppedByCleanerInsteadOfExplicitly() throws InterruptedException { @@ -58,8 +42,4 @@ public class PooledDirectBBufWithCleanerTest extends DirectBBufTest { assertThat(sum, lessThan(totalAllocated)); } } - - protected void allocateAndForget(Allocator allocator, long size) { - allocator.allocate(size); - } }