unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Nala Ginrut <nalaginrut@gmail.com>
To: Matt Wette <matt.wette@gmail.com>
Cc: 27782@debbugs.gnu.org
Subject: bug#27782: mmap for guile
Date: Sat, 25 Nov 2017 00:22:14 +0800	[thread overview]
Message-ID: <CAPjoZoeWyRx5_eTogzhuUbARZyPtp4OsbCjtGnkp_TGvYfkfag@mail.gmail.com> (raw)
In-Reply-To: <D6FB4C4A-D4AB-421F-ADD5-CA8ECDA8F37E@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 7196 bytes --]

Thanks for the work! Could you please add MAP_POPULATE too?

2017年11月24日 下午11:55,"Matt Wette" <matt.wette@gmail.com>写道:

>
>
>
>
>
> I did a little more on this.  Here is the latest.
> It provides mmap (not searched) and mmap/search (searched for pointers to
> GC).
>
>
> --- libguile/filesys.c.orig     2017-03-01 10:54:31.000000000 -0800
> +++ libguile/filesys.c  2017-10-28 10:05:10.000000000 -0700
> @@ -1828,9 +1828,14 @@
>
>
>
> +#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-10-28 10:05:10.000000000 -0700
> +++ libguile/mman.c     2017-11-04 09:23:35.000000000 -0700
> @@ -0,0 +1,199 @@
> +// mman.c - v171104a
> +#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_search (SCM addr, SCM len, SCM prot, SCM flags, SCM
> fd,
> +                            SCM offset);
> +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_search, "mmap/search", 2, 4, 0,
> +            (SCM addr, SCM len, SCM prot, SCM flags, SCM fd, SCM offset),
> +           "mmap addr len [prot [flags [fd [offset]]]]"
> +           "See the unix man page for mmap.  Returns a bytevector."
> +           "Note that the region allocated will be searched by the
> garbage"
> +           "collector for pointers.  \n"
> +           " 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/search %null-pointer #x1000)\n"
> +           "@end example"
> +           )
> +#define FUNC_NAME s_scm_mmap_search
> +{
> +  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;
> +}
> +#undef FUNC_NAME
> +
> +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."
> +           "Note that the region returned by mmap will NOT be searched "
> +           "by the garbage collector for pointers.\n"
> +           "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 bvec-1MB (mmap 0 #x100000)\n"
> +           "@end example"
> +           )
> +#define FUNC_NAME s_scm_mmap
> +{
> +  SCM bvec;
> +  void *c_mem;
> +  size_t c_len;
> +
> +  bvec = scm_mmap_search(addr, len, prot, flags, fd, offset);
> +  c_mem = SCM_BYTEVECTOR_CONTENTS(bvec);
> +  c_len = SCM_BYTEVECTOR_LENGTH(bvec);
> +
> +  /* tell GC not to scan for pointers */
> +  GC_exclude_static_roots(c_mem, (char*) c_mem + 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_ANON", scm_from_int (MAP_ANON));
> +#endif
> +#ifdef MAP_FILE
> +  scm_c_define ("MAP_FILE", scm_from_int (MAP_FILE));
> +#endif
> +#ifdef MAP_FIXED
> +  scm_c_define ("MAP_FIXED", scm_from_int (MAP_FIXED));
> +#endif
> +#ifdef MAP_HASSEMAPHORE
> +  scm_c_define ("MAP_HASSEMAPHORE", scm_from_int (MAP_HASSEMAPHORE));
> +#endif
> +#ifdef MAP_PRIVATE
> +  scm_c_define ("MAP_PRIVATE", scm_from_int (MAP_PRIVATE));
> +#endif
> +#ifdef MAP_SHARED
> +  scm_c_define ("MAP_SHARED", scm_from_int (MAP_SHARED));
> +#endif
> +#ifdef MAP_NOCACHE
> +  scm_c_define ("MAP_NOCACHE", scm_from_int (MAP_NOCACHE));
> +#endif
> +  scm_c_define ("PAGE_SIZE", scm_from_int (getpagesize()));
> +}
> +
> +#endif /* HAVE_SYS_MMAN_H */
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 8998 bytes --]

  reply	other threads:[~2017-11-24 16:22 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 ` bug#27782: mmap for guile 2.2.2 Matt Wette
2017-10-28 17:09   ` Matt Wette
2017-11-24 15:54 ` bug#27782: mmap for guile Matt Wette
2017-11-24 16:22   ` Nala Ginrut [this message]
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=CAPjoZoeWyRx5_eTogzhuUbARZyPtp4OsbCjtGnkp_TGvYfkfag@mail.gmail.com \
    --to=nalaginrut@gmail.com \
    --cc=27782@debbugs.gnu.org \
    --cc=matt.wette@gmail.com \
    /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).