github.com/matrixorigin/matrixone@v0.7.0/pkg/fileservice/rc.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fileservice 16 17 import "sync/atomic" 18 19 // RC represents a reference counted value that will not evict in LRU if refs is greater than 0 20 type RC[T any] struct { 21 refs int64 22 Value T 23 } 24 25 // NewRC creates an RC value with 0 reference 26 func NewRC[T any](value T) *RC[T] { 27 return &RC[T]{ 28 Value: value, 29 refs: 0, 30 } 31 } 32 33 // IncRef increases reference count 34 func (r *RC[T]) IncRef() { 35 atomic.AddInt64(&r.refs, 1) 36 } 37 38 // DecRef decreases reference count 39 func (r *RC[T]) DecRef() { 40 atomic.AddInt64(&r.refs, -1) 41 } 42 43 // RefCount returns reference count 44 func (r *RC[T]) RefCount() int64 { 45 return atomic.LoadInt64(&r.refs) 46 }