<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">OK, I have solved my issue. I found some old posts from 2019 that I made regarding SDL2 structures and the reply chain from there led me to use 'MR_GC_malloc' and it all looks to be just fine now.<div class=""><br class=""></div><div class="">Just in case anybody else finds it useful, here is the central piece of allocation code</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""> size_t c_length = (sizeof(char) * chunk.size) + 1;<br class=""> char* the_string = (char*)MR_GC_malloc(c_length);<br class=""><br class=""> if (the_string) {<br class=""> memcpy(the_string, &chunk.memory[0], chunk.size);<br class=""> Response = makeResponse(HttpStatus, the_string);<br class=""> }<br class=""> else {<br class=""> Response = makeError(""Failed to allocate string"");<br class=""> }</font></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">...and the modified function that returns my curl_response record:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">:- func make_response(int::in, string::in)<br class=""> = (io.res(curl_response)::out) is det.<br class=""><br class="">make_response(HttpStatus, Content) = Out :-<br class=""> trace[io(!Dbg), runtime(env("MCURL_DEBUG"))]<br class=""> (<br class=""> io.format("make_response:\n%s\n", [s(Content)], !Dbg)<br class=""> ),<br class=""> Out = ok(curl_response(HttpStatus, Content, [])).<br class=""><br class="">:- pragma foreign_export("C", make_response(in, in)<br class=""> = (out), "makeResponse").<br class=""></font><div class=""><br class=""></div><div class=""><br class=""></div><div class="">When I run this now I get the following output,</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""><b class="">➜ mercury-libcurl ./mcurl <a href="https://google.com" class="">https://google.com</a></b></font></div><div class=""><font face="Menlo" class="">GET <a href="https://google.com" class="">https://google.com</a></font></div><div class=""><font face="Menlo" class="">* 220 bytes retrieved</font></div><div class=""><font face="Menlo" class="">* opened and cleaned up OK</font></div><div class=""><font face="Menlo" class="">HTTP: 301, length: 220</font></div><div class=""><font face="Menlo" class="">Content:</font></div><div class=""><font face="Menlo" class=""><HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"></font></div><div class=""><font face="Menlo" class=""><TITLE>301 Moved</TITLE></HEAD><BODY></font></div><div class=""><font face="Menlo" class=""><H1>301 Moved</H1></font></div><div class=""><font face="Menlo" class="">The document has moved</font></div><div class=""><font face="Menlo" class=""><A HREF="<a href="https://www.google.com" class="">https://www.google.com</a>/">here</A>.</font></div><div class=""><font face="Menlo" class=""></BODY></HTML></font></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Which is great news as far as I am concerned, I can now review and refactor what I have and move on to doing POST, PUT etc.</div><div class=""><br class=""></div><div class="">Thanks again,</div><div class="">Sean.</div><div class=""><br class=""></div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 29 Aug 2022, at 18:05, Sean Charles (emacstheviking) <<a href="mailto:objitsu@gmail.com" class="">objitsu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">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.</div><div class="">I have the same code from here: <a href="https://curl.se/libcurl/c/getinmemory.html" class="">https://curl.se/libcurl/c/getinmemory.html</a></div><div class=""><br class=""></div><div class="">After a call succeeds, here is how I am currently handling to curl response:</div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""> if (CURLE_OK == res) {<br class=""> printf(""* %lu bytes retrieved\\n"", (unsigned long)chunk.size);<br class=""> long HttpStatus;<br class=""> curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &HttpStatus);<br class=""><b class=""> Response = makeResponse((int)HttpStatus);<br class=""></b> }<br class=""> else {<br class=""> Response = makeError(1, curl_easy_strerror(res));<br class=""> }</font></div><div class=""><br class=""></div><div class="">The line in bold, here is where I need to be able to create a string with the data from chunk.memory, for the number of bytes received, chunk.size, this is where I am unsure how to continue. The makeResponse() code is:</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">:- func make_response(int::in)<br class=""> = (io.res(curl_response)::out) is det.<br class=""><br class="">make_response(HttpStatus)<br class=""> = ok(curl_response(HttpStatus, "Data!", [])).<br class=""><br class="">:- pragma foreign_export("C", make_response(in)<br class=""> = (out), "makeResponse").</font></div><div class=""><br class=""></div><div class="">with:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">:- type curl_header ---> curl_header(string, string).<br class="">:- type curl_response<br class=""> ---> curl_response(<br class=""> status :: int,<br class=""> content :: string,<br class=""> headers :: list(curl_header)<br class=""> ).</font></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">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 ?</div><div class=""><br class=""></div><div class="">The final intention is to be able to have enough HTTP support available so that I can use the average API out there, GET/POST/PUT/FETCH/OPTIONS, with a view to implementing support layer for using my SOLID POD from a Mercury command line application.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">Sean.</div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></div></div></body></html>