View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.buffer;
18  
19  import io.netty.util.IllegalReferenceCountException;
20  import io.netty.util.internal.PlatformDependent;
21  
22  import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
23  
24  import static io.netty.util.internal.ObjectUtil.checkPositive;
25  
26  /**
27   * Abstract base class for {@link ByteBuf} implementations that count references.
28   */
29  public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
30      private static final long REFCNT_FIELD_OFFSET;
31      private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> refCntUpdater =
32              AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
33  
34      private volatile int refCnt = 1;
35  
36      static {
37          long refCntFieldOffset = -1;
38          try {
39              if (PlatformDependent.hasUnsafe()) {
40                  refCntFieldOffset = PlatformDependent.objectFieldOffset(
41                          AbstractReferenceCountedByteBuf.class.getDeclaredField("refCnt"));
42              }
43          } catch (Throwable ignore) {
44              refCntFieldOffset = -1;
45          }
46  
47          REFCNT_FIELD_OFFSET = refCntFieldOffset;
48      }
49  
50      protected AbstractReferenceCountedByteBuf(int maxCapacity) {
51          super(maxCapacity);
52      }
53  
54      @Override
55      int internalRefCnt() {
56          // Try to do non-volatile read for performance as the ensureAccessible() is racy anyway and only provide
57          // a best-effort guard.
58          //
59          // TODO: Once we compile against later versions of Java we can replace the Unsafe usage here by varhandles.
60          return REFCNT_FIELD_OFFSET != -1 ? PlatformDependent.getInt(this, REFCNT_FIELD_OFFSET) : refCnt();
61      }
62  
63      @Override
64      public int refCnt() {
65          return refCnt;
66      }
67  
68      /**
69       * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
70       */
71      protected final void setRefCnt(int refCnt) {
72          refCntUpdater.set(this, refCnt);
73      }
74  
75      @Override
76      public ByteBuf retain() {
77          return retain0(1);
78      }
79  
80      @Override
81      public ByteBuf retain(int increment) {
82          return retain0(checkPositive(increment, "increment"));
83      }
84  
85      private ByteBuf retain0(final int increment) {
86          int oldRef = refCntUpdater.getAndAdd(this, increment);
87          if (oldRef <= 0 || oldRef + increment < oldRef) {
88              // Ensure we don't resurrect (which means the refCnt was 0) and also that we encountered an overflow.
89              refCntUpdater.getAndAdd(this, -increment);
90              throw new IllegalReferenceCountException(oldRef, increment);
91          }
92          return this;
93      }
94  
95      @Override
96      public ByteBuf touch() {
97          return this;
98      }
99  
100     @Override
101     public ByteBuf touch(Object hint) {
102         return this;
103     }
104 
105     @Override
106     public boolean release() {
107         return release0(1);
108     }
109 
110     @Override
111     public boolean release(int decrement) {
112         return release0(checkPositive(decrement, "decrement"));
113     }
114 
115     private boolean release0(int decrement) {
116         int oldRef = refCntUpdater.getAndAdd(this, -decrement);
117         if (oldRef == decrement) {
118             deallocate();
119             return true;
120         }
121         if (oldRef < decrement || oldRef - decrement > oldRef) {
122             // Ensure we don't over-release, and avoid underflow.
123             refCntUpdater.getAndAdd(this, decrement);
124             throw new IllegalReferenceCountException(oldRef, -decrement);
125         }
126         return false;
127     }
128     /**
129      * Called once {@link #refCnt()} is equals 0.
130      */
131     protected abstract void deallocate();
132 }