Version française
Home     About     Download     Resources     Contact us    
Browse thread
mmap() and strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] mmap() and strings
From: Julien Cristau <julien.cristau@ens-lyon.fr>
> I wrote:
> > > We thought we could use mmap(2), 
> > > but there seems to be no easy solution 
> > > to mmap() a memory region and treat it as a string in ocaml. 
> > 
> On 08/12/2004-21:18, Basile STARYNKEVITCH wrote:
> > Use Bigarray-s for that. They can mmap files (on Unix & Linux) and are
> > already in Ocaml 3.08
> > 
> Actually, i had a look at bigarrays, and it's one of the solutions I 
> considered. However, I'd like to keep strings as data structure, because 
> the operations I have to perform take a string as an argument, and not a 
> (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t, 
> and it would be a pain to change all these functions (if I change them, 
> I'll probably bind mmap() and munmap() directly and call them with 
> MAP_ANONYMOUS, but I'd rather not do that).

I don't know exactly your goal, but if it is just that you don't want
to write a single line of C (and all the boilerplate), then you can
always do some magic (note that this is going to be very dark magic!)

The main problem is way string length is represented.
What you have to do is create a pseudo block header inside a bigarray.
The simplest way is to first create a string of the right size, and
then copy it byte by byte to the bigarray, starting with index (-4)
(for a 32-bit machine) and ending at ((len/4+1)*4) (the last by of the
last word of the string encodes part of the length), using
String.unsafe_get or String.unsafe_blit (more subtle).
Then you want to get a pointer at offset 4 in the string.
Not too hard either:
    (Obj.magic
       (!(snd (Obj.magic biga : Obj.t * int ref)) + 2)
       : string)

Now this has lots of dependencies on the behavior of the compiler and
how bigarrays are represented, but I believe this should work.
Not however that if you have problems with that, debugging can become
hairy, in this completely unsafe world.

Cheers,

Jacques Garrigue