refs

package
v0.0.0-...-a858404 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package refs defines an interface for reference counted objects.

Index

Constants

This section is empty.

Variables

View Source
var CleanupSync sync.WaitGroup

CleanupSync is used to wait for async cleanup actions.

Functions

func DoLeakCheck

func DoLeakCheck()

DoLeakCheck iterates through the live object map and logs a message for each object. It should be called when no reference-counted objects are reachable anymore, at which point anything left in the map is considered a leak. On multiple calls, only the first call will perform the leak check.

func DoRepeatedLeakCheck

func DoRepeatedLeakCheck()

DoRepeatedLeakCheck is the same as DoLeakCheck except that it can be called multiple times by the caller to incrementally perform leak checking.

func FormatStack

func FormatStack(pcs []uintptr) string

FormatStack converts the given stack into a readable format.

func LeakCheckEnabled

func LeakCheckEnabled() bool

LeakCheckEnabled returns whether leak checking is enabled. The following functions should only be called if it returns true.

func LogDecRef

func LogDecRef(obj CheckedObject, refs int64)

LogDecRef logs a reference decrement.

func LogIncRef

func LogIncRef(obj CheckedObject, refs int64)

LogIncRef logs a reference increment.

func LogTryIncRef

func LogTryIncRef(obj CheckedObject, refs int64)

LogTryIncRef logs a successful TryIncRef call.

func OnExit

func OnExit()

OnExit is called on sandbox exit. It runs GC to enqueue refcount finalizers, which check for reference leaks. There is no way to guarantee that every finalizer will run before exiting, but this at least ensures that they will be discovered/enqueued by GC.

func RecordStack

func RecordStack() []uintptr

RecordStack constructs and returns the PCs on the current stack.

func Register

func Register(obj CheckedObject)

Register adds obj to the live object map.

func SetLeakMode

func SetLeakMode(mode LeakMode)

SetLeakMode configures the reference leak checker.

func Unregister

func Unregister(obj CheckedObject)

Unregister removes obj from the live object map.

Types

type CheckedObject

type CheckedObject interface {
	// RefType is the type of the reference-counted object.
	RefType() string

	// LeakMessage supplies a warning to be printed upon leak detection.
	LeakMessage() string

	// LogRefs indicates whether reference-related events should be logged.
	LogRefs() bool
}

CheckedObject represents a reference-counted object with an informative leak detection message.

type LeakMode

type LeakMode uint32

LeakMode configures the leak checker.

const (
	// NoLeakChecking indicates that no effort should be made to check for
	// leaks.
	NoLeakChecking LeakMode = iota

	// LeaksLogWarning indicates that a warning should be logged when leaks
	// are found.
	LeaksLogWarning

	// LeaksPanic indidcates that a panic should be issued when leaks are found.
	LeaksPanic
)

func GetLeakMode

func GetLeakMode() LeakMode

GetLeakMode returns the current leak mode.

func (*LeakMode) Get

func (l *LeakMode) Get() any

Get implements flag.Value.

func (*LeakMode) Set

func (l *LeakMode) Set(v string) error

Set implements flag.Value.

func (LeakMode) String

func (l LeakMode) String() string

String implements flag.Value.

type RefCounter

type RefCounter interface {
	// IncRef increments the reference counter on the object.
	IncRef()

	// DecRef decrements the object's reference count. Users of refs_template.Refs
	// may specify a destructor to be called once the reference count reaches zero.
	DecRef(ctx context.Context)
}

RefCounter is the interface to be implemented by objects that are reference counted.

type TryRefCounter

type TryRefCounter interface {
	RefCounter

	// TryIncRef attempts to increment the reference count, but may fail if all
	// references have already been dropped, in which case it returns false. If
	// true is returned, then a valid reference is now held on the object.
	TryIncRef() bool
}

TryRefCounter is like RefCounter but allow the ref increment to be tried.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL