我更喜欢使用在 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;
}
在法罗一侧
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 存储库找到更多信息,因为我已将所有这些打包到我称为 CPP 的库中(主要目的是使用 C++,但它也适用于 C)
https://github.com/kilon/CPP
我的方法的优点是:
您不必担心 GC
你不需要复制数据
因为共享内存使用操作系统的内存映射文件系统
内核,您将获得大量速度,而且您的共享内存始终
自动存储到文件中,因此您无需担心
发生崩溃时丢失数据
mmap 文件的工作方式类似于 squeak 图像,实时存储
状态
mmap 因为它是一个操作系统内核函数,它在所有操作系统中都受支持,但
也是大多数编程语言,这意味着您可以将其与
任何你想要的编程语言
缺点
- 因为这在手动内存管理区域内有效,所以您会丢失
GC 的优势,因此您需要自己处理该内存
手动
- 由于它的外部 GC,您还失去了许多动态能力
Smalltalk 对象,因此您必须遵守 C 规则。的
当然没有人阻止您将数据复制为 Smalltalk
对象,如果你愿意或将数据传递给现有的 Smalltalk
对象
- 如果你搞砸了,你会像平常一样轻松地使 Squeak VM 崩溃
内存泄漏