如何在sbcl中存储映射tmpfs文件? [英] How do I memory map tmpfs files in sbcl?

查看:91
本文介绍了如何在sbcl中存储映射tmpfs文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所述。我想使用共享内存在两个Lisp进程之间进行通信。关于如何执行此操作的任何指示?

Exactly as the question says. I want to use shared memory to communicate between two lisp processes. Any pointers on how to do that?

我可以在Clozure上看到一些有关执行此操作的教程:-

I can see some tutorials on doing this in clozure at :-

http://ccl.clozure.com/manual/chapter4.7.html

有人可以指向我指向类似的库来使用sbcl吗?

Can someone point me to a similar library to do this with sbcl?

推荐答案

对于可移植的实现,您可能想使用 osicat 库,该库为 osicat-posix 包。

For a portable implementation, you might want to use the osicat library, which provides a CFFI wrapper for many POSIX calls in the osicat-posix package.

http://wandrian.net/2012-04-07 -1352-mmap-files-in-lisp.html (由Nicolas Martyanoff撰写)。

There is a very nice and short article with code for using it at http://wandrian.net/2012-04-07-1352-mmap-files-in-lisp.html (by Nicolas Martyanoff).

为了保护这一点,我主要从那里引用:

To preserve that, I mostly cite from there:


通过使用 osicat打开文件来完成文件映射-posix:open ,使用 fstat 读取其大小,然后调用 mmap 。一旦文件被映射,我们就可以关闭文件描述符了。

Mapping a file is done by opening it with osicat-posix:open, reading its size with fstat, then calling mmap. Once the file has been mapped we can close the file descriptor, it’s not needed anymore.



(defun mmap-file (path)
  (let ((fd (osicat-posix:open path (logior osicat-posix:o-rdonly))))
    (unwind-protect
         (let* ((size (osicat-posix:stat-size (osicat-posix:fstat fd)))
                (addr (osicat-posix:mmap (cffi:null-pointer) size
                                         (logior osicat-posix:prot-read)
                                         (logior osicat-posix:map-private)
                                         fd 0)))
           (values addr size))
      (osicat-posix:close fd))))




mmap文件功能返回两个值:内存映射的地址及其大小。

The mmap-file function returns two values: the address of the memory mapping and its size.

取消映射此内存块是通过 osicat-posix:munmap 。

Unmapping this chunk of memory is done with osicat-posix:munmap.

让我们添加一个宏以安全地映射和取消格式p个文件:

Let’s add a macro to safely map and unmap files:



(defmacro with-mmapped-file ((file addr size) &body body)
  (let ((original-addr (gensym "ADDR-"))
        (original-size (gensym "SIZE-")))
    `(multiple-value-bind (,addr ,size)
         (mmap-file ,file)
       (let ((,original-addr ,addr)
             (,original-size ,size))
         (unwind-protect
              (progn ,@body)
           (osicat-posix:munmap ,original-addr ,original-size))))))

此宏 mmap 保存给定文件,并将两个给定变量绑定到其地址和大小。然后,您可以使用 cffi:inc-pointer 计算地址指针,并使用 cffi:mem-aref 访问文件内容。您可能希望围绕它构建自己的包装器,以表示文件的格式(例如UTF-8中的纯文本)。

This macro mmaps the given file and binds the two given variables to its address and and size. You can then calculate address pointers with cffi:inc-pointer and access the file contents with cffi:mem-aref. You might want to build your own wrappers around this to represent the format of your file (e. g. plain text in UTF-8).

(与上面的链接比较) ,我删除了 osicat-posix:munmap 的包装,使其成为具有完全相同的签名和效果的函数,因为它对我来说似乎是多余的。)

(In comparison to the posting linked above, I removed the wrapping of osicat-posix:munmap into another function of exactly the same signature and effect, because it seemed superfluous to me.)

这篇关于如何在sbcl中存储映射tmpfs文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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