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: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>

> 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)

Sorry, the above code is wrong.
The right one is
    let str_of_bigarray biga =
      (Obj.magic (snd (Obj.magic biga : Obj.t * int) + 2) : string)
The "+2" is intended to add 4 to the pointer stored in the second word
of the bigarray, which happens to be the pointer to the raw data.
And for initialization
    let copy_string s biga =
      String.unsafe_blit s (-4) (str_of_bigarray biga) (-4)
         ((String.length s / 4 + 2)*4)
After this, you can use it as a normal string.
(Again, this code comes with no warranty, use at your own risk)

Jacques Garrigue