guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guile bugs


From: Taylan Ulrich Bayırlı/Kammer
Subject: Re: Guile bugs
Date: Fri, 21 Jul 2017 18:33:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux)

Marko Rauhamaa <address@hidden> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]