如何在jnr ffi中使用带结构的结构

时间:2016-07-14 08:25:59

标签: java c struct ffi jnr

我有以下c代码:

#include <stdio.h>

struct Second {
    int a_number;
};

struct Top {
    struct Second second;
};

void lets_go(struct Top *top) {
    printf("The number is %d\n", top->second.a_number);
}

除了Java,我想做这个:

int main(void) {
    struct Top top = {{8}};
    lets_go(&top);
}

我也想使用看起来很酷的jnr-ffi内容,所以我查看了测试并进行了修改并最终得到了这个:

package structs.playing;

import structs.playing.Program.Test.Top;
import structs.playing.Program.Test.Second;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;

public final class Program {

    public static interface Test {

        void lets_go(Top top);

        public static final class Second extends Struct {               
            public final Signed32 a_number = new Signed32();                
            public Second(final Runtime runtime) {
                super(runtime);
            }
        }

        public static final class Top extends Struct {              
            public Second second;                           
            public Top(final Runtime runtime) {
                super(runtime);
            }
        }
    }

    public static void main(final String[] args) {

        Test test = LibraryLoader.create(Test.class).load("test");
        Runtime runtime = Runtime.getRuntime(test);         
        Top top = new Top(runtime);
        Second second = new Second(runtime);
        top.second = second;
        second.a_number.set(7);         
        test.lets_go(top);
    }    
}

问题是a_number的值根本没有设置,所以我在输出中得到一个垃圾值,例如:

The number is 46645760

那么如何获得与我的C代码相同的内容?

3 个答案:

答案 0 :(得分:4)

编辑: 我花了一点时间查看代码,我对创建结构的理解略有改变。

我相信你应该声明关于结构的所有内容并使其成为最终结构,因为每当你声明一个新成员时,它就会使用它所属的结构注册自己。

每个用例的struct中都有一些辅助函数。重载的array()方法允许您注册Members或Structs数组。 inner()方法允许您注册单个结构。否则,您只需定义新的Member对象,它们就会自动注册。

例如:

struct Second {
    int a_number;
};

struct Top {
    struct Second second;
    struct Second seconds[5];
    int another_number;
    int more_numbers[5];
};

表示为:

public final class Second extends Struct {
    public final Signed32 a_number = new Signed32();
    public Second(final Runtime runtime) {
        super(runtime);
    }
}

public final class Top extends Struct {              
    public final Second second = inner(new Second(getRuntime()));  
    public final Second[] seconds = array(new Second[5]);
    public final Signed32 another_number = new Signed32();
    public final Signed32[] more_numbers = array(new Signed32[5]);
    public Top(final Runtime runtime) {
        super(runtime);
    }
}

ORIGINAL: 我相信这样做的正确方法是使用接受的重载Struct构造函数(Runtime,Struct)。 https://github.com/jnr/jnr-ffi/blob/master/src/main/java/jnr/ffi/Struct.java#L129

protected Struct(Runtime runtime, Struct enclosing) {
    this(runtime);
    __info.alignment = enclosing.__info.alignment;
}

此构造函数强制封闭结构共享其内存。所以在你的例子中我认为它看起来像这样:

package structs.playing;

import structs.playing.Program.Test.Top;
import structs.playing.Program.Test.Second;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;

public final class Program {

    public static interface Test {

        void lets_go(Top top);

        public static final class Second extends Struct {               
            public final Signed32 a_number = new Signed32();                
            public Second(final Runtime runtime, final Struct enclosing) {
                super(runtime, enclosing);
            }
        }

        public static final class Top extends Struct {              
            public Second second;                           
            public Top(final Runtime runtime) {
                super(runtime);
            }
        }
    }

    public static void main(final String[] args) {

        Test test = LibraryLoader.create(Test.class).load("test");
        Runtime runtime = Runtime.getRuntime(test);         
        Top top = new Top(runtime);
        Second second = new Second(runtime, top);
        top.second = second;
        second.a_number.set(7);         
        test.lets_go(top);
    }    
}

注意对Second的构造函数的更改,并注意我将Top对象传递给Second对象,因此它知道top将它包围起来。这个没有经过测试,只是在我试图理解代码时分享我发现的东西。

我认为你的例子中发生的事情是第二个对象正在分配自己的内存,而Top对此一无所知。

如果这不起作用我会建议你做这样的事情:

public static final class Top extends Struct {              
    public Second second = new Second(getRuntime(), this);                           
    public Top(final Runtime runtime) {
        super(runtime);
    }
}

答案 1 :(得分:2)

分配结构时,如行

top.second = second;

在您的Java代码中,结构从<{1}} 复制second,这样它们就会成为不同内存区域中的独立实体。稍后,当您将7分配给以下行中top.second的{​​{1}}属性时:

a_number

second的相应属性保持不变,因为它们不是同一个对象。

为了得到与C代码相同的结果,请尝试将second.a_number.set(7); 方法更改为:

top.second

初始化新的main对象不是必需的,因为在初始化public static void main(final String[] args) { Test test = LibraryLoader.create(Test.class).load("test"); Runtime runtime = Runtime.getRuntime(test); Top top = new Top(runtime); top.second.a_number.set(8); test.lets_go(top); } 对象时已经为Second分配了内存。

答案 2 :(得分:1)

我已经弄明白了(顺便说一句,我知道成员应该是私有的并且包含在属性中但是我想让代码片段尽可能小,这不是生产质量代码)... < / p>

如果你将Pointer成员变量放入结构中,那么在构造子坐标结构时可以使用它的内存......

package structs.playing;

import structs.playing.Program.Test.Top;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;

public final class Program {

    public static interface Test {

        void lets_go(Top top);

        public static final class Second extends Struct {

            public final Signed32 a_number = new Signed32();

            public Second(final Runtime runtime) {
                super(runtime);
            }           
        }

        public static final class Top extends Struct {

            private final Pointer secondPointer = new Pointer();            
            public final Second second;

            public Top(final Runtime runtime) {
                super(runtime);                             
                second = new Second(runtime); 
                second.useMemory(secondPointer.getMemory());
            }           
        }
    }

    public static void main(final String[] args) {

         Test test = LibraryLoader.create(Test.class).load("test");
         Runtime runtime = Runtime.getRuntime(test);         
         Top top = new Top(runtime);
         top.second.a_number.set(8);         
         test.lets_go(top);
    }
}