[m-users.] Memory allocation from C back into Mercury

Julien Fischer jfischer at opturion.com
Tue Aug 30 14:14:19 AEST 2022


On Mon, 29 Aug 2022, Sean Charles (emacstheviking) wrote:

> I now have a working binding for libcurl that can do a GET and I am
> stuck trying to find a good example of how to allocate a mercury
> string to hand back into Mercury land.

You may want o have a look at the addition I proposed for the samples
directory on the reviews list recently:

     <https://lists.mercurylang.org/archives/reviews/2022-August/023191.html>

(I'm still working on addressing the review comments, so it hasn't been
committed yet.)

...

> I am also unsure of how to map a C "long"; the curl call
> `curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &longVal);` expects a
> pointer to a long, which I am currently then casting to an integer
> given the known range of HTTP status codes, but in general, if I
> wanted to pass that into a mercury function, what type do I use given
> I am on M1 arm64, the C specs only state 'minimum width' of 32, so
> int32 springs to mind but is this the correct assumption ?

In general, the safest thing to do would be to cast to int64_t and passe
the values back to Mercury as int64 values.  On all the platforms that
Mercury supports a long will either be 64 or 32 bits.  (As you say,
given their limited range, it doesn't really matter for HTTP status
codes.)

There's a sample in my JSON library, that uses curl, which may be
of interest:

    <https://github.com/juliensf/mercury-json/blob/master/samples/messages.m>

(It no longer works because the Github API it was talking to no longer
exists.)

Julien.


More information about the users mailing list