我需要将一个字符串数组传递给FFI调用,我想这样做:
library passArray: {'hola' 'manola'} size: 2.
其中passArray:size:
类似于:
passArray: anArray size: anInteger
<cdecl: void 'someFunction' (void* size_t)>
^ self externalCallFailed
但无论我尝试什么,它都以“无法强迫论证”而失败。
有什么想法吗? (是的,我可以“外化”所有字符串,然后构建一个指针数组,但我认为我不需要它。
答案 0 :(得分:1)
我更喜欢使用共享内存方法,在Smalltalk和C之间共享数据。共享内存的好处是你不必担心在Smalltalk和C之间移动数据,因为数据可以从C访问和Smalltalk在同一时间。此外,由于共享内存在VM和GC边界之外运行,因此您不必担心数据被垃圾收集并最终导致内存泄漏。
我不知道如何在Squeak上做这个,因为我是Pharo用户,但必须是类似的东西。
在C面
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <iostream>
#include <string>
#define FILEPATH "mmapped.bin"
#define NUMINTS (1000)
#define FILESIZE (NUMINTS * sizeof(int))
int main(int argc, char *argv[])
{
int i;
int fd;
std::string* map;
std::string map_contents;
fd = open(FILEPATH, O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
exit(EXIT_FAILURE);
}
map = (std::string*)mmap(0, FILESIZE, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
/* Read the file int-by-int from the mmap
*/
map_contents = std::string(*map);
std::cout<<"type of map is : "<< typeid(map).name()<<"\n";
std::cout<<"I am reading from mmap : "<< map_contents <<" \n";
if (munmap(map, FILESIZE) == -1) {
perror("Error un-mmapping the file");
}
close(fd);
return 0;
}
在Pharo方面
examples
retrieveSharedValueStep1
<example>
"This method is an example that retrieves a struct from a shared memory section, in order for this example to work you need you first copy paste the contents of the Example Souce Code of the C++ file in the comment section (you can also find the cpp file in the same directory where the git repo has been downloaded) of this class to a C++ source code file and compile it a run then replace the path of in this code of CPPBridge openFile: with the correct path of the bin that the C++ files has created , in order for this to work also you need to execute the C++ example first so it creates and file and share the memory.
After executing this method you can execute retrieveSharedValueStep2 to unmap and close the memory mapped file (keeps sharing the memory it just does not store it to the file)"
|instance fdNumber lseek mmapPointer data struct|
"Let's create an instance just an an example but we wont use it because we can use either class method or intance methods. You would want to use instance method if you want to open multiple memory mapped files meaning multiple areas of shared memory. Class methods for using just one"
instance := CPPBridge new.
"Warning !!! You must change the path to the file that is located in your hard drive. The file should be at the same location you built atlas-server.cpp which is responsible for creating the file. The number returned is a number that OS uses to identify the image , flag O_RDWR is just a number that states that we want to write and read the file"
fdNumber := CPPBridge openFile: '/Users/kilon/git/Pharo/CPPBridge/mmapped.bin' flags: (O_RDWR) .
"lseek is used to stretch the file to a new size"
lseek := CPPBridge lSeek_fd: fdNumber range:3999 value:0.
"this is the most importan method, this method maps the file to memmory , which means it loads its contents into memory and associates the memory with the file. PROT_READ means we want to write the memory , PROT_WRITE to write the memory and MAP_SHARED is the most importan because it defines the memory area as shared so we can access it from other application"
mmapPointer := CPPBridge mmap_adress: 0 fileSize:4000 flag1: (PROT_READ | PROT_WRITE )flag2: MAP_SHARED fd: fdNumber offset: 0 .
"This assigns the pointer to our Pharo structure so we can use it to get the contents of the C structure located in the shared memory"
struct := CPPStruct pointTo: (mmapPointer getHandle ).
"data here serves as a convenience array its not necessary we use it just to collect information about the instance, the fd number of the file, the streched size of the file, the adress (point) where the file is mapped to in memory and struct that contains the values of the C struct that we received"
data :={ instance. fdNumber . lseek. mmapPointer . struct}.
data inspect.
"Store data to the class so we can use it in the second method"
ExampleDATA := data.
^data
"
Its also possible to write to the shared memory , in this case we use once again the C struct which has the following members (variables) :
1) data = char[3000] this is where we store the string
2) count = int this is where we store the size of the string
struct := {(mmapPointer getHandle copyFrom: 1 to:3000 )asString . (mmapPointer getHandle integerAt: 3001 size:4 signed: false)}.
mmapPointer is the pointer that points to the first byte of the shared memory.
getHandle gives us the memory adress that the pointer points to
copyFrom:1 to:3000 copies byte from byte 0 (remember C counts from 0 , Pharo counts from 1) to byte 3000 because the string we store is stored as a char array of 3000 elements, each element is a char, each char is 1 byte in leght and represents a single character of the string. This gets the value of the first struct member.
on the other hand integerAt: 3001 size: 4 signed: false returns us the value count memeber of the C struct . its an integer in position 3001 because our string is a char[3000] and the size is 4 bytes because its an C int, signed false because we use no negative values because it does not make sense for a string to have negative length. This gets the value of the second struct member"
您可以通过访问我的github repo找到更多信息,因为我已将所有这些打包到我称为CPP的库中(主要目的是使用C ++,但它也适用于C)
我的方法的优点是:
您不必担心GC
您无需复制数据
因为共享内存使用OS的内存映射文件系统 内核你得到的速度加上你的共享内存总是如此 自动存储到文件中,因此您无需担心 在发生崩溃时丢失数据
mmap文件的工作方式类似于吱吱声图像,存储实时 状态
mmap因为它是一个OS内核函数,但它支持所有操作系统 也是大多数编程语言,这意味着你可以使用它 您想要的任何编程语言
<强>缺点强>