Source File
pointer.go
Belonging Package
internal/weak
// Copyright 2024 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file./*The weak package is a package for managing weak pointers.Weak pointers are pointers that explicitly do not keep a value live andmust be queried for a regular Go pointer.The result of such a query may be observed as nil at any point after aweakly-pointed-to object becomes eligible for reclamation by the garbagecollector.More specifically, weak pointers become nil as soon as the garbage collectoridentifies that the object is unreachable, before it is made reachableagain by a finalizer.In terms of the C# language, these semantics are roughly equivalent to thethe semantics of "short" weak references.In terms of the Java language, these semantics are roughly equivalent to thesemantics of the WeakReference type.Using go:linkname to access this package and the functions it referencesis explicitly forbidden by the toolchain because the semantics of thispackage have not gone through the proposal process. By exposing thisfunctionality, we risk locking in the existing semantics due to Hyrum's Law.If you believe you have a good use-case for weak references not alreadycovered by the standard library, file a proposal issue athttps://github.com/golang/go/issues instead of relying on this package.*/package weakimport ()// Pointer is a weak pointer to a value of type T.//// This value is comparable is guaranteed to compare equal if the pointers// that they were created from compare equal. This property is retained even// after the object referenced by the pointer used to create a weak reference// is reclaimed.//// If multiple weak pointers are made to different offsets within same object// (for example, pointers to different fields of the same struct), those pointers// will not compare equal.// If a weak pointer is created from an object that becomes reachable again due// to a finalizer, that weak pointer will not compare equal with weak pointers// created before it became unreachable.type Pointer[ any] struct {u unsafe.Pointer}// Make creates a weak pointer from a strong pointer to some value of type T.func [ any]( *) Pointer[] {// Explicitly force ptr to escape to the heap.= abi.Escape()var unsafe.Pointerif != nil {= runtime_registerWeakPointer(unsafe.Pointer())}runtime.KeepAlive()return Pointer[]{}}// Strong creates a strong pointer from the weak pointer.// Returns nil if the original value for the weak pointer was reclaimed by// the garbage collector.// If a weak pointer points to an object with a finalizer, then Strong will// return nil as soon as the object's finalizer is queued for execution.func ( Pointer[]) () * {return (*)(runtime_makeStrongFromWeak(.u))}// Implemented in runtime.//go:linkname runtime_registerWeakPointerfunc (unsafe.Pointer) unsafe.Pointer//go:linkname runtime_makeStrongFromWeakfunc (unsafe.Pointer) unsafe.Pointer
![]() |
The pages are generated with Golds v0.7.6. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |