Skip to content

[vm/ffi] Expose struct field offsets #41237

Open
@dcharkes

Description

@dcharkes

dart:ffi internally calculates the fields offsets of structs in order to enable loading/storing from/to those fields.

However, sometimes one would like to pass a pointer to a field inside a struct to C. For that use case we should expose the internal offsets.

One way we could do this, for example for

class VeryLargeStruct extends Struct {
@Int8()
int a;
@Int16()
int b;
@Int32()
int c;
@Int64()
int d;
@Uint8()
int e;
@Uint16()
int f;
@Uint32()
int g;
@Uint64()
int h;
@IntPtr()
int i;
@Double()
double j;
@Float()
double k;
Pointer<VeryLargeStruct> parent;
@IntPtr()
int numChildren;
Pointer<VeryLargeStruct> children;
@Int8()
int smallLastField;
}
would be

class VeryLargeStruct extends Struct { 
  @Int8() 
  int a;
  external static int get offsetOfA; // Will be filled in by the compiler.

  // ...
}

Or alternatively we could provide inner pointers:

class VeryLargeStruct extends Struct { 
  @Int8() 
  int a;
  external static Pointer<Int8> get pointerToA; // Will be filled in by the compiler.

  // ...
}

(Though, I like exposing offset better. That is a better indication you're doing your own pointer arithmetic.)

Edit: we can't expose Pointers because the struct might be backed by TypedData. But we could generate extension methods for the struct pointers in package:ffigen: dart-lang/native#331.

cc @timsneath

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions