根据我的另一个问题和Some Name的回答,我想尝试相反的方式并改进之前的问题:
我想要执行以下步骤:
- 在Java中分配缓冲区
- 在C++中填充数组
- 编辑Java中的值
- 在C++中进行某些数字计算
- 读取Java中的值
考虑下面的C++类:
#pragma once
#pragma pack(push, 8)
class Point
{
public:
double x;
double y;
Point();
virtual ~Point();
void ebeProduct(Point &other) {
x = x * other.x;
y = y * other.y;
}
};
Point::Point()
{
}
Point::~Point()
{
}
#pragma pack(pop)
以下代码片段:
Point point = Point();
std::cout << "Size of Point: " << sizeof(Point) << std::endl;
std::cout << "Address is: " << &point << std::endl;
std::cout << "x address is: " << &(point.x) << " and offset is: " << (long long)&(point.x) - (long long)&point << std::endl;
std::cout << "y address is: " << &(point.y) << " and offset is: " << (long long)&(point.y) - (long long)&point << std::endl;
显示以下结果:
Size of Point: 24
Address is: 000000D45B0FF2C0
x address is: 000000D45B0FF2C8 and offset is: 8
y address is: 000000D45B0FF2D0 and offset is: 16
现在我可以在Java中假设以下大小:
final int NUMBER_OF_POINTS = 10;
final int SIZE_OF_POINT = 24;
final int X_OFFSET = 8;
final int Y_OFFSET = 16;
接下来,我将使用
unsafe
分配一个内存区域,并调用public static native void allocatePoints(long address, int numberOfPoints);
方法,以便用Point
对象填充它们:long address = unsafe.allocateMemory(NUMBER_OF_POINTS * SIZE_OF_POINT);
allocatePoints(address, NUMBER_OF_POINTS);
填充点的C++代码:
Point * points = (Point *) address;
for (int pointIndex = 0; pointIndex < numberOfPoints; pointIndex++) {
new(points + pointIndex) Point();
}
接下来,我将根据给定的偏移量,在Java中填充任意数字到点集中:
for (int i = 0; i < NUMBER_OF_POINTS; i++) {
unsafe.putDouble(address + i * SIZE_OF_POINT + X_OFFSET, i);
unsafe.putDouble(address + i * SIZE_OF_POINT + Y_OFFSET, i * 2);
}
接下来,调用 public static native void multiplyPoints();
方法,它仅会将每个第二个点与其前一个点相乘。
最后,我将在Java中打印给定的值:
for (int i = 0; i < NUMBER_OF_POINTS; i++) {
System.err.print(unsafe.getDouble(address + i * SIZE_OF_POINT + X_OFFSET));
System.err.print(" ; ");
System.err.println(unsafe.getDouble(address + i * SIZE_OF_POINT + Y_OFFSET));
}
这些阶段实际上输出了正确的结果。
我的问题是,这些阶段是否合理:我是否可以假设给定的偏移量是正确的(暂时在同一台机器上),并且直接将值写入Java缓冲区,并从C++对象中读取它们总是会产生正确的结果?
#pragma pack(16)
指令,这应该保证一切都是对齐的。 3:如果Unsafe
被弃用,并且没有其他方式来分配大块内存,我将使用ByteBuffer。 - Guy Yafe