Search code examples
cdartinteropffidart-ffi

Dart C Interoperability. Pass an array of integers to C function


I am learning Dart now and I playing with Dart's Interoperability with C. I am able to use a C method with two int params. Code below:

hello.dart

import 'dart:ffi' as ffi;

typedef sum_func = ffi.Int32 Function(ffi.Int32 a, ffi.Int32 b);
typedef Sum = int Function(int a, int b);
...
final dylib = ffi.DynamicLibrary.open(path);
final sumPointer = dylib.lookup<ffi.NativeFunction<sum_func>>('sum');

final sum = sumPointer.asFunction<Sum>();
print('3 + 5 = ${sum(3, 5)}');

hello.c

int sum(int a, int b){
    return a + b;
}

hello.h

int add(int x, int y)

hello.def

LIBRARY   hello
EXPORTS
   sum

This all works really well, but I also want to have an max C method, which takes an int array as an input and returns the biggest number. How can I do this? I've implemented all the required code in C, but I am not sure how do I "link" it with Dart. Could anyone help me please?


Solution

  • First, I really want to say that I am not a C programmer and especially when it comes to pointers I am not even pretending I have a fully understanding of how to do this kind of things the most optimal way.

    With this out of the way here is my solution based on the primitives example found here: https://github.com/dart-lang/samples/tree/master/ffi/primitives

    primitives.dart

    import 'dart:ffi';
    import 'dart:io' show Platform;
    
    import 'package:ffi/ffi.dart';
    
    typedef max_func = Int32 Function(Pointer<Int32> list, Int32 size);
    typedef Max = int Function(Pointer<Int32> list, int size);
    
    void main() {
      var path = './primitives_library/libprimitives.so';
      if (Platform.isMacOS) path = './primitives_library/libprimtives.dylib';
      if (Platform.isWindows) path = r'primitives_library\Debug\primitives.dll';
      final dylib = DynamicLibrary.open(path);
    
      final list = [1, 5, 3, 59030, 131000, 0];
      final listPtr = intListToArray(list);
    
      final maxPointer = dylib.lookup<NativeFunction<max_func>>('max');
      final max = maxPointer.asFunction<Max>();
      print('${max(listPtr, list.length)}'); // 131000
      malloc.free(listPtr);
    }
    
    Pointer<Int32> intListToArray(List<int> list) {
      final ptr = malloc.allocate<Int32>(sizeOf<Int32>() * list.length);
      for (var i = 0; i < list.length; i++) {
        ptr.elementAt(i).value = list[i];
      }
      return ptr;
    }
    

    primitives.h

    int max(int *listPtr, int size);
    

    primitives.c

    #include "primitives.h"
    
    int max(int *listPtr, int size)
    {
      int currentMax = *listPtr;
    
      for (int i = 0; i < size; i++)
      {
        if (currentMax < *listPtr)
        {
          currentMax = *listPtr;
        }
        listPtr++;
      }
    
      return currentMax;
    }