From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 2FE80BBAF for ; Mon, 23 Aug 2010 02:42:25 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqUBALdgcUzRVdg2kGdsb2JhbACDF49ajTMIFQEBAQEJCQwHEQMfniOIXIJPhRSJAgEBAwWBHYMicwSJdohA X-IronPort-AV: E=Sophos;i="4.56,254,1280700000"; d="scan'208";a="57660328" Received: from mail-qw0-f54.google.com ([209.85.216.54]) by mail2-smtp-roc.national.inria.fr with ESMTP; 23 Aug 2010 02:42:24 +0200 Received: by qwg5 with SMTP id 5so5874101qwg.27 for ; Sun, 22 Aug 2010 17:42:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:sender:received :in-reply-to:references:date:x-google-sender-auth:message-id:subject :from:to:cc:content-type:content-transfer-encoding; bh=8n/WgA3Usc5lywdrqD5GXnUWtl9L4kBXxGqQ1c/BWUI=; b=onBGahhRafryK/O2vRzyzVi6y2SYnFwHjICvETgtX4wHfUrwJvIOtytmttw0BvB2mK Ue+zwiEI9owEx3BeVXxkublLX8JktQUseMM59HjEZmYBBlQR2lfjkCGbpxTKkvjr1cD4 Sw3aKRIVPhEp4jGBn5P4x0MbhfhqBZzlZLFKc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=wRAXARdzc9tQuixprJtsc0vi7ROBrWq5k/W0o5FS+4OOfcb+dnkwD4/CYT1klv7S8F 4aafj9prcYpmDot1dV2BkOgsqrXlUM5cLEHMcuXS6AiKsNmTYNFSWj3kb0eNLlNMJ5Iv GtQZervvKPzsbRrs3LYOBoef0ijpnz4KCNt7E= MIME-Version: 1.0 Received: by 10.224.73.106 with SMTP id p42mr2809185qaj.176.1282524143902; Sun, 22 Aug 2010 17:42:23 -0700 (PDT) Sender: till.varoquaux@gmail.com Received: by 10.229.234.3 with HTTP; Sun, 22 Aug 2010 17:42:23 -0700 (PDT) In-Reply-To: References: <1282434367.988.11.camel@mathias-ens> Date: Sun, 22 Aug 2010 20:42:23 -0400 X-Google-Sender-Auth: 8XGu-qO2bEZi_KSlJ8md6cKFoHg Message-ID: Subject: Re: [Caml-list] caml_copy_string From: Till Varoquaux To: Mathias Kende Cc: Jeffrey Barber , caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; ocaml:01 memcpy:01 malloc:01 byterun:01 mlvalues:01 val:01 val:01 ocaml:01 ffi:01 byte:01 alloc:01 pointer:01 beginner's:01 bug:01 mathias:98 Actually Mathias is spot on: you need your string to be allocated in the memory region owned by the ocaml GC and tagged properly (that is wrapped with the correct GC info). After that you can pass it around in C as a string but you should never resize it. That being said you only save a call to what existentially is memcpy (unless you need to malloc your c string): this should be real fast. Are you sure this is your bottleneck? Till On Sun, Aug 22, 2010 at 1:16 PM, Till Varoquaux wrote= : > In byterun/mlvalues.h > > #define Bp_val(v) ((char *) (v)) > .... > #define String_val(x) ((char *) Bp_val(x)) > > Doesn't look like String_val is doing much copying to me.... > > > Till > > On Sat, Aug 21, 2010 at 7:46 PM, Mathias Kende wro= te: >> Le samedi 21 ao=C3=BBt 2010 =C3=A0 18:30 -0500, Jeffrey Barber a =C3=A9c= rit : >>> Is there a way to get a string from C to OCaml without the >>> caml_copy_string >>> function, or is there a version that doesn't copy the string? >> >> There is no such function in the Caml FFI. You could write one yourself >> but then the string must have been specially allocated because you need >> to add a one word header to the string and maybe some byte at the end. >> So, if you have to exchange strings between OCaml and C, the easiest way >> is to always allocate them with the caml_alloc_string function. That way >> you can use the pointer returned by String_val in your C code and the >> string remains a valid Caml string (except caml does not use zero as the >> end of string and will stick to its allocated size). >> >> Mathias >> >> _______________________________________________ >> Caml-list mailing list. Subscription management: >> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list >> Archives: http://caml.inria.fr >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> >