unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: taylanbayirli@gmail.com (Taylan Ulrich Bayırlı/Kammer)
To: Marko Rauhamaa <marko@pacujo.net>
Cc: "Ludovic Courtès" <ludo@gnu.org>, guile-user@gnu.org
Subject: Re: Guile bugs
Date: Fri, 21 Jul 2017 18:33:58 +0200	[thread overview]
Message-ID: <87eft94umh.fsf@gmail.com> (raw)
In-Reply-To: <87pocvc5ya.fsf@elektro.pacujo.net> (Marko Rauhamaa's message of "Thu, 20 Jul 2017 21:35:09 +0300")

Marko Rauhamaa <marko@pacujo.net> writes:

>>>  1. Does Guile offer mmap to Scheme code?
>>
>> It's pretty easy to call C functions from Scheme.
>
> Not good enough. I would have to precompile libraries for different
> target platforms (or require the target environment to have a C
> compiler). Besides, the need is quite generic and shouldn't require an
> extension.

One can also call standard C functions through the same Guile interface
that I used in my example, so one can e.g. turn mmap() into a Scheme
procedure.  open() already happens to exist in the form of 'open-fdes',
and to get the file size one can use (stat:size (stat "filepath")).

The only remaining problem is that mmap's flag constants like PROT_READ,
MAP_SHARED, etc. don't exist in Guile, and can't be reached through FFI
since they're #define constants.  Sans that problem, this works:

)> ,use (system foreign)
)> ,use (rnrs bytevectors)
)> (define mmap
     (pointer->procedure
      '*
      (dynamic-func "mmap" (dynamic-link))                   ;[1]
      (list '* size_t int int int size_t)))
)> (define fd (open-fdes "/home/taylan/todo" O_RDONLY))
)> (define size (stat:size (stat fd)))
)> (define file-ptr (mmap (make-pointer 0) size 1 2 fd 0))   ;[2]
)> (define file-bv (pointer->bytevector file-ptr size))
)> (integer->char (bytevector-u8-ref file-bv 0))
$1 = #\-

[1] When dynamic-link is called with no argument, it returns a "global
    symbol handle" that contains the lib bindings of the Guile process.
    A Guile process is sure to have "mmap" linked in.

[2] The numbers 1 and 2 happen to correspond to PROT_READ and
    MAP_PRIVATE on my system.

As a temporary workaround to the problem that mmap flag constants don't
exist in Guile, one could define them manually for the systems one
intends to support in the foreseeable future.

>>>  3. How do I efficiently encode information in a bytevector in Scheme
>>>     code?
>>
>> What sort of data?
>>
>> I have a library called bytestructures that imitates the C type system
>> within Scheme, to be used on bytevectors that contain data structures
>> generated by C code, though the library is built upon a generic core
>> with which other structures can be declared as well.  Not sure if this
>> helps you:
>>
>> https://github.com/TaylanUB/scheme-bytestructures/
>
> That's precisely it. It would be nice to have it as part of standard
> Guile.

Maybe one day.  These days I don't have much time to work on it.  Maybe
I'll see that I provide a GNU-compliant build system first so people can
install it easily.

In case you use Guix, someone made a package for it, so you could
install it through there too.

> I wonder, though, if doing that is fast enough in Scheme code.

Since Guile supports syntax-case, the "macro API" of the library can be
used so the library itself adds absolutely zero runtime overhead.

