如何为JNA库中的所有结构设置全局内存字节对齐? [英] How to set global memory byte alignment for all structures in JNA library?

查看:475
本文介绍了如何为JNA库中的所有结构设置全局内存字节对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为JNA库(* .dll Java包装器)中的所有数据结构设置全局内存字节对齐?

Is there any way to set global memory byte alignment for all data structures in JNA library (*.dll Java wrapper)?

有时,我必须在实施过程中通过反复试验来确定正确的对齐方式,目前,我以非常笨拙的方式进行此操作-我在每个结构(很多结构)中都设置了数据对齐方式(super(ALIGN_NONE))在单独的文件中).

Sometimes I have to determine correct alignment by trial and error during implementation and currently I'm doing this in very clumsy way - I'm setting data alignment (super(ALIGN_NONE)) in each and every structure (a lot of structures in separate files).

解决我的问题的最好方法是扩展结构:

edit: The best way to solve my problem was to extend Structure:

public abstract class StructureAligned extends Structure {
    public static final int STRUCTURE_ALIGNMENT = ALIGN_NONE;

    protected StructureAligned() {
        super(STRUCTURE_ALIGNMENT);
    }

    protected StructureAligned(Pointer p) {
        super(p, STRUCTURE_ALIGNMENT);
    }
}

..但这导致了下一个问题:哪个(指针)构造函数更好,为什么:

..but this led to next question: Which (Pointer) constructor is better and why:

super(p, STRUCTURE_ALIGNMENT);

super(STRUCTURE_ALIGNMENT);
read();

super(STRUCTURE_ALIGNMENT);
useMemory(p);
read();

?

推荐答案

制作自己的Structure子类,并使用所需的对齐类型调用适当的构造函数,或者在构造函数中调用setAlignType().

Make your own Structure subclass and call the appropriate constructor with the desired alignment type, or call setAlignType() in the constructor.

如果JNA没有为平台上最常用的编译器正确计算适当的默认对齐方式,则应针对JNA提交错误.

If JNA is not correctly calculating the appropriate default alignment for the most commonly used compiler on the platform, then you should file a bug against JNA.

但是,如果您有一个仅出于自身内部原因而使用一堆任意对齐方式的库,那么通过一个公共基类为所有Structure类关闭对齐方式会更合适.

If, however, you have a library that simply uses a bunch of arbitrary alignments for its own internal reasons, then turning off alignment for all your Structure classes via a common base class is more appropriate.

这篇关于如何为JNA库中的所有结构设置全局内存字节对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