github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/internal/cache/entry_invariants.go (about)

     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 (invariants && !race) || (tracing && !race)
     6  // +build invariants,!race tracing,!race
     7  
     8  package cache
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  
    14  	"github.com/cockroachdb/pebble/internal/invariants"
    15  )
    16  
    17  // When the "invariants" or "tracing" build tags are enabled, we need to
    18  // allocate entries using the Go allocator so entry.val properly maintains a
    19  // reference to the Value.
    20  const entriesGoAllocated = true
    21  
    22  func entryAllocNew() *entry {
    23  	e := &entry{}
    24  	// Note: this is a no-op if invariants and tracing are disabled or race is
    25  	// enabled.
    26  	invariants.SetFinalizer(e, func(obj interface{}) {
    27  		e := obj.(*entry)
    28  		if v := e.ref.refs(); v != 0 {
    29  			fmt.Fprintf(os.Stderr, "%p: cache entry has non-zero reference count: %d\n%s",
    30  				e, v, e.ref.traces())
    31  			os.Exit(1)
    32  		}
    33  	})
    34  	return e
    35  }
    36  
    37  func entryAllocFree(e *entry) {
    38  }