(This API puts a few limits on the full feature set, but the standard
modules that mimic C types don't use those features.)


Taylan



  parent reply	other threads:[~2017-07-21 16:33 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-12 23:56 How to make GNU Guile more successful Amirouche
2017-02-13  0:21 ` Amirouche
2017-02-13 11:06 ` Arne Babenhauserheide
2017-02-13 12:14   ` Arne Babenhauserheide
2017-02-13 20:20   ` Amirouche
2017-02-13 23:08     ` Arne Babenhauserheide
2017-02-13 20:28   ` Panicz Maciej Godek
2017-02-13 20:42     ` Amirouche
2017-02-13 22:34     ` Marko Rauhamaa
2017-02-13 23:56       ` Arne Babenhauserheide
2017-02-14  0:18         ` David Kastrup
2017-02-14 22:21           ` Arne Babenhauserheide
2017-02-15 17:03           ` Christopher Allan Webber
2017-02-16 19:18             ` sirgazil
2017-02-16 20:26               ` Amirouche
2017-02-14  5:59         ` Marko Rauhamaa
2017-02-14 19:36           ` Linas Vepstas
2017-02-14 20:54             ` Marko Rauhamaa
2017-02-14 22:20           ` Arne Babenhauserheide
2017-02-13 22:54     ` Arne Babenhauserheide
2017-02-14  9:54       ` Panicz Maciej Godek
2017-02-14 21:35         ` Arne Babenhauserheide
2017-03-01 19:21           ` Amirouche
2017-03-10 20:23             ` Amirouche
2017-07-14 21:54     ` Linas Vepstas
2017-07-14 21:59       ` Marko Rauhamaa
2017-07-15 10:10       ` Jan Wedekind
2017-07-15 12:55         ` Nala Ginrut
2017-07-15 12:58           ` Nala Ginrut
2017-07-15 22:17           ` Jan Wedekind
2017-07-16  9:54             ` Nala Ginrut
2017-07-17 18:52         ` Arun Isaac
2017-07-18 11:22         ` Ernest Adrogué
2017-07-16  8:30       ` Freja Nordsiek
2017-07-16  9:18         ` Marko Rauhamaa
2017-07-16 10:11           ` Freja Nordsiek
2017-07-16 10:31             ` Marko Rauhamaa
2017-07-16 10:39               ` Freja Nordsiek
2017-07-16 10:45                 ` Freja Nordsiek
2017-07-20 15:28       ` Guile bugs Ludovic Courtès
2017-07-20 16:22         ` Marko Rauhamaa
2017-07-20 18:26           ` Taylan Ulrich Bayırlı/Kammer
2017-07-20 18:35             ` Marko Rauhamaa
2017-07-20 20:41               ` Ludovic Courtès
2017-07-20 22:23                 ` Marko Rauhamaa
2017-07-21  4:05                   ` Mark H Weaver
2017-07-21  6:15                     ` Marko Rauhamaa
2017-07-21  8:16                       ` Chris Vine
2017-07-21  8:27                         ` Marko Rauhamaa
2017-07-21  9:17                       ` Mark H Weaver
2017-07-21 10:08                         ` Marko Rauhamaa
2017-07-21 10:22                           ` David Kastrup
2017-09-09 21:14                       ` Linas Vepstas
2017-09-09 22:31                         ` Marko Rauhamaa
2017-09-09 23:02                           ` Linas Vepstas
2017-07-21 16:33               ` Taylan Ulrich Bayırlı/Kammer [this message]
2017-07-21 17:12                 ` Marko Rauhamaa
2017-07-21 14:19           ` Matt Wette
2017-09-09 20:30         ` Linas Vepstas
2017-09-10 13:11           ` Ludovic Courtès
2017-09-10 19:56             ` Linas Vepstas
2017-09-11  7:26               ` Ludovic Courtès
2017-09-11  8:10                 ` Marko Rauhamaa
2017-09-11 11:34                   ` Ludovic Courtès
2017-09-14 17:54                 ` Linas Vepstas
2017-09-15  7:56                   ` Ludovic Courtès
2017-09-19 11:04                     ` Linas Vepstas
2017-09-19 20:18                       ` Chris Vine
2017-09-19 20:21                         ` Chris Vine
2017-09-19 23:39                           ` Nala Ginrut

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=87eft94umh.fsf@gmail.com \
    --to=taylanbayirli@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=ludo@gnu.org \
    --cc=marko@pacujo.net \
    /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).