Skip to content

docs: clarify avalue double indirection for out-pointer parameters - #80

Merged
kolkov merged 1 commit into
mainfrom
docs/struct-examples
Sep 9, 2026
Merged

kolkov merged 1 commit into
mainfrom
docs/struct-examples

Conversation

@kolkov

@kolkov kolkov commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Document the avalue pointer-to-value convention with focus on the non-obvious out-pointer case. Updated in three places:

  • ffi/ffi.go godoc — avalue Convention section with WRONG/CORRECT examples
  • README.md — new section with out-pointer pattern and rule of thumb
  • docs/ARCHITECTURE.md — avalue Indirection Convention in Type System

Also fixes stale package doc (platform list, performance numbers).

The Problem

// WRONG — C receives NULL:
var device unsafe.Pointer
avalue := []unsafe.Pointer{unsafe.Pointer(&device)}

// CORRECT — C receives &device:
var device unsafe.Pointer
devicePtr := unsafe.Pointer(&device)
avalue := []unsafe.Pointer{unsafe.Pointer(&devicePtr)}

Discovered while building NVIDIA NVML bindings via GoFFI on Windows.

Test plan

  • go build ./... — clean
  • go test ./... — all pass
  • No stale references in public docs

Closes #79

…loses #79)

Add comprehensive documentation of the avalue pointer-to-value convention
in three places:

- ffi/ffi.go godoc: avalue Convention section with WRONG/CORRECT examples
- README.md: new 'avalue Convention' section with out-pointer pattern
- docs/ARCHITECTURE.md: avalue Indirection Convention in Type System section

Also update package doc: fix stale platform list and performance numbers.
@kolkov
kolkov force-pushed the docs/struct-examples branch from f8e2ab7 to 1b765fb Compare September 9, 2026 09:17
@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kolkov
kolkov merged commit 8636646 into main Sep 9, 2026
15 checks passed
@kolkov
kolkov deleted the docs/struct-examples branch September 9, 2026 09:22
unxed pushed a commit to unxed/goffi that referenced this pull request Sep 25, 2026
Port of upstream go-webgpu/goffi 8636646 (go-webgpu#80, closes go-webgpu#79): avalue[i]
is a pointer TO the argument value, so a C out-pointer parameter
(T *out) needs an intermediate variable, outPtr := unsafe.Pointer(&out),
passed as unsafe.Pointer(&outPtr). Passing &out directly hands C the
current value of out -- usually NULL. Documented, with the wrong and
the right form, in the ffi package doc, README and docs/ARCHITECTURE.md.

Adapted rather than taken verbatim in ffi/ffi.go:

- the package doc's platform list follows this fork's
  docs/PLATFORMS.md (adds NetBSD and Windows 386) instead of upstream's
  "8 production targets";
- the Performance paragraph is left as it was: upstream replaced it
  with its own benchmark figures, which were not measured here;
- the "Use runtime.Pinner to pin pointers under a moving GC" line that
  upstream dropped from CallFunctionContext's safety notes is kept.

(cherry picked from commit 8636646)
unxed added a commit to unxed/goffi that referenced this pull request Sep 25, 2026
…rk main

Upstream go-webgpu#78 added its own goffi_static. The fork already has one, and
Profile U and goffi_musl are built on it (ffi.Available, internal/static,
callback_static.go, ffi.ErrStaticBuild). The two are equivalent for
callers: LoadLibrary/GetSymbol return a *LibraryError wrapping
ffi.ErrStaticBuild, the ELF has no PT_INTERP or DT_NEEDED, and errno
capture is off. So the code keeps the fork's implementation:

- conflicting .go/.s files and upstream's build-tag-only edits resolve to
  the fork's version;
- upstream-only static files that would duplicate the fork's definitions
  are dropped: ffi/dl_static.go, ffi/errors_static.go,
  ffi/fakecgo_static.go, internal/fakecgo/static.go and
  asm_static_{amd64,arm64}.s;
- upstream's ffi/static_test.go is kept, restricted to linux, where the
  fork's static mode applies (it is a no-op on Windows and Android).

Taken from upstream: scripts/check-elf-linking.sh and the elf-linking CI
job (added to ci-success next to platform-matrix), ADR-001, the v0.6.4
CHANGELOG/ROADMAP/README entries, the linking-modes docs (with rows for
goffi_musl and the universal build), and the package doc changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation: clarify avalue double indirection for out-pointer parameters

1 participant