[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: mmap in guile -- guile memory management question
From: |
Ludovic Courtès |
Subject: |
Re: mmap in guile -- guile memory management question |
Date: |
Mon, 13 Nov 2006 10:15:59 +0100 |
User-agent: |
Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) |
Hi,
Daniel Ridge <address@hidden> writes:
> I use mmap in guile via a very simple C module and I have a piece of
> complicated Guile hackery that I would like to simplify. I'm hoping
> someone will embarrass me with 3 rational lines to solve my problem.
>
> My problem is that I don't know how to manufacture a new Guile string
> with a pre-existing pointer (returned by mmap) and a length that I
> specify. Instead, I grab a string from Guile and whack its contents
> and length with my own and then put back a reasonable value in a
> guardian at GC time.
First, you should really use 1.8, if that's possible. ;-)
Then, turning arbitrary file contents into a string does not sound like
a reasonable option: a file may contain non-printable characters or
non-characters (in the Unicode sense). Thus, you may want to use octet
vectors to represent arbitrary file contents, rather than strings. In
1.8, you would use SRFI-4 octet vectors to that end (passing the address
returned by `mmap ()' to `scm_take_u8vector ()' and making the returned
vector uncollectable so that `free ()' isn't eventually invoked on the
`mmap' address).
However, ports are generally more appropriate than vectors to handle
arbitrarily large file contents. Thus, I would recommend using a
regular file port and then using something like `uniform-array-read!'
(in 1.8) to get part of the file contents into a vector. Note that the
current implementation of file ports doesn't use `mmap ()' behind the
scenes (but it could be changed to do so).
Hope this helps,
Ludovic.