use @CheckReturnValue for safer pool/Inlist usage

This commit is contained in:
Hannes Janetzek 2013-09-29 05:38:18 +02:00
parent d771d70c3a
commit 5775c8cbf3
18 changed files with 102 additions and 111 deletions

View File

@ -92,7 +92,7 @@ class TextRenderer extends ElementRenderer {
class LabelPool extends Pool<TextItem> {
Label releaseAndGetNext(Label l) {
if (l.item != null)
TextItem.pool.release(l.item);
l.item = TextItem.pool.release(l.item);
// drop references
l.item = null;
@ -101,6 +101,7 @@ class TextRenderer extends ElementRenderer {
Label ret = (Label) l.next;
// ignore warning
super.release(l);
return ret;
@ -561,7 +562,7 @@ class TextRenderer extends ElementRenderer {
}
// temporary used Label
mPool.release(l);
l = (Label) mPool.release(l);
TextLayer tl = mNextLayer.textLayer;
@ -650,8 +651,7 @@ class TextRenderer extends ElementRenderer {
}
/* private */void cleanup() {
mPool.releaseAll(mLabels);
mLabels = null;
mLabels = (Label) mPool.releaseAll(mLabels);
mTileSet.releaseTiles();
mLabelTask = null;
}

View File

@ -128,8 +128,7 @@ public class MapRenderer {
}
public void releaseBuffers() {
releaseAll(mUsedBuffers);
mUsedBuffers = null;
mUsedBuffers = releaseAll(mUsedBuffers);
}
}

View File

@ -66,7 +66,7 @@ public abstract class SpriteManager<T> {
}
public void clear() {
TextureItem.releaseAll(mTexture);
mTexture = TextureItem.pool.releaseAll(mTexture);
mAtlas.clear();
items = null;

View File

@ -146,8 +146,7 @@ public class BitmapLayer extends TextureLayer {
TextureItem.releaseTexture(textures);
textures = null;
VertexItem.pool.releaseAll(vertexItems);
vertexItems = null;
vertexItems = VertexItem.pool.releaseAll(vertexItems);
}
public static final class Renderer {

View File

@ -295,7 +295,7 @@ public class ElementLayers {
l.vertexItems = null;
l.curItem = null;
}
VertexItem.pool.releaseAll(items);
items = VertexItem.pool.releaseAll(items);
return size;
}
@ -311,8 +311,7 @@ public class ElementLayers {
sbuf.put(it.vertices, 0, VertexItem.SIZE);
}
VertexItem.pool.releaseAll(l.vertexItems);
l.vertexItems = null;
l.vertexItems = VertexItem.pool.releaseAll(l.vertexItems);
}
// cleanup only when layers are not used by tile or overlay anymore!
@ -321,8 +320,7 @@ public class ElementLayers {
// clear line and polygon layers directly
for (RenderElement l = baseLayers; l != null; l = l.next) {
if (l.vertexItems != null) {
VertexItem.pool.releaseAll(l.vertexItems);
l.vertexItems = null;
l.vertexItems = VertexItem.pool.releaseAll(l.vertexItems);
l.curItem = null;
}
l.verticesCnt = 0;

View File

@ -401,16 +401,10 @@ public class ExtrusionLayer extends RenderElement {
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, 0);
for (int i = 0; i < 4; i++)
VertexItem.pool.releaseAll(mIndices[i]);
VertexItem.pool.releaseAll(mVertices);
mIndices = null;
mVertices = null;
mClipper = null;
clear();
compiled = true;
mClipper = null;
}
@Override
@ -420,11 +414,10 @@ public class ExtrusionLayer extends RenderElement {
vboVertices = BufferObject.release(vboVertices);
} else {
for (int i = 0; i < 4; i++)
VertexItem.pool.releaseAll(mIndices[i]);
mIndices[i] = VertexItem.pool.releaseAll(mIndices[i]);
mIndices = null;
VertexItem.pool.releaseAll(mVertices);
mVertices = null;
mVertices = VertexItem.pool.releaseAll(mVertices);
}
}
}

View File

@ -23,9 +23,9 @@ import org.oscim.backend.canvas.Paint.Cap;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapPosition;
import org.oscim.core.Tile;
import org.oscim.renderer.GLState;
import org.oscim.renderer.GLUtils;
import org.oscim.renderer.MapRenderer;
import org.oscim.renderer.GLState;
import org.oscim.renderer.MapRenderer.Matrices;
import org.oscim.theme.renderinstruction.Line;
import org.oscim.utils.FastMath;
@ -567,8 +567,7 @@ public final class LineLayer extends RenderElement {
@Override
public void clear() {
if (vertexItems != null) {
VertexItem.pool.releaseAll(vertexItems);
vertexItems = null;
vertexItems = VertexItem.pool.releaseAll(vertexItems);
curItem = null;
}
verticesCnt = 0;
@ -744,7 +743,7 @@ public final class LineLayer extends RenderElement {
}
// Cap mode
if (ll.width < 1.5 /*|| ll.line.fixed*/) {
if (ll.width < 1.5 /* || ll.line.fixed */) {
if (capMode != CAP_THIN) {
capMode = CAP_THIN;
GL.glUniform1f(uLineMode, capMode);

View File

@ -62,11 +62,8 @@ public class MeshLayer extends RenderElement {
Log.d(TAG, "-> " + verticesCnt + " " + numIndices);
if (numIndices <= 0) {
vertexItems.release();
vertexItems = null;
indiceItems.release();
indiceItems = null;
vertexItems = VertexItem.pool.releaseAll(vertexItems);
indiceItems = VertexItem.pool.releaseAll(indiceItems);
}
}
@ -95,8 +92,7 @@ public class MeshLayer extends RenderElement {
for (VertexItem it = indiceItems; it != null; it = it.next)
sbuf.put(it.vertices, 0, it.used);
VertexItem.pool.releaseAll(indiceItems);
indiceItems = null;
indiceItems = VertexItem.pool.releaseAll(indiceItems);
sbuf.flip();
@ -109,11 +105,8 @@ public class MeshLayer extends RenderElement {
@Override
protected void clear() {
indicesVbo = BufferObject.release(indicesVbo);
VertexItem.pool.releaseAll(indiceItems);
VertexItem.pool.releaseAll(vertexItems);
indiceItems = null;
vertexItems = null;
indiceItems = VertexItem.pool.releaseAll(indiceItems);
vertexItems = VertexItem.pool.releaseAll(vertexItems);
}
public static class Renderer {

View File

@ -174,10 +174,9 @@ public final class SymbolLayer extends TextureLayer {
if (pos > 0)
sbuf.put(buf, 0, pos);
VertexItem.pool.release(si);
si = VertexItem.pool.release(si);
TextureItem.releaseAll(prevTextures);
prevTextures = null;
prevTextures = TextureItem.pool.releaseAll(prevTextures);
}
private TextureItem getTexture(Bitmap bitmap) {
@ -195,19 +194,16 @@ public final class SymbolLayer extends TextureLayer {
}
public void clearItems() {
SymbolItem.pool.releaseAll(symbols);
symbols = null;
symbols = SymbolItem.pool.releaseAll(symbols);
verticesCnt = 0;
}
@Override
public void clear() {
TextureItem.releaseAll(textures);
SymbolItem.pool.releaseAll(symbols);
textures = TextureItem.pool.releaseAll(textures);
symbols = SymbolItem.pool.releaseAll(symbols);
vertexItems = VertexItem.pool.releaseAll(vertexItems);
textures = null;
symbols = null;
vertexItems = null;
verticesCnt = 0;
}

View File

@ -268,18 +268,13 @@ public final class TextLayer extends TextureLayer {
@Override
public void clear() {
TextureItem.releaseAll(textures);
labels = null;
TextItem.pool.releaseAll(labels);
VertexItem.pool.releaseAll(vertexItems);
textures = null;
vertexItems = null;
textures = TextureItem.pool.releaseAll(textures);
labels = TextItem.pool.releaseAll(labels);
vertexItems = VertexItem.pool.releaseAll(vertexItems);
verticesCnt = 0;
}
public void clearLabels() {
TextItem.pool.releaseAll(labels);
labels = null;
labels = TextItem.pool.releaseAll(labels);
}
}

View File

@ -105,10 +105,6 @@ public class TextureItem extends Inlist<TextureItem> {
}
}
public synchronized static void releaseAll(TextureItem ti) {
pool.releaseAll(ti);
}
/**
* Retrieve a TextureItem from pool with default Bitmap with dimension
* TextureRenderer.TEXTURE_WIDTH/HEIGHT.
@ -191,7 +187,7 @@ public class TextureItem extends Inlist<TextureItem> {
}
};
private final static SyncPool<TextureItem> pool = new TextureItemPool();
public final static SyncPool<TextureItem> pool = new TextureItemPool();
private final static ArrayList<Integer> mTextures = new ArrayList<Integer>();
private final static ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(10);

View File

@ -35,17 +35,6 @@ public class VertexItem extends Inlist<VertexItem> {
}
};
/**
* Add VertexItems back to pool. Make sure to not use the reference
* afterwards!
* i.e.:
* vertexItem.release();
* vertexItem = null;
* */
public void release() {
VertexItem.pool.releaseAll(this);
}
public int getSize() {
int size = used;
for (VertexItem it = next; it != null; it = it.next)

View File

@ -106,7 +106,7 @@ public class AtlasRenderLayer extends ElementRenderer {
}
tl.prepare();
TextItem.pool.releaseAll(tl.labels);
tl.labels = TextItem.pool.releaseAll(tl.labels);
}
boolean initial = true;

View File

@ -225,14 +225,11 @@ public class MapTile extends Tile {
// TODO move this to layers clear
layers.vbo = BufferObject.release(layers.vbo);
layers.clear();
layers = null;
}
TextItem.pool.releaseAll(labels);
SymbolItem.pool.releaseAll(symbols);
layers = null;
labels = null;
symbols = null;
labels = TextItem.pool.releaseAll(labels);
symbols = SymbolItem.pool.releaseAll(symbols);
state = STATE_NONE;
}

View File

@ -18,6 +18,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.oscim.backend.Log;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
@ -27,8 +28,6 @@ import org.oscim.tiling.source.common.PbfDecoder;
import org.oscim.utils.pool.Inlist;
import org.oscim.utils.pool.Pool;
import org.oscim.backend.Log;
public class TileDecoder extends PbfDecoder {
private final static String TAG = TileDecoder.class.getName();
@ -236,7 +235,7 @@ public class TileDecoder extends PbfDecoder {
// FIXME extract layer tag here
f.elem.setLayer(5);
mMapDataCallback.process(f.elem);
mFeaturePool.release(f);
f = mFeaturePool.release(f);
}
return true;

View File

@ -14,6 +14,8 @@
*/
package org.oscim.utils.pool;
import javax.annotation.CheckReturnValue;
/**
* Utility class for making poolable objects.
* Instead of using an additional list to hold pool items just extend this
@ -33,6 +35,7 @@ public class Inlist<T extends Inlist<T>> {
* @param item the item
* @return the new head of 'list' (item)
*/
@CheckReturnValue
public static <T extends Inlist<T>> T push(T list, T item) {
item.next = list;
return item;
@ -58,6 +61,7 @@ public class Inlist<T extends Inlist<T>> {
* @param item the item
* @return the new head of 'list'
*/
@CheckReturnValue
public static <T extends Inlist<T>> T remove(T list, T item) {
if (item == list) {
T head = item.next;
@ -84,6 +88,7 @@ public class Inlist<T extends Inlist<T>> {
* @param i the index
* @return the item or null
*/
@CheckReturnValue
public static <T extends Inlist<T>> T get(T list, int i) {
if (i < 0)
return null;
@ -105,6 +110,7 @@ public class Inlist<T extends Inlist<T>> {
* @param item the item
* @return the new head of 'list'
*/
@CheckReturnValue
public static <T extends Inlist<T>> T appendItem(T list, T item) {
if (item.next != null)
@ -130,6 +136,7 @@ public class Inlist<T extends Inlist<T>> {
* @param other the other
* @return the head of 'list'
*/
@CheckReturnValue
public static <T extends Inlist<T>> T appendList(T list, T other) {
if (list == null)
@ -157,6 +164,7 @@ public class Inlist<T extends Inlist<T>> {
* @param list the list
* @return the last item
*/
@CheckReturnValue
public static <T extends Inlist<T>> T last(T list) {
while (list != null) {
if (list.next == null)
@ -174,6 +182,7 @@ public class Inlist<T extends Inlist<T>> {
* @param other the other list
* @return the new head of list
*/
@CheckReturnValue
public static <T extends Inlist<T>> T prependRelative(T list, T item, T other) {
if (item.next != null)

View File

@ -14,6 +14,8 @@
*/
package org.oscim.utils.pool;
import javax.annotation.CheckReturnValue;
public abstract class Pool<T extends Inlist<T>> {
protected T pool;
@ -27,33 +29,48 @@ public abstract class Pool<T extends Inlist<T>> {
return true;
}
// release 'item' to pool.
// make sure that item is not in any other Inlist!
public void release(T item) {
/**
* Release 'item' to pool.
* <p>
* Usage item = pool.release(item), to ensure to not keep a reference to
* item!
*/
@CheckReturnValue
public T release(T item) {
if (item == null)
return;
return null;
if (!clearItem(item))
return;
return null;
item.next = pool;
pool = item;
return null;
}
public void releaseAll(T item) {
if (item == null)
return;
/**
* Release 'list' to pool.
* <p>
* Usage list = pool.releaseAll(list), to ensure to not keep a reference to
* list!
*/
@CheckReturnValue
public T releaseAll(T list) {
if (list == null)
return null;
while (item != null) {
T next = item.next;
while (list != null) {
T next = list.next;
clearItem(item);
clearItem(list);
item.next = pool;
pool = item;
list.next = pool;
pool = list;
item = next;
list = next;
}
return null;
}
// remove 'item' from 'list' and add back to pool

View File

@ -14,6 +14,8 @@
*/
package org.oscim.utils.pool;
import javax.annotation.CheckReturnValue;
public abstract class SyncPool<T extends Inlist<T>> {
protected final int maxFill;
protected int fill;
@ -66,14 +68,21 @@ public abstract class SyncPool<T extends Inlist<T>> {
*/
protected abstract T createItem();
public void release(T item) {
/**
* Release 'item' to pool.
* <p>
* Usage item = pool.release(item), to ensure to not keep a reference to
* item!
*/
@CheckReturnValue
public T release(T item) {
if (item == null)
return;
return null;
if (!clearItem(item)) {
// dont add back to pool
freeItem(item);
return;
return null;
}
if (fill < maxFill) {
synchronized (this) {
@ -85,17 +94,19 @@ public abstract class SyncPool<T extends Inlist<T>> {
} else {
freeItem(item);
}
return null;
}
/**
* Release all items from 'item'. Do not use the
* 'item' reference afterwards!
*
* @param item the item (or list or items)
* Release 'list' to pool.
* <p>
* Usage list = pool.releaseAll(list), to ensure to not keep a reference to
* list!
*/
public void releaseAll(T item) {
@CheckReturnValue
public T releaseAll(T item) {
if (item == null)
return;
return null;
if (fill > maxFill) {
while (item != null) {
@ -103,7 +114,7 @@ public abstract class SyncPool<T extends Inlist<T>> {
freeItem(item);
item = item.next;
}
return;
return null;
}
synchronized (this) {
@ -125,6 +136,7 @@ public abstract class SyncPool<T extends Inlist<T>> {
item = next;
}
}
return null;
}
/**