Giter Site home page Giter Site logo

Comments (7)

dcharkes avatar dcharkes commented on June 28, 2024 1

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.

from ffi.

hanabi1224 avatar hanabi1224 commented on June 28, 2024

@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.

from ffi.

dcharkes avatar dcharkes commented on June 28, 2024

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.

from ffi.

dcharkes avatar dcharkes commented on June 28, 2024

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.

from ffi.

dcharkes avatar dcharkes commented on June 28, 2024

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.

from ffi.

abdulbari149 avatar abdulbari149 commented on June 28, 2024

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?

from ffi.

dcharkes avatar dcharkes commented on June 28, 2024

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`.
  });

from ffi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.