Re: FFI with C structs

Domagoj Stolfa <[email protected]> Fri, 5 Oct 2018 13:50:55 +0100
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============2135695731670303178==
Content-Type: multipart/alternative;
 boundary="------------CE73A91B9E65EA136A01DFEA"
Content-Language: en-US

This is a multi-part message in MIME format.
--------------CE73A91B9E65EA136A01DFEA
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit

Hi:


I think that MLTon.Pointer is exactly what I was looking for -- but for 
some reason have missed :-). Thanks a lot!


-- 

Domagoj


On 05/10/2018 03:34, Matthew Fluet wrote:
> You could accomplish this with the operations in MLton.Pointer (see 
> http://mlton.org/MLtonPointer). The MLton.Pointer.t type is an FFI 
> type (http://mlton.org/ForeignFunctionInterfaceTypes), so the typical 
> scenario would be to obtain the pointer from some imported function 
> and then manipulate it with the MLton.Pointer operations.  Something like:
>
>   val getFoo = _import "getFoo": unit -> MLton.Pointer.t;
>   val foo = getFoo ()
>   val a = MLton.Pointer.getInt32 (foo, 0)
>   val b = MLton.Pointer.getWord8 (MLton.Pointer.add (foo, 4), 0)
>   val c = MLton.Pointer.getInt32 (MLton.Pointer.add (foo, 8), 0)
>
> MLton does ship with a version of Matthias Blume's ml-nlffigen tool 
> and library (http://mlton.org/MLNLFFIGen and 
> http://mlton.org/MLNLFFI), which can be used to generate interface 
> code from a header file.
>
> But, if you are asking how you could craft an SML type such that it 
> could be passed to a C function expecting a "foo", then that is not 
> possible with MLton.  Only a limited number of types are allowed in 
> MLton's FFI, precisely because we do not guarantee representations of 
> other types.
>
> --Matthew
>
>
> On Wed, Oct 3, 2018 at 4:03 PM Domagoj Stolfa <[email protected] 
> <mailto:[email protected]>> wrote:
>
>     Hi all:
>
>
>     I was wondering about what an idiomatic way to fill out a C
>     structure in
>     SML using mlton would be. Suppose I have a C struct such as:
>
>
>     struct foo {
>
>          int a;
>
>          char b;
>
>          int c;
>
>     };
>
>
>     Is there any way I could specify a datatype or a module with the
>     correct
>     ABI for this C struct (alignment, offsets and size). I'm happy to
>     specify this manually or use an automated way to do it -- but would
>     prefer to avoid doing it with arrays and manually dumping the data to
>     conform with the ABI. Another option I'd prefer to avoid, but
>     would be
>     more acceptable is building a little C interface that would take some
>     SML types and translate them, but was wondering if there was a more
>     idiomatic way to accomplish this.
>
>
>     Thanks!
>
>
>     -- 
>
>     Domagoj
>
>
>     -- 
>     You received this message because you are subscribed to the Google
>     Groups "MLton-user" group.
>     To unsubscribe from this group and stop receiving emails from it,
>     send an email to [email protected]
>     <mailto:mlton-user%[email protected]>.
>
>
>
>     _______________________________________________
>     MLton-user mailing list
>     [email protected]
>     <mailto:[email protected]>; [email protected]
>     <mailto:[email protected]>
>     https://lists.sourceforge.net/lists/listinfo/mlton-user
>
> -- 
> You received this message because you are subscribed to the Google 
> Groups "MLton-user" group.
> To unsubscribe from this group and stop receiving emails from it, send 
> an email to [email protected] 
> <mailto:[email protected]>.
>
>
> _______________________________________________
> MLton-user mailing list
> [email protected]; [email protected]
> https://lists.sourceforge.net/lists/listinfo/mlton-user

--------------CE73A91B9E65EA136A01DFEA
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p><tt>Hi:</tt></p>
    <p><tt><br>
      </tt></p>
    <p><tt>I think that MLTon.Pointer is exactly what I was looking for
        -- but for some reason have missed :-). Thanks a lot!</tt></p>
    <p><tt><br>
      </tt></p>
    <p><tt>-- <br>
      </tt></p>
    <p><tt>Domagoj<br>
      </tt></p>
    <p><tt></tt><br>
    </p>
    <div class="moz-cite-prefix">On 05/10/2018 03:34, Matthew Fluet
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAMrhFL4sQRbODqTZ_su+-zhrx7fWJPHccs5w3E97RxHqr-ov5A@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div dir="ltr">
          <div dir="ltr">
            <div dir="ltr">
              <div dir="ltr">
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">You could
                  accomplish this with the operations in MLton.Pointer
                  (see <a href="http://mlton.org/MLtonPointer"
                    moz-do-not-send="true">http://mlton.org/MLtonPointer</a>). 
                  The MLton.Pointer.t type is an FFI type (<a
                    href="http://mlton.org/ForeignFunctionInterfaceTypes"
                    moz-do-not-send="true">http://mlton.org/ForeignFunctionInterfaceTypes</a>),
                  so the typical scenario would be to obtain the pointer
                  from some imported function and then manipulate it
                  with the MLton.Pointer operations.  Something like:</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large"><br>
                </div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">  val getFoo =
                  _import "getFoo": unit -&gt; MLton.Pointer.t;</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">  val foo =
                  getFoo ()</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">  val a =
                  MLton.Pointer.getInt32 (foo, 0)</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">  val b =
                  MLton.Pointer.getWord8 (MLton.Pointer.add (foo, 4), 0)</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">  val c =
                  MLton.Pointer.getInt32 (MLton.Pointer.add (foo, 8), 0)</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large"><br>
                </div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">MLton does ship
                  with a version of Matthias Blume's ml-nlffigen tool
                  and library (<a href="http://mlton.org/MLNLFFIGen"
                    moz-do-not-send="true">http://mlton.org/MLNLFFIGen</a>
                  and <a href="http://mlton.org/MLNLFFI"
                    moz-do-not-send="true">http://mlton.org/MLNLFFI</a>),
                  which can be used to generate interface code from a
                  header file.</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large"><br>
                </div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">But, if you are
                  asking how you could craft an SML type such that it
                  could be passed to a C function expecting a "foo",
                  then that is not possible with MLton.  Only a limited
                  number of types are allowed in MLton's FFI, precisely
                  because we do not guarantee representations of other
                  types.</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large"><br>
                </div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large">--Matthew</div>
                <div class="gmail_default"
                  style="font-family:&quot;courier
                  new&quot;,monospace;font-size:large"><br>
                </div>
              </div>
            </div>
          </div>
        </div>
        <br>
        <div class="gmail_quote">
          <div dir="ltr">On Wed, Oct 3, 2018 at 4:03 PM Domagoj Stolfa
            &lt;<a href="mailto:[email protected]" moz-do-not-send="true">[email protected]</a>&gt;
            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all:<br>
            <br>
            <br>
            I was wondering about what an idiomatic way to fill out a C
            structure in <br>
            SML using mlton would be. Suppose I have a C struct such as:<br>
            <br>
            <br>
            struct foo {<br>
            <br>
                 int a;<br>
            <br>
                 char b;<br>
            <br>
                 int c;<br>
            <br>
            };<br>
            <br>
            <br>
            Is there any way I could specify a datatype or a module with
            the correct <br>
            ABI for this C struct (alignment, offsets and size). I'm
            happy to <br>
            specify this manually or use an automated way to do it --
            but would <br>
            prefer to avoid doing it with arrays and manually dumping
            the data to <br>
            conform with the ABI. Another option I'd prefer to avoid,
            but would be <br>
            more acceptable is building a little C interface that would
            take some <br>
            SML types and translate them, but was wondering if there was
            a more <br>
            idiomatic way to accomplish this.<br>
            <br>
            <br>
            Thanks!<br>
            <br>
            <br>
            -- <br>
            <br>
            Domagoj<br>
            <br>
            <br>
            -- <br>
            You received this message because you are subscribed to the
            Google Groups "MLton-user" group.<br>
            To unsubscribe from this group and stop receiving emails
            from it, send an email to <a
              href="mailto:mlton-user%[email protected]"
              target="_blank" moz-do-not-send="true">[email protected]</a>.<br>
            <br>
            <br>
            <br>
            _______________________________________________<br>
            MLton-user mailing list<br>
            <a href="mailto:[email protected]"
              target="_blank" moz-do-not-send="true">[email protected]</a>;
            <a href="mailto:[email protected]" target="_blank"
              moz-do-not-send="true">[email protected]</a><br>
            <a
              href="https://lists.sourceforge.net/lists/listinfo/mlton-user"
              rel="noreferrer" target="_blank" moz-do-not-send="true">https://lists.sourceforge.net/lists/listinfo/mlton-user</a><br>
          </blockquote>
        </div>
      </div>
      -- <br>
      You received this message because you are subscribed to the Google
      Groups "MLton-user" group.<br>
      To unsubscribe from this group and stop receiving emails from it,
      send an email to <a
        href="mailto:[email protected]"
        moz-do-not-send="true">[email protected]</a>.<br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
MLton-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>; <a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="https://lists.sourceforge.net/lists/listinfo/mlton-user">https://lists.sourceforge.net/lists/listinfo/mlton-user</a>
</pre>
    </blockquote>
  </body>
</html>

--------------CE73A91B9E65EA136A01DFEA--


--===============2135695731670303178==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============2135695731670303178==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user

--===============2135695731670303178==--