dongzanghua8422 2016-04-18 06:59
浏览 28

在c共享库中公开带有2D slice作为参数的函数(将通过JNA和C在Java中使用)

I am trying to write a simple matrix operations API using go and expose the APIs as a shared library. This shared library will be used from Java(using JNA) and from C. The documentation is very sparse about using any data type beyond simple int or string as function parameters. My requirement is to expose functions with 1 or more 2D slices as parameters AND also as return types. I am not able to figure out if such a thing is supported. Is this possible? Are there any examples for this?

  • 写回答

1条回答 默认 最新

  • dongtang1997 2016-04-19 06:49
    关注

    I think the key point is to have a look to the c bindings of slice,string and int generated by go build tool. I not tried 2D slice, but it should no different to 1D slice with unsafe pointer converter, maybe just be one more time allocation and convertion.

    I'm not sure it's the best way, but here's the example for 1D slice:

    the go part:

    import "C"
    //export CFoo
    func CFoo(content []byte) string{
        var ret []byte
        //blablabla to get ret
        cbuf := unsafe.Pointer(C.malloc(C.size_t(len(ret))))
        C.memcpy(cbuf, unsafe.Pointer(&ret[0]), C.size_t(len(ret)))
        var finalString string
        hdr := (*reflect.StringHeader)(unsafe.Pointer(&finalString))
        hdr.Data = uintptr(unsafe.Pointer(cbuf))
        hdr.Len = len(ret)
        return finalString
    }
    

    compile with -buildmode=c-shared, to get libmygo.so.

    I not know JNA, expecting it like JNI. the JNI part as well as pure C part:

    #include <stdio.h>
    #include <jni.h>
    #include <string.h>
    
    typedef signed char GoInt8;
    typedef unsigned char GoUint8;
    typedef short GoInt16;
    typedef unsigned short GoUint16;
    typedef int GoInt32;
    typedef unsigned int GoUint32;
    typedef long long GoInt64;
    typedef unsigned long long GoUint64;
    typedef GoInt32 GoInt;
    typedef GoUint32 GoUint;
    typedef __SIZE_TYPE__ GoUintptr;
    typedef float GoFloat32;
    typedef double GoFloat64;
    typedef float _Complex GoComplex64;
    typedef double _Complex GoComplex128;
    
    typedef struct { const char *p; GoInt n; } GoString;
    typedef void *GoMap;
    typedef void *GoChan;
    typedef struct { void *t; void *v; } GoInterface;
    typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
    
    JNIEXPORT JNICALL jbyteArray Java_com_mynextev_infotainment_app_myev_Native_foo(JNIEnv* env, jobject obj,jbyteArray content){
        JNIEnv ienv = *env;
    
        void * Ccontent = ienv->GetByteArrayElements(env, content, 0); 
        int Lcontent = ienv->GetArrayLength(env, content);
        GoSlice Gcontent = {Ccontent, Lcontent, Lcontent};
        if(!gret.n){
            printf("jni CDoAESEnc");
            return NULL;
        }
        jbyteArray ret = ienv->NewByteArray(env, gret.n);
        ienv->SetByteArrayRegion(env, ret, 0, gret.n, gret.p);
        free((void*)gret.p);
        ienv->ReleaseByteArrayElements(env, content, Ccontent, JNI_ABORT);
        return ret;
    }
    

    build it with libmygo.so.

    finally you get two so files. one for C which can be used standalone; one for Java which must be used with libmygo.so together.

    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了