KLALB V3.6.0 写了一半
This commit is contained in:
@@ -18,39 +18,25 @@ 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 {
|
||||
static Thread t;
|
||||
static {
|
||||
t=new Thread(()->{
|
||||
while(true) {
|
||||
System.gc();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
t.setDaemon(true);
|
||||
t.setPriority(Thread.MAX_PRIORITY-1);
|
||||
t.start();
|
||||
}
|
||||
|
||||
|
||||
public ByteBufferAllocator(boolean isDirect) {
|
||||
|
||||
}
|
||||
public ByteBuffer allocate(int capacity) {
|
||||
//if(capacity<1024)
|
||||
return allocateHeap(capacity);
|
||||
//return allocateNative(capacity);
|
||||
// return allocateNative(capacity);
|
||||
|
||||
|
||||
}
|
||||
//private Arena ar=BufferedArena.ofBuffered();
|
||||
private Arena ara=Arena.ofAuto();
|
||||
//private Arena ara=Arena.ofAuto();
|
||||
public ByteBuffer allocateNative(int capacity) {
|
||||
//return ((jdk.internal.foreign.ArenaImpl)Arena.ofAuto()).allocateNoInit(capacity,1).asByteBuffer();
|
||||
//return ((jdk.internal.foreign.ArenaImpl)ara).allocateNoInit(capacity,1).asByteBuffer();
|
||||
|
||||
ByteBuffer buf=Arena.ofAuto().allocate(capacity).asByteBuffer();
|
||||
if(buf.capacity()!=capacity)
|
||||
@@ -74,64 +60,25 @@ public class ByteBufferAllocator {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] allocateArray(int capacity) {
|
||||
if(usf!=null&&meth!=null) {
|
||||
try {
|
||||
public static byte[] allocateUninitializedArray(int capacity) {
|
||||
if(usf!=null) {
|
||||
return (byte[]) usf.allocateUninitializedArray(byte.class, capacity);
|
||||
} catch (Exception e) {
|
||||
return new byte[capacity];
|
||||
}
|
||||
}else {
|
||||
return new byte[capacity];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ByteBuffer allocateHeap(int capacity) {
|
||||
return ByteBuffer.wrap((byte[])allocateArray(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)) ;
|
||||
}
|
||||
|
||||
|
||||
private static class ByteBufferPhantomReference extends PhantomReference<ByteBuffer>{
|
||||
|
||||
private static AtomicReference<ByteBufferPhantomReference> first=new AtomicReference<>(null);
|
||||
|
||||
private AtomicReference<ByteBufferPhantomReference> next=new AtomicReference<>(null);
|
||||
private AtomicReference<ByteBufferPhantomReference> prev=new AtomicReference<>(null);
|
||||
|
||||
private ByteBuffer father;
|
||||
private ByteBufferPool pool;
|
||||
|
||||
public ByteBufferPhantomReference(ByteBuffer referent, ReferenceQueue<? super ByteBuffer> q,ByteBuffer father,ByteBufferPool pool) {
|
||||
super(referent, q);
|
||||
this.father=father;
|
||||
this.pool=pool;
|
||||
insert();
|
||||
}
|
||||
|
||||
private void insert() {
|
||||
ByteBufferPhantomReference refn= first.getAndSet(this);
|
||||
if(refn!=null) {
|
||||
next.set(refn);
|
||||
refn.prev.set(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void remove() {
|
||||
ByteBufferPhantomReference prevn= prev.getAndSet(null);
|
||||
ByteBufferPhantomReference nextn=next.getAndSet(null);
|
||||
if(prevn!=null)
|
||||
prevn.next.set(nextn);
|
||||
if(nextn!=null)
|
||||
nextn.prev.set(prevn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
//System.out.println("clear");
|
||||
pool.back(father);
|
||||
remove();
|
||||
super.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user