KLALB V3.4
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
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;
|
||||
@@ -18,66 +21,41 @@ import org.kne.debug.TimeDebugger;
|
||||
|
||||
import jdk.internal.misc.Unsafe;
|
||||
public class ByteBufferAllocator {
|
||||
private List<ByteBufferPool> bbfps=new ArrayList<>();
|
||||
|
||||
private ByteBufferPool[]bbfpsarr;
|
||||
|
||||
|
||||
private ReferenceQueue<ByteBuffer> refq=new ReferenceQueue<>();
|
||||
private boolean isDirect;
|
||||
|
||||
public ByteBufferAllocator(boolean isDirect) {
|
||||
this.isDirect=isDirect;
|
||||
int i=1;
|
||||
int maxi=0;
|
||||
int mps=16;
|
||||
for (int j = 0; j < 18; j++) {
|
||||
int count=100;
|
||||
mps+=count;
|
||||
bbfps.add(new ByteBufferPool(mps, i,isDirect));
|
||||
maxi=i;
|
||||
i<<=1;
|
||||
}
|
||||
bbfpsarr=new ByteBufferPool[maxi];
|
||||
for (int j = 0; j < bbfpsarr.length; j++) {
|
||||
bbfpsarr[j]=getByteBufferPool(j);
|
||||
}
|
||||
|
||||
Thread t= new Thread(()->{
|
||||
static Thread t;
|
||||
static {
|
||||
t=new Thread(()->{
|
||||
while(true) {
|
||||
System.gc();
|
||||
try {
|
||||
Reference<? extends ByteBuffer> ref;
|
||||
ref = refq.remove();
|
||||
if(ref!=null) {
|
||||
ref.clear();
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO 自动生成的 catch 块
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
t.setDaemon(true);
|
||||
t.setPriority(Thread.MAX_PRIORITY-1);
|
||||
t.start();
|
||||
}
|
||||
public ByteBuffer allocate(int capacity) {
|
||||
if(true) {
|
||||
return allocateHeap(capacity);
|
||||
}
|
||||
//new Exception().printStackTrace();
|
||||
ByteBufferPool bbfp=bbfpsarr[capacity];
|
||||
ByteBuffer bbf=null;
|
||||
try {
|
||||
bbf=bbfp.borrow();
|
||||
}catch(OutOfMemoryError err) {
|
||||
System.gc();
|
||||
Thread.yield();
|
||||
bbf=bbfp.borrow();
|
||||
}
|
||||
ByteBuffer bbfs=bbf.slice(0, capacity);
|
||||
|
||||
public ByteBufferAllocator(boolean isDirect) {
|
||||
|
||||
new ByteBufferPhantomReference(bbfs,refq,bbf,bbfp);
|
||||
return bbfs;
|
||||
}
|
||||
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)Arena.ofAuto()).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;
|
||||
@@ -97,30 +75,23 @@ public class ByteBufferAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
private static ByteBuffer allocateHeap(int capacity) {
|
||||
//return ByteBuffer.wrap((byte[]) usf.allocateUninitializedArray(byte.class, capacity));
|
||||
public static byte[] allocateArray(int capacity) {
|
||||
if(usf!=null&&meth!=null) {
|
||||
try {
|
||||
return ByteBuffer.wrap((byte[]) usf.allocateUninitializedArray(byte.class, capacity));
|
||||
} catch (Exception e) {
|
||||
return ByteBuffer.wrap(new byte[capacity]);
|
||||
}
|
||||
}else {
|
||||
return ByteBuffer.wrap(new byte[capacity]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ByteBufferPool getByteBufferPool(int capacity) {
|
||||
int elen=0;
|
||||
for (int i = 0; i < bbfps.size(); i++) {
|
||||
ByteBufferPool bfp=bbfps.get(i);
|
||||
if(capacity<=(elen= bfp.getLength())) {
|
||||
return bfp;
|
||||
try {
|
||||
return (byte[]) usf.allocateUninitializedArray(byte.class, capacity);
|
||||
} catch (Exception e) {
|
||||
return new byte[capacity];
|
||||
}
|
||||
}else {
|
||||
return new byte[capacity];
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("capacity:"+capacity+">"+elen);
|
||||
}
|
||||
|
||||
public static ByteBuffer allocateHeap(int capacity) {
|
||||
return ByteBuffer.wrap((byte[])allocateArray(capacity)) ;
|
||||
}
|
||||
|
||||
|
||||
private static class ByteBufferPhantomReference extends PhantomReference<ByteBuffer>{
|
||||
|
||||
private static AtomicReference<ByteBufferPhantomReference> first=new AtomicReference<>(null);
|
||||
|
||||
Reference in New Issue
Block a user