使用SWIG将数组从C返回到Java

时间:2011-09-16 17:23:37

标签: java swig

我有这样的C函数:

void get_data(const obj_t *obj, short const **data, int *data_len);

我特意为Swig写了这个,因为

const short *get_data(const obj_t *obj, int *data_len);

导致麻烦,因为SWIG的字体图不够智能,无法将data_len与返回值相关联。

在Java中,我希望能够像这样调用这个函数:

short data[]= mylib.get_data(obj);

但我无法弄清楚如何让数组输出参数成为返回值。使用Ruby和Python,这很好用,因为这些语言的SWIG支持返回输出参数作为返回值(因为语言可以有多个返回值)。

如何让它与Java一起使用?

1 个答案:

答案 0 :(得分:8)

我已将以下测试头文件放在一起以演示此问题:

typedef struct { } obj_t;

const short *get_data(const obj_t *obj, int *data_len) {
  (void)obj;
  static short arr[] = {1,2,3,4,5};
  *data_len = sizeof(arr)/sizeof(*arr);
  return arr;
}

我将通过我编写的模块文件进行讨论,它开始非常标准:

%module test

%{
#include "test.h"
%}

然后我们为data_len参数设置一个类型映射。它不需要在Java端可见,因为数组已知长度,但我们需要为指针指定一些存储空间,我们确保它持续足够长的时间以便我们以后可以读取它将数组返回给Java时。

%typemap(in,numinputs=0,noblock=1) int *data_len {
   int temp_len;
   $1 = &temp_len;
}

然后我们希望SWIG在Java端使用short[]作为返回类型:

%typemap(jstype) const short *get_data "short[]"
%typemap(jtype) const short *get_data "short[]"
JNI方面的

jshortArray - 没有必要构造代理类型,所以我们直接传递返回的值:

%typemap(jni) const short *get_data "jshortArray"
%typemap(javaout) const short *get_data {
  return $jnicall;
}

最后,我们创建一个类型图,它将创建一个新数组,其大小基于函数返回的长度,并将返回的结果复制到Java数组中。如果需要,我们应该free()这里的真实结果数组,但在我的例子中,它是静态分配的,因此不需要被释放。

%typemap(out) const short *get_data {
  $result = JCALL1(NewShortArray, jenv, temp_len);
  JCALL4(SetShortArrayRegion, jenv, $result, 0, temp_len, $1);
  // If the result was malloc()'d free it here
}

最后,我们使用我们刚刚编写的类型图包含SWIG的头文件进行换行:

%include "test.h"

我测试了这个:

public class run {
  public static void main(String argv[]) {
    System.loadLibrary("test");
    obj_t obj = new obj_t();
    short[] result = test.get_data(obj);
    for (int i = 0; i < result.length; ++i) {
      System.out.println(result[i]);
    }
  }
}

哪个产生了:

1
2
3
4
5

供参考,你可以包裹:

void get_data(const obj_t *obj, short const **data, int *data_len);

另外,如果你的函数有办法在不设置数组的情况下查询大小,你可以通过在Java端分配一个正确大小的数组来稍微更聪明地包装它。为此,您需要在Java中编写一个查询大小的中间函数,设置调用然后返回结果数组。这样您就可以使用GetShortArrayElements / ReleaseShortArrayElements进行可能的0次复制调用。

这是有效的,因为Java中的数组基本上是通过引用传递的,例如:

public class ret {
  public static void foo(int arr[]) {
    arr[0] = -100;
  }

  public static void main(String argv[]) {
    int arr[] = new int[10];
    System.out.println(arr[0]);
    foo(arr);
    System.out.println(arr[0]);
  }
}
相关问题