Allocator takes size as an int instead of a long.

Motivation:
We don't want to support buffers larger than what can be addressed with an int.
This ensures we won't run into trouble with the max IO size on various operating systems.
This commit is contained in:
Chris Vest 2020-10-30 14:31:45 +01:00
parent 2c5be51ec6
commit fdeed0c94e
4 changed files with 9 additions and 9 deletions

View File

@ -43,7 +43,7 @@ public interface Allocator extends AutoCloseable {
* @param size The size of {@link Buf} to allocate. * @param size The size of {@link Buf} to allocate.
* @return The newly allocated {@link Buf}. * @return The newly allocated {@link Buf}.
*/ */
Buf allocate(long size); Buf allocate(int size);
/** /**
* Allocate a {@link Buf} of the given size in bytes. This method may throw an {@link OutOfMemoryError} if there is * Allocate a {@link Buf} of the given size in bytes. This method may throw an {@link OutOfMemoryError} if there is
@ -55,7 +55,7 @@ public interface Allocator extends AutoCloseable {
* @param order The default byte order used by the accessor methods that don't have an explicit byte order. * @param order The default byte order used by the accessor methods that don't have an explicit byte order.
* @return The newly allocated {@link Buf}. * @return The newly allocated {@link Buf}.
*/ */
default Buf allocate(long size, ByteOrder order) { default Buf allocate(int size, ByteOrder order) {
return allocate(size).order(order); return allocate(size).order(order);
} }
@ -71,7 +71,7 @@ public interface Allocator extends AutoCloseable {
var man = MemoryManager.getHeapMemoryManager(); var man = MemoryManager.getHeapMemoryManager();
return new Allocator() { return new Allocator() {
@Override @Override
public Buf allocate(long size) { public Buf allocate(int size) {
checkSize(size); checkSize(size);
return man.allocateConfined(size, man.drop(), null); return man.allocateConfined(size, man.drop(), null);
} }
@ -82,7 +82,7 @@ public interface Allocator extends AutoCloseable {
var man = MemoryManager.getNativeMemoryManager(); var man = MemoryManager.getNativeMemoryManager();
return new Allocator() { return new Allocator() {
@Override @Override
public Buf allocate(long size) { public Buf allocate(int size) {
checkSize(size); checkSize(size);
return man.allocateConfined(size, man.drop(), null); return man.allocateConfined(size, man.drop(), null);
} }
@ -93,7 +93,7 @@ public interface Allocator extends AutoCloseable {
var man = MemoryManager.getNativeMemoryManager(); var man = MemoryManager.getNativeMemoryManager();
return new Allocator() { return new Allocator() {
@Override @Override
public Buf allocate(long size) { public Buf allocate(int size) {
checkSize(size); checkSize(size);
return man.allocateConfined(size, man.drop(), Statics.CLEANER); return man.allocateConfined(size, man.drop(), Statics.CLEANER);
} }

View File

@ -36,7 +36,7 @@ class SizeClassedMemoryPool implements Allocator, Drop<Buf> {
} }
@Override @Override
public Buf allocate(long size) { public Buf allocate(int size) {
Allocator.checkSize(size); Allocator.checkSize(size);
var sizeClassPool = getSizeClassPool(size); var sizeClassPool = getSizeClassPool(size);
Send<Buf> send = sizeClassPool.poll(); Send<Buf> send = sizeClassPool.poll();

View File

@ -47,8 +47,8 @@ public class CompositeBufTest extends BufTest {
var b = secondAllocator.get(); var b = secondAllocator.get();
return new Allocator() { return new Allocator() {
@Override @Override
public Buf allocate(long size) { public Buf allocate(int size) {
long half = size / 2; int half = size / 2;
try (Buf firstHalf = a.allocate(half); try (Buf firstHalf = a.allocate(half);
Buf secondHalf = b.allocate(size - half)) { Buf secondHalf = b.allocate(size - half)) {
return Buf.compose(firstHalf, secondHalf); return Buf.compose(firstHalf, secondHalf);

View File

@ -44,7 +44,7 @@ public class DirectBufWithCleanerTest extends DirectBufTest {
assertThat(sum, lessThan(totalAllocated)); assertThat(sum, lessThan(totalAllocated));
} }
protected void allocateAndForget(Allocator allocator, long size) { protected void allocateAndForget(Allocator allocator, int size) {
allocator.allocate(size); allocator.allocate(size);
} }
} }