<div dir="ltr">Hi,<div><br></div><div>I have some issues using the new raw memory API. For instance, let's suppose I want to call the `<span style="color:rgb(61,29,129);font-family:menlo;font-size:11px">SecRandomCopyBytes</span>` API to generate a cryptographically secure random 32-bit number. The difficulty is its 3rd argument, which is declared as <span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"><</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UInt8</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">></span>. Here is a function that does that:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> entropicRandom() -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UInt32</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> randomWordPT = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures"><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UInt32</span><span style="font-variant-ligatures:no-common-ligatures">>.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">allocate</span><span style="font-variant-ligatures:no-common-ligatures">(capacity: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> = randomWordPT.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">withMemoryRebound</span><span style="font-variant-ligatures:no-common-ligatures">(to: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">self</span><span style="font-variant-ligatures:no-common-ligatures">, capacity: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">4</span><span style="font-variant-ligatures:no-common-ligatures">) { (p: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UnsafeMutablePointer</span><span style="font-variant-ligatures:no-common-ligatures"><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">UInt8</span><span style="font-variant-ligatures:no-common-ligatures">>) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int32</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">in</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> result = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">SecRandomCopyBytes</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">kSecRandomDefault</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, </span><span style="font-variant-ligatures:no-common-ligatures">MemoryLayout</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"><</span><span style="font-variant-ligatures:no-common-ligatures">UInt32</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">>.</span><span style="font-variant-ligatures:no-common-ligatures">size</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">, p)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> result</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> randomInt32 = randomWordPT[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">0</span><span style="font-variant-ligatures:no-common-ligatures">]</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> randomWordPT.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">deallocate</span><span style="font-variant-ligatures:no-common-ligatures">(capacity: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> randomInt32</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p></div><div><br></div><div>apparently, the calls to allocate and then deallocate suggest that there is some heap allocation happening behind the scene here, possibly malloc/free. Is that correct?</div><div><br></div><div>If so, this is quite wasteful. Is there a way to use a local variable on the stack to achieve the same result?</div><div><br></div><div>Thanks,</div><div><br></div><div>Jean-Denis</div><div><br></div><div><br></div><div><br></div></div>