forked from MagicBane/Server
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.4 KiB
69 lines
2.4 KiB
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . |
|
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· |
|
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ |
|
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ |
|
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ |
|
// Magicbane Emulator Project © 2013 - 2022 |
|
// www.magicbane.com |
|
|
|
|
|
package engine.pooling; |
|
|
|
import org.pmw.tinylog.Logger; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
public class ByteBufferPool extends LinkedObjectPool<ByteBuffer> { |
|
|
|
private final int defaultBufferSize; |
|
|
|
public ByteBufferPool(int defaultBufferSize) { |
|
super(ObjectPool.DEFAULT_SIZE); |
|
this.defaultBufferSize = defaultBufferSize; |
|
} |
|
|
|
public ByteBufferPool(int size, int defaultBufferSize) { |
|
super(size); |
|
this.defaultBufferSize = defaultBufferSize; |
|
} |
|
|
|
@Override |
|
protected ByteBuffer makeNewObject() { |
|
return ByteBuffer.allocate(defaultBufferSize); |
|
} |
|
|
|
@Override |
|
protected void resetObject(ByteBuffer obj) { |
|
obj.clear(); |
|
} |
|
|
|
@Override |
|
public ByteBuffer get() { |
|
// Logger.debug("ByteBufferPool.get() BB.capacity(): " + bb.capacity() |
|
// + ", bb.pos(): " + bb.position() + ". Pool.size() is now: " |
|
// + this.getPoolSize()); |
|
return super.get(); |
|
} |
|
|
|
@Override |
|
public void put(ByteBuffer bb) { |
|
if (bb.isDirect()) |
|
super.put(bb); |
|
// Logger.debug("ByteBufferPool.put() BB.capacity(): " + bb.capacity() |
|
// + ", bb.pos(): " + bb.position() + ". Pool.size() is now: " |
|
// + this.getPoolSize()); |
|
} |
|
|
|
@Override |
|
protected void handlePoolExhaustion() { |
|
Logger.debug("ByteBufferPool(" + defaultBufferSize |
|
+ ") exhausted, making more objects."); |
|
|
|
// If none exist, make (and pool) a few |
|
// Dont sync the loop, let the makeNewObject() |
|
// call return before locking the pool object |
|
for (int i = 0; i < 5; ++i) |
|
this.makeAndAdd(); |
|
} |
|
|
|
}
|
|
|