Chromium Code Reviews| Index: base/android/java/src/org/chromium/base/ReferencePool.java |
| diff --git a/base/android/java/src/org/chromium/base/ReferencePool.java b/base/android/java/src/org/chromium/base/ReferencePool.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..37bd8b529527455a33757e0d6c5cdd50a62fce7d |
| --- /dev/null |
| +++ b/base/android/java/src/org/chromium/base/ReferencePool.java |
| @@ -0,0 +1,69 @@ |
| +// 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.lang.ref.WeakReference; |
| +import java.util.WeakHashMap; |
| + |
| +/** |
| + * A ReferencePool 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 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 |
| + * (or even strong) references could lead to leaks with a ReferencePool: For example, if a |
| + * {@link Reference} is held by its own payload (including indirectly) but is otherwise not |
| + * referenced, it will not get garbage-collected, because the ReferencePool will still hold a strong |
| + * reference to the payload. |
| + */ |
| +public class ReferencePool { |
|
rmcilroy
2017/05/10 14:38:11
nit - not sure about the name. How about Discardab
Bernhard Bauer
2017/05/10 17:47:50
Good idea! Done. I kept variable and member names
|
| + /** |
| + * The underlying data storage. The wildcard type parameter allows using a single pool for |
| + * references of any type. |
| + */ |
| + private final WeakHashMap<Reference<?>, Object> mPool = new WeakHashMap<>(); |
| + |
| + /** |
| + * A reference to an object in the pool. Will be nulled out when the pool has been drained. |
| + * @param <T> The type of the object. |
| + */ |
| + public class Reference<T> { |
|
rmcilroy
2017/05/10 14:38:11
nit - DiscardableReference to make it clear it cou
Bernhard Bauer
2017/05/10 17:47:50
Done.
|
| + /** |
| + * @return The referent, or null if the pool has been drained. |
| + */ |
| + @SuppressWarnings("unchecked") |
| + @Nullable |
| + public T get() { |
| + return (T) mPool.get(this); |
| + } |
| + } |
| + |
| + /** |
| + * @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> Reference<T> put(T payload) { |
| + assert payload != null; |
| + Reference<T> reference = new Reference<>(); |
| + mPool.put(reference, payload); |
| + return reference; |
| + } |
| + |
| + /** |
| + * Drains the pool, removing all references to objects in the pool and therefore allowing them |
| + * to be garbage collected. |
| + */ |
| + public void drain() { |
| + mPool.clear(); |
| + } |
| +} |