LCOV - code coverage report
Current view: top level - pebble/internal/cache - refcnt_normal.go (source / functions) Hit Total Coverage
Test: 2024-12-19 08:17Z d5339521 - tests only.lcov Lines: 22 29 75.9 %
Date: 2024-12-19 08:17:33 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use
       2             : // of this source code is governed by a BSD-style license that can be found in
       3             : // the LICENSE file.
       4             : 
       5             : //go:build !tracing
       6             : // +build !tracing
       7             : 
       8             : package cache
       9             : 
      10             : import (
      11             :         "fmt"
      12             :         "sync/atomic"
      13             : 
      14             :         "github.com/cockroachdb/redact"
      15             : )
      16             : 
      17             : // refcnt provides an atomic reference count. This version is used when the
      18             : // "tracing" build tag is not enabled. See refcnt_tracing.go for the "tracing"
      19             : // enabled version.
      20             : type refcnt struct {
      21             :         val atomic.Int32
      22             : }
      23             : 
      24             : // initialize the reference count to the specified value.
      25           1 : func (v *refcnt) init(val int32) {
      26           1 :         v.val.Store(val)
      27           1 : }
      28             : 
      29           1 : func (v *refcnt) refs() int32 {
      30           1 :         return v.val.Load()
      31           1 : }
      32             : 
      33           1 : func (v *refcnt) acquire() {
      34           1 :         switch v := v.val.Add(1); {
      35           0 :         case v <= 1:
      36           0 :                 panic(redact.Safe(fmt.Sprintf("pebble: inconsistent reference count: %d", v)))
      37             :         }
      38             : }
      39             : 
      40             : // acquireAllowZero is the same as acquire, but allows acquireAllowZero to be
      41             : // called with a zero refcnt. This is useful for cases where the entry which
      42             : // is being reference counted is inside a container and the container does not
      43             : // hold a reference. The container uses release() returning true to attempt to
      44             : // do a cleanup from the container.
      45           1 : func (v *refcnt) acquireAllowZero() {
      46           1 :         v.val.Add(1)
      47           1 : }
      48             : 
      49           1 : func (v *refcnt) release() bool {
      50           1 :         switch v := v.val.Add(-1); {
      51           0 :         case v < 0:
      52           0 :                 panic(redact.Safe(fmt.Sprintf("pebble: inconsistent reference count: %d", v)))
      53           1 :         case v == 0:
      54           1 :                 return true
      55           1 :         default:
      56           1 :                 return false
      57             :         }
      58             : }
      59             : 
      60           1 : func (v *refcnt) value() int32 {
      61           1 :         return v.val.Load()
      62           1 : }
      63             : 
      64           1 : func (v *refcnt) trace(msg string) {
      65           1 : }
      66             : 
      67           0 : func (v *refcnt) traces() string {
      68           0 :         return ""
      69           0 : }
      70             : 
      71             : // Silence unused warning.
      72             : var _ = (*refcnt)(nil).traces

Generated by: LCOV version 1.14