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 java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
20
21 import io.netty.util.internal.ReferenceCountUpdater;
22
23 /**
24  * Abstract base class for {@link ByteBuf} implementations that count references.
25  */

26 public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
27     private static final long REFCNT_FIELD_OFFSET =
28             ReferenceCountUpdater.getUnsafeOffset(AbstractReferenceCountedByteBuf.class"refCnt");
29     private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> AIF_UPDATER =
30             AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCountedByteBuf.class"refCnt");
31
32     private static final ReferenceCountUpdater<AbstractReferenceCountedByteBuf> updater =
33             new ReferenceCountUpdater<AbstractReferenceCountedByteBuf>() {
34         @Override
35         protected AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> updater() {
36             return AIF_UPDATER;
37         }
38         @Override
39         protected long unsafeOffset() {
40             return REFCNT_FIELD_OFFSET;
41         }
42     };
43
44     // Value might not equal "real" reference count, all access should be via the updater
45     @SuppressWarnings("unused")
46     private volatile int refCnt = updater.initialValue();
47
48     protected AbstractReferenceCountedByteBuf(int maxCapacity) {
49         super(maxCapacity);
50     }
51
52     @Override
53     boolean isAccessible() {
54         // Try to do non-volatile read for performance as the ensureAccessible() is racy anyway and only provide
55         // a best-effort guard.
56         return updater.isLiveNonVolatile(this);
57     }
58
59     @Override
60     public int refCnt() {
61         return updater.refCnt(this);
62     }
63
64     /**
65      * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
66      */

67     protected final void setRefCnt(int refCnt) {
68         updater.setRefCnt(this, refCnt);
69     }
70
71     /**
72      * An unsafe operation intended for use by a subclass that resets the reference count of the buffer to 1
73      */

74     protected final void resetRefCnt() {
75         updater.resetRefCnt(this);
76     }
77
78     @Override
79     public ByteBuf retain() {
80         return updater.retain(this);
81     }
82
83     @Override
84     public ByteBuf retain(int increment) {
85         return updater.retain(this, increment);
86     }
87
88     @Override
89     public ByteBuf touch() {
90         return this;
91     }
92
93     @Override
94     public ByteBuf touch(Object hint) {
95         return this;
96     }
97
98     @Override
99     public boolean release() {
100         return handleRelease(updater.release(this));
101     }
102
103     @Override
104     public boolean release(int decrement) {
105         return handleRelease(updater.release(this, decrement));
106     }
107
108     private boolean handleRelease(boolean result) {
109         if (result) {
110             deallocate();
111         }
112         return result;
113     }
114
115     /**
116      * Called once {@link #refCnt()} is equals 0.
117      */

118     protected abstract void deallocate();
119 }
120