-
Notifications
You must be signed in to change notification settings - Fork 7
c: ClosureData, GoClosure #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,6 +191,18 @@ func GoDeferData() Pointer | |
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| func ClosureData[ClosureT any](closure ClosureT) Pointer { | ||
| ret := new(ClosureT) | ||
| *ret = closure | ||
| return Pointer(ret) | ||
| } | ||
|
|
||
| func GoClosure[ClosureT any](data Pointer) (closure ClosureT) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Document GoClosure's unchecked type reinterpretation + nil precondition
|
||
| return *(*ClosureT)(data) | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| //go:linkname AllocaSigjmpBuf llgo.sigjmpbuf | ||
| func AllocaSigjmpBuf() Pointer | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Document ClosureData's unsafe contract (GC lifetime + exported doc comment)
ClosureDatais an exported function with no doc comment, unlike neighbors such asGoString(line 180). Beyond golint/godoc convention, the lifetime contract is non-obvious:new(ClosureT)allocates a Go-managed object returned as a bareunsafe.Pointer. Once handed to C, the GC no longer sees a typed reference, so if C retains the pointer past the last Go reference the backing object can be collected or moved, leaving a dangling pointer. Recommend a doc comment stating (1) the purpose and (2) that the caller must keep the returnedPointer's backing allocation reachable (e.g. viaruntime.KeepAlive/runtime.Pinner) for as long as C uses it.