<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Hi everyone,</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" 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 class="remarkup-code-block" data-code-lang="php" data-sigil="remarkup-code-block" style="margin: 12px; padding: 0px; border: 0px; white-space: pre; font-family: Menlo, Consolas, Monaco, monospace; font-size: 10px;"><pre class="remarkup-code" 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-position: initial initial; background-repeat: initial initial;"><span class="no" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(0, 10, 101);">customMem</span> <span class="o" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(170, 34, 17);"><-</span> <span class="no" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(0, 10, 101);">malloc</span>
<span class="no" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(0, 10, 101);">_</span>         <span class="o" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(170, 34, 17);"><-</span> <span class="nf" data-symbol-name="memset" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(0, 64, 18);">memset</span> <span class="o" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(170, 34, 17);">(</span><span class="no" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(0, 10, 101);">castPtr</span> <span class="no" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(0, 10, 101);">customMem</span><span class="o" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(170, 34, 17);">)</span> <span class="mi" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent; color: rgb(96, 18, 0);">0</span> <span class="c" 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 class="remarkup-code-block" data-code-lang="php" data-sigil="remarkup-code-block" style="margin: 12px; padding: 0px; border: 0px; white-space: pre; font-family: Menlo, Consolas, Monaco, monospace; font-size: 10px;"><pre class="remarkup-code" 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-position: initial initial; background-repeat: initial initial;"><span class="s2" 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 class="remarkup-code-block" data-code-lang="php" data-sigil="remarkup-code-block" style="margin: 12px; padding: 0px; border: 0px; white-space: pre; font-family: Menlo, Consolas, Monaco, monospace; font-size: 10px;"><pre class="remarkup-code" 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-position: initial initial; background-repeat: initial initial;"><span class="s2" 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;">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 id="bloop_sign_1415801858758530048" class="bloop_sign"><span style="font-family:helvetica,arial;font-size:13px"></span><p id="bloop_customfont" style="margin: 0px; "><br></p><p id="bloop_customfont" style="margin: 0px; ">Alex</p><div><a href="https://twitter.com/ifesdjeen">https://twitter.com/ifesdjeen</a><br><div><p id="bloop_customfont" style="line-height: normal; font-family: Helvetica, Arial; margin: 0px; "><a href="http://clojurewerkz.org/">http://clojurewerkz.org/</a></p></div></div></div></body></html>