English 中文(简体)
JNA阵列和点工
原标题:JNA arrays and Pointer
  • 时间:2011-09-29 14:16:59
  •  标签:
  • java
  • c
  • jna

I m working on a project at the moment that requires me to receive a call in Java from a C library. Basically I call a C function that takes a function pointer, the C function then uses the function pointer as a callback. I m using JNA to pass a Java object (I ll call it the Callback object from now on) as the callback function. The callback object has a single method that receives a C structure (called Frame) that contains a 16 element byte array as well as other variables. I ve wrapped this structure in a Java class in the standard JNA way, like this:

class Frame extends Structure {
    public short port;
    public short flags;
    public Pointer name // this is a pointer to the byte array
    public int rateDivisor;
}

追索机制运作良好! 背心物体从C处收到一小块物体,但当我试图利用<代码>,即:getByteArray(0、16)从点子公司获得星体阵列时,该申请因暴力例外而坠毁。 然而,如果我用一阵列取代点子:

class Frame extends Structure {
    public short port;
    public short flags;
    public byte[] name = new byte[16];
    public int rateDivisor;
}

Then the code works fine! There s a good reason why I don t want to use this code however. Every time I call the function the returned Frame is actually the same object (it s just being reused) but the byte array is a new object. This callback function is being called many times a second which causes the garbage collector to goes crazy gobbling up thousands of temporary arrays. This project is performance critical so I really don t want any temporary objects in this part of the application.

My guess is that by using a byte array in the Frame class I m causing JNA to create a new copy of the C byte array. What I want to do is have a pointer to the C byte array therefore removing the need to copy the data, hence the experimentation with JNA Pointer.

My question is why can t I get the byte array from the Pointer? Any help would be much appreciated :)

(说明:使这种情况更加困难的是,我没有接触到《来源法》!)

问题回答

原因可能与以下原因一样简单:名称不是C级结构对应单位/m>。

让我向大家介绍两个例子:

C:

typedef struct{
  char * name;
}MyCStruct;

地图:

class MyJStruct extends Structure{
  Pointer name;
}

但在其他情况下(我认为你会去做):

C:

typedef struct{
  char name[16];
}MyCStruct;

地图:

class MyJStruct extends Structure{
  byte[] name = new byte[16];
}

在第一种情况下,C结构具有点数,其规模是点数(4或8Bytes,视32/64bits而定),第二种情况下,C结构拥有整块体,即面积为16个。

这解释了两幅瓦 Java地图之间的区别:日本宇宙航行局需要了解如何阅读结构领域,也解释了为什么在打电话<代码>时出现错误。

  1. take the first 4 Bytes following the flags field
  2. treat it as a pointer
  3. go into ram to the pointed adress and read 16 Bytes

自记忆区指出后坠毁的,很可能无法达到方案。

您可在C中查到:如果sizeof (struct Frame) 是24,那么,如果其第12(或16)条,则其位置为32个轨道点(或64个轨道点)

关于这一问题的正式文件非常清楚,但很难找到,因此,你可以:

rel=“noreferer”http://t Wall.github.com/jna/3.3.0/javadoc/overview-summary.html

在“Nested ranges”下发现,我确实鼓励你阅读下文“可变规模结构”一节。

希望这一帮助

目 录

将你的反馈意见参数改为点。 • 保持自己的私人点和坐标;Frame地图(有或没有薄弱环节),并且只有在输入点基础上,才建立新框架。

e.g.

Map frames = new HashMap<Pointer,Frame>();

void callback(Pointer p) {
   Frame f = frames.get(p);
   if (f == null) {
       f = new Frame(p);
       frames.put(p, f);
   }
   // do whatever...
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...