Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. public class WeakFileReference extends WeakReference<File> {
  2.  
  3. private final File handle;
  4. public WeakFileReference(File handle, ReferenceQueue q) {
  5. super(handle, q);
  6. this.handle = (File)handle.clone();
  7. }
  8. }
  9.  
  10. /*
  11. * Copyright 2013 The Netty Project
  12. *
  13. * The Netty Project licenses this file to you under the Apache License,
  14. * version 2.0 (the "License"); you may not use this file except in compliance
  15. * with the License. You may obtain a copy of the License at:
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  22. * License for the specific language governing permissions and limitations
  23. * under the License.
  24. */
  25. package io.netty.util;
  26.  
  27. /**
  28. * A reference-counted object that requires explicit deallocation.
  29. * <p>
  30. * When a new {@link ReferenceCounted} is instantiated, it starts with the reference count of {@code 1}.
  31. * {@link #retain()} increases the reference count, and {@link #release()} decreases the reference count.
  32. * If the reference count is decreased to {@code 0}, the object will be deallocated explicitly, and accessing
  33. * the deallocated object will usually result in an access violation.
  34. * </p>
  35. * <p>
  36. * If an object that implements {@link ReferenceCounted} is a container of other objects that implement
  37. * {@link ReferenceCounted}, the contained objects will also be released via {@link #release()} when the container's
  38. * reference count becomes 0.
  39. * </p>
  40. */
  41. public interface ReferenceCounted {
  42. /**
  43. * Returns the reference count of this object. If {@code 0}, it means this object has been deallocated.
  44. */
  45. int refCnt();
  46.  
  47. /**
  48. * Increases the reference count by {@code 1}.
  49. */
  50. ReferenceCounted retain();
  51.  
  52. /**
  53. * Increases the reference count by the specified {@code increment}.
  54. */
  55. ReferenceCounted retain(int increment);
  56.  
  57. /**
  58. * Records the current access location of this object for debugging purposes.
  59. * If this object is determined to be leaked, the information recorded by this operation will be provided to you
  60. * via {@link ResourceLeakDetector}. This method is a shortcut to {@link #touch(Object) touch(null)}.
  61. */
  62. ReferenceCounted touch();
  63.  
  64. /**
  65. * Records the current access location of this object with an additional arbitrary information for debugging
  66. * purposes. If this object is determined to be leaked, the information recorded by this operation will be
  67. * provided to you via {@link ResourceLeakDetector}.
  68. */
  69. ReferenceCounted touch(Object hint);
  70.  
  71. /**
  72. * Decreases the reference count by {@code 1} and deallocates this object if the reference count reaches at
  73. * {@code 0}.
  74. *
  75. * @return {@code true} if and only if the reference count became {@code 0} and this object has been deallocated
  76. */
  77. boolean release();
  78.  
  79. /**
  80. * Decreases the reference count by the specified {@code decrement} and deallocates this object if the reference
  81. * count reaches at {@code 0}.
  82. *
  83. * @return {@code true} if and only if the reference count became {@code 0} and this object has been deallocated
  84. */
  85. boolean release(int decrement);
  86. }
  87.  
  88. /*
  89. * Copyright 2013 The Netty Project
  90. *
  91. * The Netty Project licenses this file to you under the Apache License,
  92. * version 2.0 (the "License"); you may not use this file except in compliance
  93. * with the License. You may obtain a copy of the License at:
  94. *
  95. * http://www.apache.org/licenses/LICENSE-2.0
  96. *
  97. * Unless required by applicable law or agreed to in writing, software
  98. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  99. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  100. * License for the specific language governing permissions and limitations
  101. * under the License.
  102. */
  103. package io.netty.util;
  104.  
  105. import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
  106.  
  107. import static io.netty.util.internal.ObjectUtil.checkPositive;
  108.  
  109. /**
  110. * Abstract base class for classes wants to implement {@link ReferenceCounted}.
  111. */
  112. public abstract class AbstractReferenceCounted implements ReferenceCounted {
  113.  
  114. private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater =
  115. AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class, "refCnt");
  116.  
  117. private volatile int refCnt = 1;
  118.  
  119. @Override
  120. public final int refCnt() {
  121. return refCnt;
  122. }
  123.  
  124. /**
  125. * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
  126. */
  127. protected final void setRefCnt(int refCnt) {
  128. refCntUpdater.set(this, refCnt);
  129. }
  130.  
  131. @Override
  132. public ReferenceCounted retain() {
  133. return retain0(1);
  134. }
  135.  
  136. @Override
  137. public ReferenceCounted retain(int increment) {
  138. return retain0(checkPositive(increment, "increment"));
  139. }
  140.  
  141. private ReferenceCounted retain0(int increment) {
  142. int oldRef = refCntUpdater.getAndAdd(this, increment);
  143. if (oldRef <= 0 || oldRef + increment < oldRef) {
  144. // Ensure we don't resurrect (which means the refCnt was 0) and also that we encountered an overflow.
  145. refCntUpdater.getAndAdd(this, -increment);
  146. throw new IllegalReferenceCountException(oldRef, increment);
  147. }
  148. return this;
  149. }
  150.  
  151. @Override
  152. public ReferenceCounted touch() {
  153. return touch(null);
  154. }
  155.  
  156. @Override
  157. public boolean release() {
  158. return release0(1);
  159. }
  160.  
  161. @Override
  162. public boolean release(int decrement) {
  163. return release0(checkPositive(decrement, "decrement"));
  164. }
  165.  
  166. private boolean release0(int decrement) {
  167. int oldRef = refCntUpdater.getAndAdd(this, -decrement);
  168. if (oldRef == decrement) {
  169. deallocate();
  170. return true;
  171. } else if (oldRef < decrement || oldRef - decrement > oldRef) {
  172. // Ensure we don't over-release, and avoid underflow.
  173. refCntUpdater.getAndAdd(this, decrement);
  174. throw new IllegalReferenceCountException(oldRef, -decrement);
  175. }
  176. return false;
  177. }
  178.  
  179. /**
  180. * Called once {@link #refCnt()} is equals 0.
  181. */
  182. protected abstract void deallocate();
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement