SyncPool/Pool: allow to pool any subclass of Inlist

This commit is contained in:
Hannes Janetzek 2014-03-18 23:09:28 +01:00
parent 6113d284d8
commit 56a223e6c6
5 changed files with 74 additions and 61 deletions

View File

@ -74,12 +74,12 @@ public class MapRenderer {
}
public BufferItem get(int size) {
BufferItem b = pool;
BufferItem b = mPool;
if (b == null) {
b = new BufferItem();
} else {
pool = b.next;
mPool = b.next;
b.next = null;
}
if (b.tmpBufferSize < size)

View File

@ -24,7 +24,7 @@ import org.oscim.utils.pool.SyncPool;
public class SymbolItem extends Inlist<SymbolItem> {
public final static SyncPool<SymbolItem> pool = new SyncPool<SymbolItem>() {
public final static SyncPool<SymbolItem> pool = new SyncPool<SymbolItem>(128) {
@Override
protected SymbolItem createItem() {

View File

@ -30,9 +30,11 @@ import javax.annotation.CheckReturnValue;
*/
public class Inlist<T extends Inlist<T>> {
/** UNTESTED */
public static class List<T extends Inlist<T>> implements Iterable<T> {
T head;
T cur;
@SuppressWarnings({ "rawtypes", "unchecked" })
public static class List<T extends Inlist<?>> implements Iterable<T> {
Inlist head;
Inlist cur;
Iterator<T> mIterator = new Iterator<T>() {
@Override
@ -45,14 +47,14 @@ public class Inlist<T extends Inlist<T>> {
if (cur == null)
throw new IllegalStateException();
T tmp = cur;
Inlist tmp = cur;
cur = cur.next;
return tmp;
return (T) tmp;
}
@Override
public void remove() {
T tmp = cur.next;
T tmp = (T) cur.next;
head = Inlist.remove(head, cur);
cur = tmp;
}
@ -65,7 +67,7 @@ public class Inlist<T extends Inlist<T>> {
}
public void push(T it) {
it.next = head;
((Inlist) it).next = head;
head = it;
}
@ -75,19 +77,23 @@ public class Inlist<T extends Inlist<T>> {
}
public T clear() {
T ret = head;
Inlist ret = head;
head = null;
cur = null;
return ret;
return (T) ret;
}
public T getHead() {
return head;
return (T) head;
}
}
public T next;
public T next() {
return next;
}
/**
* Push 'item' onto 'list'.
*

View File

@ -18,11 +18,11 @@ package org.oscim.utils.pool;
import javax.annotation.CheckReturnValue;
public abstract class Pool<T extends Inlist<T>> {
protected T pool;
protected int limit;
protected int fill;
@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class Pool<T extends Inlist<?>> {
protected T mPool;
protected int mLimit;
protected int mFill;
/**
* @param item release resources
@ -47,8 +47,8 @@ public abstract class Pool<T extends Inlist<T>> {
if (!clearItem(item))
return null;
item.next = pool;
pool = item;
((Inlist) item).next = mPool;
mPool = item;
return null;
}
@ -65,12 +65,13 @@ public abstract class Pool<T extends Inlist<T>> {
return null;
while (list != null) {
T next = list.next;
T next = (T) list.next;
clearItem(list);
list.next = pool;
pool = list;
((Inlist) list).next = mPool;
mPool = list;
list = next;
}
@ -84,21 +85,21 @@ public abstract class Pool<T extends Inlist<T>> {
clearItem(item);
Inlist.remove(list, item);
Inlist.remove((Inlist) list, item);
return list;
}
/** get an item from pool */
public T get() {
if (pool == null)
if (mPool == null)
return createItem();
T ret = pool;
pool = pool.next;
Inlist ret = mPool;
mPool = (T) mPool.next;
ret.next = null;
return ret;
return (T) ret;
}
protected abstract T createItem();

View File

@ -18,24 +18,25 @@ package org.oscim.utils.pool;
import javax.annotation.CheckReturnValue;
public abstract class SyncPool<T extends Inlist<T>> {
protected final int maxFill;
protected int fill;
public abstract class SyncPool<T extends Inlist<?>> {
protected final int mMaxFill;
protected final boolean mClearItems;
protected T pool;
public SyncPool() {
maxFill = 100;
fill = 0;
}
protected int mFill;
protected T mPool;
public SyncPool(int maxItemsInPool) {
maxFill = maxItemsInPool;
fill = 0;
this(maxItemsInPool, true);
}
public SyncPool(int maxItemsInPool, boolean clearItems) {
mMaxFill = maxItemsInPool;
mFill = 0;
mClearItems = clearItems;
}
public int getFill() {
return fill;
return mFill;
}
/**
@ -45,8 +46,8 @@ public abstract class SyncPool<T extends Inlist<T>> {
* number of initial items
*/
public void init(int items) {
fill = 0;
pool = null;
mFill = 0;
mPool = null;
}
/**
@ -80,24 +81,25 @@ public abstract class SyncPool<T extends Inlist<T>> {
* Usage item = pool.release(item), to ensure to not keep a reference to
* item!
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@CheckReturnValue
public T release(T item) {
if (item == null)
return null;
if (!clearItem(item)) {
if (mClearItems && !clearItem(item)) {
// dont add back to pool
freeItem(item);
return null;
}
if (fill < maxFill) {
if (mFill < mMaxFill) {
synchronized (this) {
fill++;
mFill++;
item.next = pool;
pool = item;
((Inlist) item).next = (T) mPool;
mPool = item;
}
} else {
} else if (mClearItems) {
freeItem(item);
}
return null;
@ -109,35 +111,38 @@ public abstract class SyncPool<T extends Inlist<T>> {
* Usage list = pool.releaseAll(list), to ensure to not keep a reference to
* list!
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@CheckReturnValue
public T releaseAll(T item) {
if (item == null)
return null;
if (fill > maxFill) {
if (mFill > mMaxFill) {
while (item != null) {
clearItem(item);
freeItem(item);
item = item.next;
if (mClearItems) {
clearItem(item);
freeItem(item);
}
item = (T) item.next;
}
return null;
}
synchronized (this) {
while (item != null) {
T next = item.next;
T next = (T) item.next;
if (!clearItem(item)) {
if (mClearItems && !clearItem(item)) {
// dont add back to pool
freeItem(item);
item = next;
continue;
}
fill++;
mFill++;
item.next = pool;
pool = item;
((Inlist) item).next = (T) mPool;
mPool = item;
item = next;
}
@ -151,17 +156,18 @@ public abstract class SyncPool<T extends Inlist<T>> {
*
* @return the item
*/
@SuppressWarnings("unchecked")
public T get() {
synchronized (this) {
if (pool == null) {
if (mPool == null) {
return createItem();
}
fill--;
mFill--;
T ret = pool;
pool = pool.next;
T ret = mPool;
mPool = (T) mPool.next;
ret.next = null;
return ret;