forked from KNEMC/KLALB
85 lines
2.5 KiB
Java
85 lines
2.5 KiB
Java
package org.kne.cloud.network;
|
|
|
|
import java.lang.foreign.Arena;
|
|
import java.lang.foreign.MemorySegment;
|
|
import java.lang.foreign.SegmentAllocator;
|
|
import java.lang.ref.Cleaner;
|
|
import java.lang.ref.PhantomReference;
|
|
import java.lang.ref.Reference;
|
|
import java.lang.ref.ReferenceQueue;
|
|
import java.lang.ref.WeakReference;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.nio.BufferOverflowException;
|
|
import java.nio.ByteBuffer;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import org.kne.debug.TimeDebugger;
|
|
import org.kne.membandboost.MembandBoost;
|
|
|
|
import jdk.internal.misc.Unsafe;
|
|
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;
|
|
}
|
|
private static jdk.internal.misc.Unsafe usf;
|
|
private static Method meth;
|
|
static {
|
|
Class<?> name;
|
|
try {
|
|
name = Class.forName("jdk.internal.misc.Unsafe");
|
|
Field field = name.getDeclaredField("theUnsafe");
|
|
field.setAccessible(true);
|
|
usf= (Unsafe) field.get(null);
|
|
meth=name.getMethod("allocateUninitializedArray", Class.class,int.class);
|
|
|
|
} catch (java.lang.reflect.InaccessibleObjectException e) {
|
|
e.printStackTrace();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
public static byte[] allocateUninitializedArray(int capacity) {
|
|
if(usf!=null) {
|
|
return (byte[]) usf.allocateUninitializedArray(byte.class, capacity);
|
|
}else {
|
|
return new byte[capacity];
|
|
}
|
|
}
|
|
|
|
public static ByteBuffer allocateHeap(int capacity) {
|
|
/*if(capacity<=2048) {
|
|
return MembandBoost.allocateUninitializedHeapBufferBlocked(capacity);
|
|
}else {*/
|
|
return MembandBoost.allocateUninitializedHeapBuffer(capacity);
|
|
//}
|
|
}
|
|
public static ByteBuffer allocateHeap0(int capacity) {
|
|
return ByteBuffer.wrap((byte[])allocateUninitializedArray(capacity)) ;
|
|
}
|
|
|
|
|
|
|
|
}
|