Skip to content

[dart:ffi] Inline arrays in Structs #35763

@dcharkes

Description

@dcharkes
Contributor

Helpers for converting between ffi.Pointers and arrays.

Maybe use external typed data for efficient conversions.

Activity

added this to the Dart VM FFI Flutter MVP milestone on Jan 25, 2019
self-assigned this
on Jan 25, 2019
added
area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
on Jan 25, 2019
removed this from the Dart VM FFI Flutter MVP milestone on Mar 11, 2019
djpnewton

djpnewton commented on Jul 13, 2019

@djpnewton
Contributor

also support for fixed size arrays.

its quite common to have a struct with fixed sized arrays to pass around in C

removed their assignment
on Jul 19, 2019
dcharkes

dcharkes commented on Oct 25, 2019

@dcharkes
ContributorAuthor

We have asTypedList for exposing the native memory as a Dart array.

For structs with inline arrays:

struct MyStruct {
  uint8_t arr[10];
  int64_t arr2[3];
};

We need a dart syntax of representing an inline array.

We could opt for exposing this as a pointer, which provides [] and []= access.

class MyStruct extends Struct {
  @InlineArray(10)
  Pointer<Uint8> arr;

  @InlineArray(3)
  Pointer<Int64> arr2;
}

Using a Pointer as the representing type does not allow us to distinguish pointers and inline arrays by the Dart type. However, this is not really a problem since C does not support passing fixed-size arrays by value in calls.

cc @mkustermann

edit: We do not want to expose it as a pointer, rather as a CArray which represents a fixed-size inlined array.

class MS extends Struct {}

MS foo(MS ms); // C signature for passing struct by value.
@size(4)CArray<MS> foo(@size(4)CArray<MS> mss); // C signature for passing inline array of structs.
Pointer<MS> foo(Pointer<MS> ms); // C signature for passing pointer.

class MS2 extends Struct {
  MS ms; // Nested struct.
}
class MS3 extends Struct {
  @size(4)
  CArray<MS> mss; // Nested array of structs.
}
class MS4 extends Struct {
  Pointer<MS> mss; // Pointer to struct.
}

CArray would be a view on top of the underlying pointer (kind of like struct).

If we expose the nested structs and the inline array as getter/setter pair, the setters would do a memcopy of the whole underlying structure.

(As sidenote, we should also introduce memcopy on structs:

Pointer<MS> p1, p2;
p1.value = p2.ref; // Copies over all memory.

)

changed the title [-]dart:ffi Arrays[/-] [+][dart:ffi] Inline arrays in Structs[/+] on Oct 25, 2019
kakyoism

kakyoism commented on Mar 30, 2020

@kakyoism

@dcharkes

I got this error when trying your solution to getting static arrays on Dart side

Code

class StaticArray extends Struct {
  @InlineArray(10)
  Pointer<Int32> array;
  @Int32()
  int len;
}

Result

$ dart structs.dart 
structs.dart:48:4: Error: Method not found: 'InlineArray'.
  @InlineArray(10)
   ^^^^^^^^^^^
structs.dart:129:15: Error: A function expression can't have a name.
  staticArray.forEach(idx, elem) => print("array[$idx]: $elem");
              ^^^^^^^
structs.dart:129:15: Error: Expected an identifier, but got 'forEach'.
  staticArray.forEach(idx, elem) => print("array[$idx]: $elem");
              ^^^^^^^

Does it mean that this is just a proposal?
What would be the canonical way of getting array data from FFI C pointers?
I'm referring to my latest question.

Thanks in advance!

dcharkes

dcharkes commented on Mar 30, 2020

@dcharkes
ContributorAuthor

Yes, for now it's just a proposal.

See the workaround made by @timsneath here for now.

I'll get around to implementing this in the future, I'm working on other dart:ffi features at the moment.

34 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onarea-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-ffi

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @artob@kevmoo@djpnewton@kakyoism@timsneath

      Issue actions

        [dart:ffi] Inline arrays in Structs · Issue #35763 · dart-lang/sdk