LCOV - code coverage report
Current view: top level - pebble/internal/cache - value_invariants.go (source / functions) Hit Total Coverage
Test: 2024-01-22 08:16Z 5b092519 - tests only.lcov Lines: 22 28 78.6 %
Date: 2024-01-22 08:16:46 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 (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             :         "github.com/cockroachdb/pebble/internal/manual"
      16             : )
      17             : 
      18             : // newValue creates a Value with a manually managed buffer of size n.
      19             : //
      20             : // This definition of newValue is used when either the "invariants" or
      21             : // "tracing" build tags are specified. It hooks up a finalizer to the returned
      22             : // Value that checks for memory leaks when the GC determines the Value is no
      23             : // longer reachable.
      24           1 : func newValue(n int) *Value {
      25           1 :         if n == 0 {
      26           0 :                 return nil
      27           0 :         }
      28           1 :         b := manual.New(n)
      29           1 :         v := &Value{buf: b}
      30           1 :         v.ref.init(1)
      31           1 :         // Note: this is a no-op if invariants and tracing are disabled or race is
      32           1 :         // enabled.
      33           1 :         invariants.SetFinalizer(v, func(obj interface{}) {
      34           1 :                 v := obj.(*Value)
      35           1 :                 if v.buf != nil {
      36           0 :                         fmt.Fprintf(os.Stderr, "%p: cache value was not freed: refs=%d\n%s",
      37           0 :                                 v, v.refs(), v.ref.traces())
      38           0 :                         os.Exit(1)
      39           0 :                 }
      40             :         })
      41           1 :         return v
      42             : }
      43             : 
      44           1 : func (v *Value) free() {
      45           1 :         // When "invariants" are enabled set the value contents to 0xff in order to
      46           1 :         // cache use-after-free bugs.
      47           1 :         for i := range v.buf {
      48           1 :                 v.buf[i] = 0xff
      49           1 :         }
      50           1 :         manual.Free(v.buf)
      51           1 :         // Setting Value.buf to nil is needed for correctness of the leak checking
      52           1 :         // that is performed when the "invariants" or "tracing" build tags are
      53           1 :         // enabled.
      54           1 :         v.buf = nil
      55             : }

Generated by: LCOV version 1.14