Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.base; | |
| 6 | |
| 7 import android.support.annotation.Nullable; | |
| 8 | |
| 9 import java.lang.ref.WeakReference; | |
| 10 import java.util.WeakHashMap; | |
| 11 | |
| 12 /** | |
| 13 * A ReferencePool allows handing out typed references to objects ("payloads") t hat can be dropped | |
| 14 * in one batch ("drained"), e.g. under memory pressure. In contrast to {@link W eakReference}s, | |
| 15 * which drop their referents when they get garbage collected, a reference pool gives more precise | |
| 16 * control over when exactly it is drained. | |
| 17 * | |
| 18 * <p>Internally it uses a {@link WeakHashMap} with the reference itself as a ke y to allow the | |
| 19 * payloads to be garbage collected regularly when the last reference goes away before the pool is | |
| 20 * drained. | |
| 21 * | |
| 22 * <p>Note that certain kinds of reference cycles that would be garbage-collecte d when using weak | |
| 23 * (or even strong) references could lead to leaks with a ReferencePool: For exa mple, if a | |
| 24 * {@link Reference} is held by its own payload (including indirectly) but is ot herwise not | |
| 25 * referenced, it will not get garbage-collected, because the ReferencePool will still hold a strong | |
| 26 * reference to the payload. | |
| 27 */ | |
| 28 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
| |
| 29 /** | |
| 30 * The underlying data storage. The wildcard type parameter allows using a s ingle pool for | |
| 31 * references of any type. | |
| 32 */ | |
| 33 private final WeakHashMap<Reference<?>, Object> mPool = new WeakHashMap<>(); | |
| 34 | |
| 35 /** | |
| 36 * A reference to an object in the pool. Will be nulled out when the pool ha s been drained. | |
| 37 * @param <T> The type of the object. | |
| 38 */ | |
| 39 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.
| |
| 40 /** | |
| 41 * @return The referent, or null if the pool has been drained. | |
| 42 */ | |
| 43 @SuppressWarnings("unchecked") | |
| 44 @Nullable | |
| 45 public T get() { | |
| 46 return (T) mPool.get(this); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * @param <T> The type of the object. | |
| 52 * @param payload The payload to add to the pool. | |
| 53 * @return A new reference to the {@code payload}. | |
| 54 */ | |
| 55 public <T> Reference<T> put(T payload) { | |
| 56 assert payload != null; | |
| 57 Reference<T> reference = new Reference<>(); | |
| 58 mPool.put(reference, payload); | |
| 59 return reference; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Drains the pool, removing all references to objects in the pool and there fore allowing them | |
| 64 * to be garbage collected. | |
| 65 */ | |
| 66 public void drain() { | |
| 67 mPool.clear(); | |
| 68 } | |
| 69 } | |
| OLD | NEW |