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 package io.netty.util;
17
18 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19
20 import io.netty.util.internal.ReferenceCountUpdater;
21
22 /**
23  * Abstract base class for classes wants to implement {@link ReferenceCounted}.
24  */

25 public abstract class AbstractReferenceCounted implements ReferenceCounted {
26     private static final long REFCNT_FIELD_OFFSET =
27             ReferenceCountUpdater.getUnsafeOffset(AbstractReferenceCounted.class"refCnt");
28     private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> AIF_UPDATER =
29             AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class"refCnt");
30
31     private static final ReferenceCountUpdater<AbstractReferenceCounted> updater =
32             new ReferenceCountUpdater<AbstractReferenceCounted>() {
33         @Override
34         protected AtomicIntegerFieldUpdater<AbstractReferenceCounted> updater() {
35             return AIF_UPDATER;
36         }
37         @Override
38         protected long unsafeOffset() {
39             return REFCNT_FIELD_OFFSET;
40         }
41     };
42
43     // Value might not equal "real" reference count, all access should be via the updater
44     @SuppressWarnings("unused")
45     private volatile int refCnt = updater.initialValue();
46
47     @Override
48     public int refCnt() {
49         return updater.refCnt(this);
50     }
51
52     /**
53      * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
54      */

55     protected final void setRefCnt(int refCnt) {
56         updater.setRefCnt(this, refCnt);
57     }
58
59     @Override
60     public ReferenceCounted retain() {
61         return updater.retain(this);
62     }
63
64     @Override
65     public ReferenceCounted retain(int increment) {
66         return updater.retain(this, increment);
67     }
68
69     @Override
70     public ReferenceCounted touch() {
71         return touch(null);
72     }
73
74     @Override
75     public boolean release() {
76         return handleRelease(updater.release(this));
77     }
78
79     @Override
80     public boolean release(int decrement) {
81         return handleRelease(updater.release(this, decrement));
82     }
83
84     private boolean handleRelease(boolean result) {
85         if (result) {
86             deallocate();
87         }
88         return result;
89     }
90
91     /**
92      * Called once {@link #refCnt()} is equals 0.
93      */

94     protected abstract void deallocate();
95 }
96