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 E8472BB81 for ; Thu, 9 Dec 2004 02:42:21 +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 iB91gLeM027110 for ; Thu, 9 Dec 2004 02:42:21 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA04982 for ; Thu, 9 Dec 2004 02:42:21 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id iB91gJhD027104 for ; Thu, 9 Dec 2004 02:42:20 +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 iB91gIEm029148; Thu, 9 Dec 2004 10:42:18 +0900 (JST) Date: Thu, 09 Dec 2004 10:42:04 +0900 (JST) Message-Id: <20041209.104204.95951378.garrigue@math.nagoya-u.ac.jp> To: caml-list@inria.fr Subject: Re: [Caml-list] mmap() and strings From: Jacques Garrigue In-Reply-To: <20041209.100901.75479815.garrigue@math.nagoya-u.ac.jp> References: <20041208202428.GA9878@ours.starynkevitch.net> <20041208205042.GD1840@ens-lyon.fr> <20041209.100901.75479815.garrigue@math.nagoya-u.ac.jp> 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 41B7AD7D.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 41B7AD7B.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 bigarray:01 byte:01 byte:01 bigarray:01 encodes:01 blit:01 pointer:01 pointer:01 blit:01 strings:01 jacques:01 jacques:01 unsafe:01 unsafe: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: Jacques Garrigue > 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