From: Matt Wette <matt.wette@gmail.com>
To: 27782@debbugs.gnu.org
Subject: bug#27782: mmap for guile 2.2.2
Date: Sat, 28 Oct 2017 08:25:27 -0700 [thread overview]
Message-ID: <68E9A19E-CC14-4FEF-96C3-4FCB6F6AD74A@gmail.com> (raw)
In-Reply-To: <CD68D357-0F0F-471C-BEC1-EB601844A04E@gmail.com>
I worked on the code a bit more. It is currently implemented as a #include "mman.c" in filesys.c
and another file "mman.c".
I believe this needs to be reviewed by someone who understands the constraints on foriegn pointers
and finalization better than I do. Also, there is a comment to add a procedure mmap/no-search to
guarantee that the allocated region is not searched by the GC for pointers.
The following patch is against guile-2.2.2:
--- libguile/filesys.c.orig 2017-07-21 06:14:18.000000000 -0700
+++ libguile/filesys.c 2017-10-27 15:40:04.000000000 -0700
@@ -1828,9 +1828,14 @@
\f
+#include "mman.c"
+
void
scm_init_filesys ()
{
+#ifdef HAVE_SYS_MMAN_H
+ init_mman();
+#endif
#ifdef HAVE_POSIX
scm_tc16_dir = scm_make_smob_type ("directory", 0);
scm_set_smob_free (scm_tc16_dir, scm_dir_free);
--- libguile/mman.c.orig 2017-07-20 17:06:55.000000000 -0700
+++ libguile/mman.c 2017-10-28 08:12:46.000000000 -0700
@@ -0,0 +1,165 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifdef HAVE_SYS_MMAN_H
+# include <sys/mman.h>
+# include <errno.h>
+#endif
+
+#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MAP_ANONYMOUS)
+
+#include "libguile/_scm.h"
+#include "libguile/smob.h"
+#include "libguile/fdes-finalizers.h"
+#include "libguile/feature.h"
+
+SCM_API SCM scm_mmap (SCM addr, SCM len, SCM prot, SCM flags, SCM fd,
+ SCM offset);
+SCM_API SCM scm_munmap (SCM bvec);
+void init_mman(void);
+static void mmap_finalizer (void *ptr, void *data);
+
+
+SCM_DEFINE (scm_mmap, "mmap", 2, 4, 0,
+ (SCM addr, SCM len, SCM prot, SCM flags, SCM fd, SCM offset),
+ "mmap addr len [prot [flags [fd [offset]]]]"
+ "See the man page. Returns a bytevector."
+ "ordinarily convert to bytevector using pointer->bytevector. "
+ "Note that the region returned by mmap is not (?) searched "
+ "by the garbage collector. Defaults:\n"
+ " PROT (logior PROT_READ PROT_WRITE)\n"
+ " FLAGS (logior MAP_ANON MAP_PRIVATE)\n"
+ " FD -1\n"
+ " OFFSET 0\n"
+ "@example\n(define reg (mmap %null-pointer #x1000)\n"
+ "@end example"
+ )
+#define FUNC_NAME s_scm_mmap
+{
+ void *c_mem, *c_addr;
+ size_t c_len;
+ int c_prot, c_flags, c_fd;
+ scm_t_off c_offset;
+ SCM pointer, bvec;
+
+ if (SCM_POINTER_P (addr))
+ c_addr = SCM_POINTER_VALUE (addr);
+ else if (scm_is_integer (addr))
+ c_addr = (void*) scm_to_uintptr_t (addr);
+ else
+ SCM_MISC_ERROR("bad addr", addr);
+
+ c_len = scm_to_size_t (len);
+
+ if (SCM_UNBNDP (prot))
+ c_prot = PROT_READ | PROT_WRITE;
+ else
+ c_prot = scm_to_int (prot);
+
+ if (SCM_UNBNDP (flags))
+ c_flags = MAP_ANON | MAP_PRIVATE;
+ else
+ c_flags = scm_to_int (flags);
+
+ if (SCM_UNBNDP (fd))
+ c_fd = -1;
+ else
+ c_fd = scm_to_int (fd);
+
+ if (SCM_UNBNDP (fd))
+ c_offset = 0;
+ else
+ c_offset = scm_to_off_t (offset);
+
+ c_mem = mmap(c_addr, c_len, c_prot, c_flags, c_fd, c_offset);
+ if (c_mem == MAP_FAILED)
+ SCM_SYSERROR; /* errno set */
+
+ pointer = scm_cell (scm_tc7_pointer, (scm_t_bits) c_mem);
+ bvec = scm_c_take_typed_bytevector((signed char *) c_mem + c_offset, c_len,
+ SCM_ARRAY_ELEMENT_TYPE_VU8, pointer);
+ /* if sizeof(void*) < sizeof(size_t) we are in trouble: */
+ scm_i_set_finalizer (SCM2PTR (bvec), mmap_finalizer, (void*) c_len);
+ return bvec;
+}
+static void
+mmap_finalizer (void *ptr, void *data)
+{
+ void *c_addr;
+ intptr_t c_len;
+ int res;
+
+ c_addr = (void *) SCM_POINTER_VALUE (SCM_PACK_POINTER (ptr));
+ c_len = (intptr_t) data;
+ res = munmap(c_addr, c_len);
+ if (res != 0) SCM_SYSERROR;
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_munmap, "munmap", 1, 0, 0,
+ (SCM bvec),
+ "See the man page. Given bytevector unmap."
+ )
+#define FUNC_NAME s_scm_munmap
+{
+ void *c_addr;
+ size_t c_len;
+ int c_res;
+
+ SCM_VALIDATE_BYTEVECTOR (1, bvec);
+
+ c_addr = (void *) SCM_BYTEVECTOR_CONTENTS (bvec);
+ c_len = SCM_BYTEVECTOR_LENGTH (bvec);
+
+ c_res = munmap(c_addr, c_len);
+ if (c_res == -1)
+ SCM_SYSERROR; /* errno set */
+
+ // TODO: clean up bytevector
+ return SCM_UNSPECIFIED;
+}
+#undef FUNC_NAME
+
+void init_mman(void) {
+#ifdef PROT_NONE
+ scm_c_define ("PROT_NONE", scm_from_int (PROT_NONE));
+#endif
+#ifdef PROT_
+ scm_c_define ("PROT_READ", scm_from_int (PROT_READ));
+#endif
+#ifdef PROT_
+ scm_c_define ("PROT_WRITE", scm_from_int (PROT_WRITE));
+#endif
+#ifdef PROT_
+ scm_c_define ("PROT_EXEC", scm_from_int (PROT_EXEC));
+#endif
+
+#ifdef MAP_ANONYMOUS
+ scm_c_define ("MAP_ANONYMOUS", scm_from_int (MAP_ANONYMOUS));
+#endif
+#ifdef MAP_ANON
+ scm_c_define ("MAP_ANO
next prev parent reply other threads:[~2017-10-28 15:25 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-21 13:39 bug#27782: [wishlist] scheme level mmap Matt Wette
[not found] ` <handler.27782.B.150064439025677.ack@debbugs.gnu.org>
2017-07-21 14:35 ` bug#27782: Acknowledgement ([wishlist] scheme level mmap) Matt Wette
2017-10-28 15:25 ` Matt Wette [this message]
2017-10-28 17:09 ` bug#27782: mmap for guile 2.2.2 Matt Wette
2017-11-24 15:54 ` bug#27782: mmap for guile Matt Wette
2017-11-24 16:22 ` Nala Ginrut
2017-11-24 17:09 ` Matt Wette
2017-11-25 14:41 ` Matt Wette
2017-11-25 16:17 ` Nala Ginrut
2020-07-04 19:40 ` bug#27782: new patch for mma Matt Wette
2020-07-09 12:45 ` Ludovic Courtès
2022-12-21 1:21 ` bug#27782: patch to add support for mmap and friends Matt Wette
2022-12-22 18:49 ` Matt Wette
2023-01-14 0:49 ` bug#27782: patch " Matt Wette
2023-02-14 14:50 ` bug#27782: mman patch for v3.0.9 Matt Wette
2023-03-01 13:31 ` Matt Wette
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=68E9A19E-CC14-4FEF-96C3-4FCB6F6AD74A@gmail.com \
--to=matt.wette@gmail.com \
--cc=27782@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).