...stinks.<div><br></div><div>I&#39;m talking about sqAllocateMemory.  Here&#39;s the base definition from platforms/Cross/vm/sq.h:</div><div><br></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">#define sqAllocateMemory(minHeapSize, desiredHeapSize)  malloc(desiredHeapSize)</font></div>
<div><br></div><div>The problem here is that there&#39;s no obvious way for the client to know how much memory is returned.  The signature implies it is somewhere between minHeapSize &amp; desiredHeapSize inclusive (or null, if allocation failed).  But how do you tell?  Well, one could always ask to grow memory by 0 and see what you get back, except for the small chicken-and-egg problem that sqGrowMemory:By: and sqShrinkMemory:By: require the ammount of memory allocated as an argument:</div>
<div><br></div><div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">#define sqGrowMemoryBy(oldLimit, delta)         oldLimit</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">#define sqShrinkMemoryBy(oldLimit, delta)       oldLimit</font></div>
<div><br></div><div>So one has to go to extraordinary lengths that are completely non-obvious in the client code to actually pass-back the ammount allocated.  Here&#39;s a client in readImageFromFile:</div><div><br></div>
<div><span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#000000">        </font><font color="#008080">&quot;allocate a contiguous block of memory for the Squeak heap&quot;</font><font color="#000000"> <br>
        memory </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">cCode:</font><font color="#000000"> </font><font color="#800080"><font size="0.9" face="&#39;Accuny&#39;">&#39;sqAllocateMemory(minimumMemory, heapSize)&#39;</font></font><font color="#000000">. <br>
        memory </font><font color="#000080">=</font><font color="#000000"> </font><font color="#800000">nil</font><font color="#000000"> </font><font color="#000080">ifTrue:</font><font color="#000000"> [</font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">insufficientMemoryAvailableError</font><font color="#000000">]. <br>
<br>        </font><font color="#6B6767">memStart</font><font color="#000000"> </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">startOfMemory</font><font color="#000000">. <br>
        </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setMemoryLimit:</font><font color="#000000"> (</font><font color="#6B6767">memStart</font><font color="#000000"> </font><font color="#000080">+</font><font color="#000000"> </font><font color="#6B6767">heapSize</font><font color="#000000">) </font><font color="#000080">-</font><font color="#000000"> </font><font color="#800000">24</font><font color="#000000">. </font><font color="#008080">&quot;decrease memoryLimit a tad for safety&quot;</font><font color="#000000"> <br>
        </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setEndOfMemory:</font><font color="#000000"> </font><font color="#6B6767">memStart</font><font color="#000000"> </font><font color="#000080">+</font><font color="#000000"> </font><font color="#6B6767">dataSize</font><font color="#000000">. </font></span></div>
<div><font class="Apple-style-span" face="Times"><span class="Apple-style-span" style="font-size: medium;"><br></span></font></div>and here&#39;s how one gets around the absence of a &quot;how much was allocated&quot; parameter, from platforms/Mac OS/vm/sqPlatformSpecifc.h</div>
<div><br></div><div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">#undef sqAllocateMemory</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif"><br></font></div>
<div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">usqInt      sqAllocateMemoryMac(sqInt minHeapSize, sqInt *desiredHeapSize);</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif"><br>
</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">#define sqAllocateMemory(x,y) sqAllocateMemoryMac(x,&amp;y);</font></div><div><br></div><div>Ingenious (seriously; John this is a very clever working around of the restriction).  Totally non-obvious at the client site, but it still gets the job done without requiring the client to change.  So this costs too much to maintain (a.k.a. puts too much strain on my rodent brain).</div>
<div><br></div><div>We could go with an explicit out parameter, as in</div><div><br></div><div><div><span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#000000">        </font><font color="#008080">&quot;allocate a contiguous block of memory for the Squeak heap&quot;</font><font color="#000000"> <br>
        memory </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">cCode:</font><font color="#000000"> </font><font color="#800080"><font size="0.9" face="&#39;Accuny&#39;">&#39;sqAllocateMemory(minimumMemory, heapSize, &amp;allocatedSize)&#39;</font></font><font color="#000000">. <br>
        memory </font><font color="#000080">=</font><font color="#000000"> </font><font color="#800000">nil</font><font color="#000000"> </font><font color="#000080">ifTrue:</font><font color="#000000"> [</font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">insufficientMemoryAvailableError</font><font color="#000000">]. <br>
<br>        </font><font color="#6B6767">memStart</font><font color="#000000"> </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">startOfMemory</font><font color="#000000">. <br>
        </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setMemoryLimit:</font><font color="#000000"> (</font><font color="#6B6767">memStart</font><font color="#000000"> </font><font color="#000080">+</font><font color="#000000"> </font><font color="#6B6767">allocatedSize</font><font color="#000000">) </font><font color="#000080">-</font><font color="#000000"> </font><font color="#800000">24</font><font color="#000000">. </font><font color="#008080">&quot;decrease memoryLimit a tad for safety&quot;</font><font color="#000000"> <br>
        </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setEndOfMemory:</font><font color="#000000"> </font><font color="#6B6767">memStart</font><font color="#000000"> </font><font color="#000080">+</font><font color="#000000"> </font><font color="#6B6767">dataSize</font><font color="#000000">. </font></span></div>
<div><font class="Apple-style-span" face="Times"><span class="Apple-style-span" style="font-size: medium;"><br></span></font></div>but out parameters are not supported by Smalltalk and the VM is a Smalltalk program goddammit.</div>
<div><br></div><div>So I want to canvas opinions on the following simplified proposal, which is to drop the oldLimit parameter from sqGrowMemoryBy, discard sqShrinkMemoryBy (it is superfluous given sqGrowMemoryBy can take a signed argument) and use <span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">sqGrowMemoryBy:</font><font color="#000000"> </font><font color="#800000">0 <span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: arial; font-size: small; ">to determine how much memory has been allocated:</span></font></span></div>
<div><br></div><div><span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#000000">        </font><font color="#008080">&quot;allocate a contiguous block of memory for the Squeak heap&quot;</font><font color="#000000"> <br>
        memory </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">sqAllocateAtLeast:</font><font color="#000000"> </font><font color="#6B6767">minimumMemory</font><font color="#000000"> </font><font color="#000080">AtMostMemory:</font><font color="#000000"> </font><font color="#6B6767">heapSize</font><font color="#000000">. <br>
        memory </font><font color="#000080">=</font><font color="#000000"> </font><font color="#800000">nil</font><font color="#000000"> </font><font color="#000080">ifTrue:</font><font color="#000000"> [</font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">insufficientMemoryAvailableError</font><font color="#000000">]. <br>
        </font><font color="#6B6767">heapSize</font><font color="#000000"> </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">sqGrowMemoryBy:</font><font color="#000000"> </font><font color="#800000">0</font><font color="#000000">. <br>
        </font><font color="#6B6767">memStart</font><font color="#000000"> </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">startOfMemory</font><font color="#000000">. <br>
        </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setMemoryLimit:</font><font color="#000000"> (</font><font color="#6B6767">memStart</font><font color="#000000"> </font><font color="#000080">+</font><font color="#000000"> </font><font color="#6B6767">heapSize</font><font color="#000000">) </font><font color="#000080">-</font><font color="#000000"> </font><font color="#800000">24</font><font color="#000000">. </font><font color="#008080">&quot;decrease memoryLimit a tad for safety&quot;</font><font color="#000000"> <br>
        </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setEndOfMemory:</font><font color="#000000"> </font><font color="#6B6767">memStart</font><font color="#000000"> </font><font color="#000080">+</font><font color="#000000"> </font><font color="#6B6767">dataSize</font><font color="#000000">.</font></span></div>
<div><font class="Apple-style-span" face="Times"><span class="Apple-style-span" style="font-size: medium;"><br></span></font></div>with the obvious default implementations in e.g. platforms/Cross/vm/sqMemory.c</div><div><br>
</div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">static unsigned long memoryLimit = 0;</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif"><br></font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">void *</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">sqAllocateAtLeastAtMostMemory(unsigned long committed, unsigned long reserved)</font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">{</font></div><div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">   void *mem;</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">    if ((mem = malloc(reserved)) ~= 0)</font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">        memoryLimit = reserved;</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">    else if ((mem = malloc(committed)) ~= 0)</font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">        memoryLimit = committed;</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">    return mem;</font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">}</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif"><br></font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">unsigned long</font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">sqGrowMemoryBy(sqInt delta)</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">{</font></div><div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">    return memoryLimit;</font></div>
<div><font class="Apple-style-span" face="&#39;comic sans ms&#39;, sans-serif">}</font></div><div><br></div><div>and everywhere in the VM which expects the old sqGrowMemoryBy to answer a pointer to the limit must instead add the result to the base of allocated memory; i.e.</div>
<div><br></div><div><span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#000000">        </font><font color="#6B6767">limit</font><font color="#000000"> </font><b>:=</b><font color="#000000"> </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">sqGrowMemory:</font><font color="#000000"> memoryLimit </font><font color="#000080">By:</font><font color="#000000"> </font><font color="#000080">delta</font><font color="#000000">. <br>
        </font><font color="#6B6767">limit</font><font color="#000000"> </font><font color="#000080">=</font><font color="#000000"> memoryLimit </font><font color="#000080">ifFalse:</font><font color="#000000"> <br>                        [</font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setMemoryLimit:</font><font color="#000000"> </font><font color="#6B6767">limit</font><font color="#000000"> </font><font color="#000080">-</font><font color="#000000"> </font><font color="#800000">24</font><font color="#000000">. </font><font color="#008080">&quot;remove a tad for safety&quot;</font><font color="#000000"> <br>
                         </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">initializeMemoryFirstFree:</font><font color="#000000"> freeBlock] <br><br></font></span></div><div><span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#000000"><span class="Apple-style-span" style="font-family: arial; font-size: small; ">is rewritten as</span></font></span></div>
<div><span class="Apple-style-span" style="font-family: Times; font-size: medium; "><font color="#000000"><font class="Apple-style-span" face="arial"><span class="Apple-style-span" style="font-size: small;"><br></span></font>        </font><font color="#6B6767">limit</font><font color="#000000"> </font><b>:=</b><font color="#000000"> memoryLimit </font><font color="#000080">+</font><font color="#000000"> (</font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">sqGrowMemoryBy:</font><font color="#000000"> </font><font color="#000080">delta</font><font color="#000000">). <br>
        </font><font color="#6B6767">limit</font><font color="#000000"> </font><font color="#000080">=</font><font color="#000000"> memoryLimit </font><font color="#000080">ifFalse:</font><font color="#000000"> <br>                        [</font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">setMemoryLimit:</font><font color="#000000"> </font><font color="#6B6767">limit</font><font color="#000000"> </font><font color="#000080">-</font><font color="#000000"> </font><font color="#800000">24</font><font color="#000000">. </font><font color="#008080">&quot;remove a tad for safety&quot;</font><font color="#000000"> <br>
                         </font><font color="#800000">self</font><font color="#000000"> </font><font color="#000080">initializeMemoryFirstFree:</font><font color="#000000"> freeBlock]</font></span></div><div>Opinions?</div><div>
<br></div><div>best</div><div>Eliot</div></div></div>