1 // Copyright (c) 2016, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
11 * Offers functionality for implementations of
12 * {@link AbstractNativeReference} which have an immutable reference to the
13 * underlying native C++ object
16 public abstract class AbstractImmutableNativeReference
17 extends AbstractNativeReference
{
20 * A flag indicating whether the current {@code AbstractNativeReference} is
21 * responsible to free the underlying C++ object
23 protected final AtomicBoolean owningHandle_
;
25 protected AbstractImmutableNativeReference(final boolean owningHandle
) {
26 this.owningHandle_
= new AtomicBoolean(owningHandle
);
30 public boolean isOwningHandle() {
31 return owningHandle_
.get();
35 * Releases this {@code AbstractNativeReference} from the responsibility of
36 * freeing the underlying native C++ object
38 * This will prevent the object from attempting to delete the underlying
39 * native object in its finalizer. This must be used when another object
40 * takes over ownership of the native object or both will attempt to delete
41 * the underlying object when garbage collected.
43 * When {@code disOwnNativeHandle()} is called, {@code dispose()} will
44 * subsequently take no action. As a result, incorrect use of this function
45 * may cause a memory leak.
50 protected final void disOwnNativeHandle() {
51 owningHandle_
.set(false);
56 if (owningHandle_
.compareAndSet(true, false)) {
62 * The helper function of {@link AbstractImmutableNativeReference#dispose()}
63 * which all subclasses of {@code AbstractImmutableNativeReference} must
64 * implement to release their underlying native C++ objects.
66 protected abstract void disposeInternal();