From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 13E0FBB81 for ; Thu, 9 Dec 2004 02:09:19 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id iB919I9i024186 for ; Thu, 9 Dec 2004 02:09:18 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA04150 for ; Thu, 9 Dec 2004 02:09:18 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iB919GG1003096 for ; Thu, 9 Dec 2004 02:09:17 +0100 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id iB919FQI028307 for ; Thu, 9 Dec 2004 10:09:15 +0900 (JST) Date: Thu, 09 Dec 2004 10:09:01 +0900 (JST) Message-Id: <20041209.100901.75479815.garrigue@math.nagoya-u.ac.jp> To: caml-list@inria.fr Subject: Re: [Caml-list] mmap() and strings From: Jacques Garrigue In-Reply-To: <20041208205042.GD1840@ens-lyon.fr> References: <20041208200458.GC1840@ens-lyon.fr> <20041208202428.GA9878@ours.starynkevitch.net> <20041208205042.GD1840@ens-lyon.fr> X-Mailer: Mew version 4.0.64 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 41B7A5BE.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 41B7A5BC.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ens-lyon:01 wrote:01 ocaml:01 basile:01 wrote:01 ocaml:01 bigarrays:01 char:01 bigarray:01 elt:01 bigarray:01 byte:01 byte:01 encodes:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: From: Julien Cristau > 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