package org.kne.cloud.network;

import java.lang.foreign.Arena;
import java.nio.ByteBuffer;

import org.kne.membandboost.MembandBoost;

public class ByteBufferAllocator {
	
	public ByteBufferAllocator(boolean isDirect) {
		
	}
	public ByteBuffer allocate(int capacity) {
		//if(capacity<1024)
		return allocateHeap(capacity);
		//	return allocateNative(capacity);
	
		
	}
	//private Arena ar=BufferedArena.ofBuffered();
	//private Arena ara=Arena.ofAuto();
	public ByteBuffer allocateNative(int capacity) {
		//return ((jdk.internal.foreign.ArenaImpl)ara).allocateNoInit(capacity,1).asByteBuffer();
		
		ByteBuffer buf=Arena.ofAuto().allocate(capacity).asByteBuffer();
		if(buf.capacity()!=capacity)
			throw new InternalError();
		return buf;
	}
	
	
	public static ByteBuffer allocateHeap(int capacity) {
		/*if(capacity<=2048) {
		return MembandBoost.allocateUninitializedHeapBufferBlocked(capacity);
		}else {*/
			return MembandBoost.allocateUninitializedHeapBuffer(capacity);
		//}
	}
	
	
}
