From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 72C187FB82 for ; Sun, 28 Feb 2016 20:10:29 +0100 (CET) IronPort-PHdr: 9a23:EhEMZR9CKn5nVv9uRHKM819IXTAuvvDOBiVQ1KB91OwcTK2v8tzYMVDF4r011RmSDdqdtawP0rCO+4nbGkU+or+5+EgYd5JNUxJXwe43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6anHS+4HYoFwnlMkItf6KuStGU0Zr8jrrqs7ToICx2xxOFKYtoKxu3qQiD/uI3uqBFbpgL9x3Sv3FTcP5Xz247bXianhL7+9vitMU7q3cYk7sb+sVBSaT3ebgjBfwdVWx+cjN92Mq+lxTZQA6erkoVVmERilIcEgHB6Bbidpj0uyr+8OF63X/JE9fxSOURUDCp6KNuADrhlC4DNDdxpGTSg9B5iKFSiB2krh17hYXTZdfGZ7JFYqrBcIZCFiJ6VcFLWnkEW9vkYg== Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=mmatalka@gmail.com; spf=Pass smtp.mailfrom=mmatalka@gmail.com; spf=None smtp.helo=postmaster@mail-wm0-f43.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of mmatalka@gmail.com) identity=pra; client-ip=74.125.82.43; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="mmatalka@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of mmatalka@gmail.com designates 74.125.82.43 as permitted sender) identity=mailfrom; client-ip=74.125.82.43; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="mmatalka@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wm0-f43.google.com) identity=helo; client-ip=74.125.82.43; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="postmaster@mail-wm0-f43.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BXAAAqRdNWlStSfUpehAxeD7psgWYhhXICgSE6EgEBAQEBAQEBEAEBAQEHCwsJIS+CLYIUAQEBAwESLgEbHQEDAQsGBQsNCSUPAQQNAhEBBQEiEyKHZwEDCggECqIDgTE+MY0dgleETwoZJw1Rg2wBAQEBAQEBAwEBAQEBAQETAQEECgSEAoICgz19gjqGNQWXDIVZhhWBdIkWhV6HBoYGL4EPJwWCLAoUCIFIaog3AQEB X-IPAS-Result: A0BXAAAqRdNWlStSfUpehAxeD7psgWYhhXICgSE6EgEBAQEBAQEBEAEBAQEHCwsJIS+CLYIUAQEBAwESLgEbHQEDAQsGBQsNCSUPAQQNAhEBBQEiEyKHZwEDCggECqIDgTE+MY0dgleETwoZJw1Rg2wBAQEBAQEBAwEBAQEBAQETAQEECgSEAoICgz19gjqGNQWXDIVZhhWBdIkWhV6HBoYGL4EPJwWCLAoUCIFIaog3AQEB X-IronPort-AV: E=Sophos;i="5.22,516,1449529200"; d="scan'208";a="166249910" Received: from mail-wm0-f43.google.com ([74.125.82.43]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 28 Feb 2016 20:10:28 +0100 Received: by mail-wm0-f43.google.com with SMTP id l68so11490318wml.0 for ; Sun, 28 Feb 2016 11:10:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=Hs/4yQMNjpOTaZHtX726NSxLRdqQlJOtTVir3u6DWPQ=; b=p4FYOWm6i5XVTP1ibvoI3Cp657bk3DopvxC65f6LXFWXaHxkqXQ1QtCI2tZg1aryxL SyOGFCfT7mwUmzhvKnsPs59RWGf8YLlxuaf361S4I7ua0DYogveB00jfSae4qUsf+H+L Z+c3vx9xe2Fyif+/MCT+bjxrfgO+OhAX4Nnw9n1ggwe+rhNS7d0Ig3QZ7yjQnTDW+3A7 bvEavbywGM/Y/0tPvGkZ6BL+W+pVwKdcTFfO5j7ClpgVAT/t9ctHuscteXBNWPGvERnu dS+HREwf2SrFUiYDBjcaTCdV/oanTWwaW84EiP7XzusggAQ5RvG2cGWfQ6CnY0ujIycb mShQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=Hs/4yQMNjpOTaZHtX726NSxLRdqQlJOtTVir3u6DWPQ=; b=RWvtSrIQNu1saQQSlmCvj5QCtQpx/Sm1HQZU84/zDzO1tkF1+GhLtdSopSDvsPZzT1 RFS6n4PDDLubCszCAS6zrOc/3JoB0FVti6SEKmvUPhOs2aOJBWu9V7q0vUEOTHtIjESn Ky7y5yP01nQKXG9FubmvFYD1fciTgpRkBkCzXENzvuzLY13qSjY8bcAW7WhAWyNSm/jF 84rsO8ze1M18Kn2NH7+8G0piWD9o3A+u/wx/65XfBg+Sbb8Tfg/fJUWEMd9LMX0W4L2f dd0V9b8zYfJPZI29fF/p3bGMNKj2MEcjbdQDbvzPj9OR8U0eGFX6aLFke5ohZgHXuZgc lQ2w== X-Gm-Message-State: AD7BkJI93JUimtFpdDbnJLkYCumKFIe9+coSVq3/TkYs7RxvQQBdWCL+VEJGpkDpNgbyrQ== X-Received: by 10.194.86.130 with SMTP id p2mr10703460wjz.93.1456686628302; Sun, 28 Feb 2016 11:10:28 -0800 (PST) Received: from localhost ([37.153.108.22]) by smtp.gmail.com with ESMTPSA id k4sm12738173wmc.12.2016.02.28.11.10.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Feb 2016 11:10:27 -0800 (PST) From: Malcolm Matalka To: Jeremy Yallop Cc: caml-list@inria.fr, ctypes@lists.ocaml.org References: <86ziulheva.fsf@gmail.com> Date: Sun, 28 Feb 2016 19:10:21 +0000 In-Reply-To: (Jeremy Yallop's message of "Sun, 28 Feb 2016 00:56:37 +0000") Message-ID: <86si0chcrm.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Caml-list] ctypes - Advice for binding big structs? Jeremy Yallop writes: > Dear Malcolm, > > On 28/02/2016, Malcolm Matalka wrote: >> I have a large/complex struct I am trying to create bindings for >> operations on it in Ocaml. I have an API that tells me how many bytes >> the struct is so I can allocate it just fine and pass it around to C >> functions I've bound with ctypes. But some data in it is accessed via >> members. I started implementing a structure in ctypes for it, but it's >> getting large and awkward. Are there any best practices around doing >> this? > > The best approach is to use the Cstubs_structs module, which allows > you to declare just the parts of the structs that you need to access > in your program, and which generates code that uses the C struct > declarations to work out sizes, alignments, field offsets, etc. The > basic API is the familiar set of functions "structure", "field" and > "seal" from the Ctypes module, but the build process is a little more > involved. However, in return for the more complex build, all the > issues that you're concerned about are addressed. > > The Cstubs_structs API is not yet very well documented, but there's a > brief guide with examples in the pull request that introduced it: > > https://github.com/ocamllabs/ocaml-ctypes/pull/62 > Great, with some small modifications this worked like a charm. >> Some concerns I have: >> >> - It seems fragile - a different version of the library might have >> different members in the struct so keeping my ocaml code in-synch >> seems error prone. > > The Cstubs_structs module addresses this by using generated C code to > determine the offsets of fields each time you build your library. > >> - It's annoying because the struct has a lot of members I don't care >> about in my case. I only want access to a few members that have >> important details. > > Since Cstubs_structs retrieves layout rather than computing it you > only need to declare the members that you care about. > >> - The struct is large with lots of types that I don't necessarily want >> to create so creating the struct becomes somewhat awkward. If I know >> the size of the types I might be able to pretend it's an array of N >> chars or something instead of trying to implement the type just to >> fill out this struct, but I don't know if that is valid. > > Again, since Cstubs_structs retrieves struct layout and alignment > information from C, you can use Ctypes.make to create struct values, > even if you haven't declared all the fields. > > Kind regards, > > Jeremy.