-
Notifications
You must be signed in to change notification settings - Fork 32
Utils to convert between Pointer<Uint8> and List<int> #22
Comments
You can use 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. |
@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. |
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. |
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. |
Right now, that store operation will just do a loop of |
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? |
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`.
}); |
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!The text was updated successfully, but these errors were encountered: