<div dir="ltr"> I don't know which systems do and don't guarantee what is already cleared; are you careful not to repeat someone else's work?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 12, 2014 at 9:21 AM, Alex Petrov <span dir="ltr"><<a href="mailto:oleksandr.petrov@gmail.com" target="_blank">oleksandr.petrov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">Hi everyone,</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><p style="margin:0px 0px 12px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">Currently the memory allocated with (Foreign.Marshal.Alloc) malloc may potentially be dirty. In C this problem is usually solved by using memset.</p><p style="margin:0px 0px 12px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">This would be extremely useful for FFI / C interop, when a data structure is allocated within Haskell code. With memset, you can do something like</p><div style="margin:12px;padding:0px;border:0px;white-space:pre-wrap;font-family:Menlo,Consolas,Monaco,monospace;font-size:10px"><pre style="margin-top:0px;margin-bottom:0px;padding:8px;border:1px solid rgb(241,196,15);overflow:auto;font-family:Monaco,monospace;background-color:rgb(253,245,212);background-repeat:initial initial"><span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(0,10,101)">customMem</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(170,34,17)"><-</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(0,10,101)">malloc</span>
<span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(0,10,101)">_</span>         <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(170,34,17)"><-</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(0,64,18)">memset</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(170,34,17)">(</span><span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(0,10,101)">castPtr</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(0,10,101)">customMem</span><span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(170,34,17)">)</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(96,18,0)">0</span> <span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(116,119,125)">#{size custom_t}</span></pre></div><p style="margin:0px 0px 12px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">This will fill a block of allocated memory with zeroes.</p><p style="margin:0px 0px 12px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">For example, I've been working on FFI bindings for collectd, and when I was allocating memory, previously used within the process, it was dirty, so my CString contained a value of</p><div style="margin:12px;padding:0px;border:0px;white-space:pre-wrap;font-family:Menlo,Consolas,Monaco,monospace;font-size:10px"><pre style="margin-top:0px;margin-bottom:0px;padding:8px;border:1px solid rgb(241,196,15);overflow:auto;font-family:Monaco,monospace;background-color:rgb(253,245,212);background-repeat:initial initial"><span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(118,101,16)">"custom name7e0700490, test_plugin_LTX_module_register): symbol nd"</span></pre></div><p style="margin:0px 0px 12px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">Instead of just</p><div style="margin:12px;padding:0px;border:0px;white-space:pre-wrap;font-family:Menlo,Consolas,Monaco,monospace;font-size:10px"><pre style="margin-top:0px;margin-bottom:0px;padding:8px;border:1px solid rgb(241,196,15);overflow:auto;font-family:Monaco,monospace;background-color:rgb(253,245,212);background-repeat:initial initial"><span style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:transparent;color:rgb(118,101,16)">"custom name"</span></pre></div><p style="margin:0px 0px 12px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">After using memset and filling everything with zeroes, problem never appeared again.</p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">This can be implemented in user applications, too, although it would be really nice to have it by default.</p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">This is a common practice in C world, and this function is not only useful for cases when the memory was just allocated from Haskell process, but also when one receives a dirty memory buffer from C code. </p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px"><br></p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">The patch for proposal was implemented and is available on Phabricator: <a href="https://phabricator.haskell.org/D465" style="font-family:Helvetica,Arial" target="_blank">https://phabricator.haskell.org/D465</a></p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px"><br></p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px"><br></p><p style="margin:0px;padding:0px;border:0px;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:18.8500003814697px">Please share your thoughts.</p></div><br><div><span style="font-family:helvetica,arial;font-size:13px"></span><p style="margin:0px"><br></p><p style="margin:0px">Alex</p><div><a href="https://twitter.com/ifesdjeen" target="_blank">https://twitter.com/ifesdjeen</a><br><div><p style="line-height:normal;font-family:Helvetica,Arial;margin:0px"><a href="http://clojurewerkz.org/" target="_blank">http://clojurewerkz.org/</a></p></div></div></div></div><br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br></div>