Skip to content

feat: transfer byte vector allocations without copying - #20

Open
mpgopala wants to merge 1 commit into
mainfrom
mpgopala/zero-copy-bindings
Open

mpgopala wants to merge 1 commit into
mainfrom
mpgopala/zero-copy-bindings

Conversation

@mpgopala

Copy link
Copy Markdown

Changes in this pull request

Update to_c_bytes to hand off the original Rust Vec<u8> allocation to C when its address is even, eliminating the intermediate allocation and copy.

  • Preserve the original pointer, length, and capacity without shrinking or copying to reduce spare capacity.
  • Reconstruct and drop the original vector only when registry cleanup runs.
  • Preserve cancellation semantics: dropping a cancelled cleanup closure does not free the transferred allocation.
  • Retain the aligned-copy fallback for odd-addressed allocations, as required by the registry’s pointer-tagging scheme.
  • Preserve empty-buffer behavior, error reporting, and existing release conventions.
  • Update ownership documentation and the changelog.

No public API or C ABI changes. Callers continue to treat returned bytes as read-only and release them through the existing library cleanup mechanism.

Validation

The following commands passed against the cimpl manifest:

  • cargo test --manifest-path /Users/gsharma/source/c2pa/cimpl/Cargo.toml — 61 tests passed; 27 documentation examples ignored.
  • cargo clippy --manifest-path /Users/gsharma/source/c2pa/cimpl/Cargo.toml --all-targets -- -D warnings — passed.
  • cargo fmt --manifest-path /Users/gsharma/source/c2pa/cimpl/Cargo.toml --check — passed.
  • cargo +nightly miri test --manifest-path /Users/gsharma/source/c2pa/cimpl/Cargo.toml --lib to_c_bytes — 8 tests passed.

Regression coverage includes allocation identity, retained spare capacity, odd-address fallback, empty inputs, tracking refusal, cancellation, cross-thread release, double-free rejection, and untracked pointers.

Miri exercised genuine odd-addressed allocations. It emits exposed-provenance warnings for pointer reconstruction, so these results are not a strict-provenance proof.

No crate version bump or publication is included.

Checklist

  • This PR represents a single feature, fix, or change.
  • All applicable changes have been documented.
  • Any TO DO items (or similar) have been entered as GitHub issues and the link to that issue has been included in a comment.

Preserve original length and capacity for tracked byte returns, keep the even-buffer fallback for odd addresses, and test ownership, cancellation, and failure paths.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mpgopala
mpgopala requested a review from gpeacock October 27, 2026 15:37
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.

1 participant