Skip to content

feat: allocate FFI output from Rust #70

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

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from

Conversation

tbrezot
Copy link
Contributor

@tbrezot tbrezot commented Aug 10, 2023

In order to avoid allocating an upper bound on the amount of memory needed in the FFI caller, I propose the following workaround:

  1. the caller allocates the size of a pointer
  2. Rust sets the value of this pointer to a pointer to the allocated buffer
  3. Rust forgets about the buffer (avoids freeing the memory pointer to by the returned pointer)
  4. the caller unwraps the pointer and copies the bytes to a proper structure
  5. the caller free the pointer

This MR applies this method to the FFI API of Findex upsert. The Rust implementation can be found in the Rust repository. All tests pass on my machine.

I need particular proofreading on the free step (5) and potential incompatibilities with a Flutter/C implementation.

@sonatype-lift
Copy link
Contributor

sonatype-lift bot commented Aug 10, 2023

Sonatype Lift is retiring

Sonatype Lift will be retiring on Sep 12, 2023, with its analysis stopping on Aug 12, 2023. We understand that this news may come as a disappointment, and Sonatype is committed to helping you transition off it seamlessly. If you’d like to retain your data, please export your issues from the web console.
We are extremely grateful and thank you for your support over the years.

📖 Read about the impacts and timeline

@ThibaudDauce
Copy link
Contributor

I just checked on Dart, it seems hard to do the same thing… I'll continue to investigate.

Co-authored-by: Manuthor <32013169+Manuthor@users.noreply.github.com>
@ThibaudDauce
Copy link
Contributor

I just checked on Dart, it seems hard to do the same thing… I'll continue to investigate.

Something like this may work:

      final bytes = Pointer.fromAddress(output.value).cast<Uint8>();
      malloc.free(bytes);

      return deserializeSearchResults(
        bytes.asTypedList(outputLengthPointer.value),
      );

@tbrezot tbrezot force-pushed the feature/allocate_from_rust branch from e8069e5 to d0603d2 Compare August 11, 2023 12:29
Base automatically changed from feature/use_findex_v5.0.0 to develop September 5, 2023 13:36
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.

3 participants