Chromium Code Reviews| Index: base/android/java/src/org/chromium/base/DiscardableReferencePool.java |
| diff --git a/base/android/java/src/org/chromium/base/DiscardableReferencePool.java b/base/android/java/src/org/chromium/base/DiscardableReferencePool.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a63f60da2cc510d152152fc08775710ebb31fab6 |
| --- /dev/null |
| +++ b/base/android/java/src/org/chromium/base/DiscardableReferencePool.java |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.base; |
| + |
| +import android.support.annotation.Nullable; |
| + |
| +import java.util.Collections; |
| +import java.util.Set; |
| +import java.util.WeakHashMap; |
| + |
| +/** |
| + * A DiscardableReferencePool allows handing out typed references to objects ("payloads") that can |
| + * be dropped in one batch ("drained"), e.g. under memory pressure. In contrast to {@link |
| + * java.lang.ref.WeakReference}s, which drop their referents when they get garbage collected, a |
| + * reference pool gives more precise control over when exactly it is drained. |
| + * |
| + * <p>Internally it uses a {@link WeakHashMap} with the reference itself as a key to allow the |
| + * payloads to be garbage collected regularly when the last reference goes away before the pool is |
| + * drained. |
| + * |
| + * <p>Note that certain kinds of reference cycles that would be garbage-collected when using weak |
|
DmitrySkiba
2017/05/10 22:04:19
This is not accurate anymore, right? Since now the
Bernhard Bauer
2017/05/11 08:43:02
True. Removed (and added a comment about thread sa
|
| + * (or even strong) references could lead to leaks with a DiscardableReferencePool: For example, if |
| + * a {@link DiscardableReference} is held by its own payload (including indirectly) but is otherwise |
| + * not referenced, it will not get garbage-collected, because the DiscardableReferencePool will |
| + * still hold a strong reference to the payload. |
| + */ |
| +public class DiscardableReferencePool { |
| + /** |
| + * The underlying data storage. The wildcard type parameter allows using a single pool for |
| + * references of any type. |
| + */ |
| + private final Set<DiscardableReference<?>> mPool; |
| + |
| + public DiscardableReferencePool() { |
| + WeakHashMap<DiscardableReference<?>, Boolean> map = new WeakHashMap<>(); |
| + mPool = Collections.newSetFromMap(map); |
| + } |
| + |
| + /** |
| + * A reference to an object in the pool. Will be nulled out when the pool is drained. |
| + * @param <T> The type of the object. |
| + */ |
| + public static class DiscardableReference<T> { |
| + @Nullable |
| + private T mPayload; |
| + |
| + private DiscardableReference(T payload) { |
| + assert payload != null; |
| + mPayload = payload; |
| + } |
| + |
| + /** |
| + * @return The referent, or null if the pool has been drained. |
| + */ |
| + @Nullable |
| + public T get() { |
| + return mPayload; |
| + } |
| + |
| + /** |
| + * Clear the referent. |
| + */ |
| + private void discard() { |
| + assert mPayload != null; |
| + mPayload = null; |
| + } |
| + } |
| + |
| + /** |
| + * @param <T> The type of the object. |
| + * @param payload The payload to add to the pool. |
| + * @return A new reference to the {@code payload}. |
| + */ |
| + public <T> DiscardableReference<T> put(T payload) { |
| + assert payload != null; |
| + DiscardableReference<T> reference = new DiscardableReference<>(payload); |
| + mPool.add(reference); |
| + return reference; |
| + } |
| + |
| + /** |
| + * Drains the pool, removing all references to objects in the pool and therefore allowing them |
| + * to be garbage collected. |
| + */ |
| + public void drain() { |
| + for (DiscardableReference<?> ref : mPool) { |
| + ref.discard(); |
| + } |
| + mPool.clear(); |
| + } |
| +} |