caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: caml-list@inria.fr
Subject: Re: [Caml-list] mmap() and strings
Date: Thu, 09 Dec 2004 10:42:04 +0900 (JST)	[thread overview]
Message-ID: <20041209.104204.95951378.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20041209.100901.75479815.garrigue@math.nagoya-u.ac.jp>

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


  reply	other threads:[~2004-12-09  1:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-08 20:04 Julien Cristau
2004-12-08 20:24 ` [Caml-list] " Basile STARYNKEVITCH
2004-12-08 20:50   ` Julien Cristau
2004-12-09  1:09     ` Jacques Garrigue
2004-12-09  1:42       ` Jacques Garrigue [this message]
2004-12-09 10:32         ` David Baelde

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041209.104204.95951378.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    /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 NNTP newsgroup(s).