limit TextItem pool items

This commit is contained in:
Hannes Janetzek 2013-01-30 04:14:24 +01:00
parent d61f85ef86
commit bde5eff447

View File

@ -16,15 +16,25 @@ package org.oscim.renderer.layer;
import org.oscim.theme.renderinstruction.Text; import org.oscim.theme.renderinstruction.Text;
import android.util.Log;
public class TextItem { public class TextItem {
private final static String TAG = TextItem.class.getName();
private final static int MAX_POOL = 250;
private static Object lock = new Object(); private static Object lock = new Object();
private static TextItem pool; private static TextItem pool;
private static int count = 0;
private static int inPool = 0;
public static TextItem get() { public static TextItem get() {
synchronized (lock) { synchronized (lock) {
if (pool == null) if (pool == null) {
count++;
return new TextItem(); return new TextItem();
}
inPool--;
TextItem ti = pool; TextItem ti = pool;
pool = pool.next; pool = pool.next;
@ -34,29 +44,43 @@ public class TextItem {
} }
} }
public static void append(TextItem ti, TextItem in) { // public static void append(TextItem ti, TextItem in) {
if (ti == null) // if (ti == null)
return; // return;
if (ti.next == null) { // if (ti.next == null) {
in.next = ti.next; // in.next = ti.next;
ti.next = in; // ti.next = in;
} // }
TextItem t = ti; //
while (t.next != null) // TextItem t = ti;
t = t.next; // while (t.next != null)
// t = t.next;
in.next = t.next; //
t.next = in; // in.next = t.next;
} // t.next = in;
// }
public static void release(TextItem ti) { public static void release(TextItem ti) {
if (ti == null) if (ti == null)
return; return;
synchronized (lock) { if (inPool > MAX_POOL) {
while (ti != null) { while (ti != null) {
TextItem next = ti.next; TextItem next = ti.next;
// drop references
ti.string = null;
ti.text = null;
ti.n1 = null;
ti.n2 = null;
ti = next;
count--;
}
}
synchronized (lock) {
while (ti != null) {
TextItem next = ti.next;
ti.next = pool; ti.next = pool;
// drop references // drop references
@ -68,10 +92,16 @@ public class TextItem {
pool = ti; pool = ti;
ti = next; ti = next;
inPool++;
} }
} }
} }
public static void printPool() {
Log.d(TAG, "in pool " + inPool + " / " + count);
}
public TextItem set(float x, float y, String string, Text text) { public TextItem set(float x, float y, String string, Text text) {
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -140,6 +170,9 @@ public class TextItem {
// link to next/prev label of the way // link to next/prev label of the way
public TextItem n1; public TextItem n1;
public TextItem n2; public TextItem n2;
public byte origin;
public boolean active; public boolean active;
// public byte placement // public byte placement
} }