Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Utils to convert between Pointer<Uint8> and List<int> #22

Closed
hanabi1224 opened this issue Nov 7, 2019 · 7 comments
Closed

Utils to convert between Pointer<Uint8> and List<int> #22

hanabi1224 opened this issue Nov 7, 2019 · 7 comments

Comments

@hanabi1224
Copy link

The logic is already part of function static Pointer<Utf8> toUtf8(String string) . It will be awesome to abstract it as a standalone one. Thanks!

@dcharkes
Copy link
Contributor

dcharkes commented Nov 7, 2019

You can use .asTypedList(int length) to get a List<int> view on native memory.

https://api.dartlang.org/stable/2.6.0/dart-ffi/Uint8Pointer/asTypedList.html

The other way around always requires writing a loop to copy the contents from the Dart list (in Dart garbage collected memory) to C memory.

Feel free to write helper methods/extensions for that and make a pull request to this repo.

@hanabi1224
Copy link
Author

hanabi1224 commented Nov 7, 2019

@dcharkes Thanks for the hints! but it's still not very clear to me whether dart VM is able to GC the result of asTypedList(int length) if it's a direct view on native memory (some kind of weak reference on dart side?) , or is it good practice to always make a copy to a strong-ref List in dart and free the native memory ASAP to avoid memory leak.

@dcharkes
Copy link
Contributor

dcharkes commented Nov 7, 2019

Yes, that is true.

I've been working on some pool abstraction that you can use for memory management, see https://dart-review.googlesource.com/c/sdk/+/123662. I hope to land (a variation) of that in this package. Does that cover your use case?

If not, you can also write copy helpers for copying out the data from C to Dart.

@dcharkes
Copy link
Contributor

it's still not very clear to me whether dart VM is able to GC the result of asTypedList(int length)

At this point no.

I've created a Pool abstraction for memory management, please see #30. You can use this to call free.

Finalizers will take longer to implement, please see dart-lang/sdk#35770. When we've added those, you can also use that to call free.

I'll close this issue, please reopen if you need more help.

@dcharkes
Copy link
Contributor

Right now, that store operation will just do a loop of Pointer<Uint8> loads and stores. Making that an actual memcopy is tracked in dart-lang/sdk#43967.

@abdulbari149
Copy link

abdulbari149 commented Oct 19, 2022

Hey, I would like to convert the dart list to a c-pointer so as to pass them in the c-code. Is there any specific function? Can you tell me the procedure to copy contents from the dart list to the c-pointer?

@dcharkes
Copy link
Contributor

This sounds like a stack overflow question.

Most likely something such as:

import "package:ffi/ffi.dart";

  final myList = [1, 2, 3, 4];
  final myPointer = malloc<Int64>(myList.length);
  for (int index = 0; index < myList.length; index++) {
    myPointer[index] = myList[index];
  }
  // Use `myPointer`.
  malloc.free(myPointer);

Or

import "package:ffi/ffi.dart";

  final myList = [1, 2, 3, 4];
  using((arena) {
    final myPointer = arena<Int64>(myList.length);
    for (int index = 0; index < myList.length; index++) {
      myPointer[index] = myList[index];
    }
    // Use `myPointer`.
  });

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants