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 * https://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.internal.RefCnt;
20
21 /**
22 * Abstract base class for {@link ByteBuf} implementations that count references.
23 */
24 public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
25
26 // this is setting the ref cnt to the initial value
27 private final RefCnt refCnt = new RefCnt();
28
29 protected AbstractReferenceCountedByteBuf(int maxCapacity) {
30 super(maxCapacity);
31 }
32
33 @Override
34 boolean isAccessible() {
35 // Try to do non-volatile read for performance as the ensureAccessible() is racy anyway and only provide
36 // a best-effort guard.
37 return RefCnt.isLiveNonVolatile(refCnt);
38 }
39
40 @Override
41 public int refCnt() {
42 return RefCnt.refCnt(refCnt);
43 }
44
45 /**
46 * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
47 */
48 protected final void setRefCnt(int count) {
49 RefCnt.setRefCnt(refCnt, count);
50 }
51
52 /**
53 * An unsafe operation intended for use by a subclass that resets the reference count of the buffer to 1
54 */
55 protected final void resetRefCnt() {
56 RefCnt.resetRefCnt(refCnt);
57 }
58
59 @Override
60 public ByteBuf retain() {
61 RefCnt.retain(refCnt);
62 return this;
63 }
64
65 @Override
66 public ByteBuf retain(int increment) {
67 RefCnt.retain(refCnt, increment);
68 return this;
69 }
70
71 @Override
72 public ByteBuf touch() {
73 return this;
74 }
75
76 @Override
77 public ByteBuf touch(Object hint) {
78 return this;
79 }
80
81 @Override
82 public boolean release() {
83 return handleRelease(RefCnt.release(refCnt));
84 }
85
86 @Override
87 public boolean release(int decrement) {
88 return handleRelease(RefCnt.release(refCnt, decrement));
89 }
90
91 private boolean handleRelease(boolean result) {
92 if (result) {
93 deallocate();
94 }
95 return result;
96 }
97
98 /**
99 * Called once {@link #refCnt()} is equals 0.
100 */
101 protected abstract void deallocate();
102 }