001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.nio; 019 020import org.apache.hadoop.hbase.io.ByteBuffAllocator; 021import org.apache.hadoop.hbase.io.ByteBuffAllocator.Recycler; 022import org.apache.yetus.audience.InterfaceAudience; 023 024import org.apache.hbase.thirdparty.io.netty.util.AbstractReferenceCounted; 025import org.apache.hbase.thirdparty.io.netty.util.ReferenceCounted; 026 027/** 028 * Maintain an reference count integer inside to track life cycle of {@link ByteBuff}, if the 029 * reference count become 0, it'll call {@link Recycler#free()} exactly once. 030 */ 031@InterfaceAudience.Private 032public class RefCnt extends AbstractReferenceCounted { 033 034 private Recycler recycler = ByteBuffAllocator.NONE; 035 036 /** 037 * Create an {@link RefCnt} with an initial reference count = 1. If the reference count become 038 * zero, the recycler will do nothing. Usually, an Heap {@link ByteBuff} will use this kind of 039 * refCnt to track its life cycle, it help to abstract the code path although it's not really 040 * needed to track on heap ByteBuff. 041 */ 042 public static RefCnt create() { 043 return new RefCnt(ByteBuffAllocator.NONE); 044 } 045 046 public static RefCnt create(Recycler recycler) { 047 return new RefCnt(recycler); 048 } 049 050 public RefCnt(Recycler recycler) { 051 this.recycler = recycler; 052 } 053 054 @Override 055 protected final void deallocate() { 056 this.recycler.free(); 057 } 058 059 @Override 060 public final ReferenceCounted touch(Object hint) { 061 throw new UnsupportedOperationException(); 062 } 063}