<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<p>Hi Thomas,</p>
<p>Like Micheal and Dave said, it's perhaps possible with some assembler "hack".</p>
<p>Here is a try which seem to work on my side. Probably the thing should be enhanced (protection, red-zone ?) :</p>
<div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">// ============================================================</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #944740">#include </span>&lt;stdlib.h&gt;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(148, 71, 64);">#include <span style="color: #c22007">&lt;Block.h&gt;</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><div style="margin: 0px; color: rgb(194, 32, 7);"><span style="color: #944740">#include </span>&lt;dispatch/dispatch.h&gt;</div><div style="margin: 0px; color: rgb(148, 71, 64);">#include <span style="color: #c22007">&lt;stdint.h&gt;</span></div></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">/*</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">** Defines</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">*/</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(148, 71, 64);">#pragma mark - Defines</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(148, 71, 64);">#define<span class="Apple-tab-span" style="white-space:pre">        </span>LOC_STACK_INCR<span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #3263a8">16</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(148, 71, 64);">#define<span class="Apple-tab-span" style="white-space:pre">        </span>LOC_ROUND_FRAME(x)<span class="Apple-tab-span" style="white-space:pre">        </span>((((unsigned)(x)) + LOC_STACK_INCR - <span style="color: #3263a8">1</span>) &amp; ~(LOC_STACK_INCR-<span style="color: #3263a8">1</span>))</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">/*</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">** Helpers</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">*/</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(148, 71, 64);">#pragma mark - Helpers</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span style="color: #3263a8">static</span> <span style="color: #3263a8">inline</span> <span style="color: #3263a8">void</span> call_block(<span style="color: #3263a8">void</span> *ctx)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">{</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span>dispatch_block_t block = ctx;</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span>block();</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">/*</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">** Functions</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">*/</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(148, 71, 64);">#pragma mark - Functions</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span style="color: #3263a8">void</span> execute_function_stack(<span style="color: #b75400">size_t</span> size, <span style="color: #3263a8">void</span> (*foo)(<span style="color: #3263a8">void</span> *ctx), <span style="color: #3263a8">void</span> *ctx)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">{</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #3263a8">void</span> *buffer = <span style="color: #3263a8">NULL</span>;</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #3263a8">void</span> *stack = <span style="color: #3263a8">NULL</span>;</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span>// Round stack size.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span>size = <span style="color: #777997">LOC_ROUND_FRAME</span>(size);</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #3263a8">if</span> (size &lt; <span style="color: #3263a8">128</span>)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(50, 99, 168);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span>return<span style="color: #000000">;</span></div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span>// Alloc stack.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #3263a8">if</span> (<span style="color: #3263a8">posix_memalign</span>(&amp;buffer, <span style="color: #777997">LOC_STACK_INCR</span>, size) != <span style="color: #3263a8">0</span>)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(50, 99, 168);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span>return<span style="color: #000000">;</span></div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span>stack = buffer + (size - </span><span style="color: #777997">LOC_STACK_INCR</span><span style="color: #000000">); </span>// Do we have to install a red-zone ?</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span>// Set the stack, call the function, and reset the stack.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="color: #3263a8">asm</span><span style="color: #000000">(</span>"movq<span class="Apple-tab-span" style="white-space:pre">        </span>%%rsp, %%r15<span class="Apple-tab-span" style="white-space:pre">        </span>\n\t"</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span>"movq<span class="Apple-tab-span" style="white-space:pre">        </span>%0, %%rsp<span class="Apple-tab-span" style="white-space:pre">                </span>\n\t"</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span>"movq<span class="Apple-tab-span" style="white-space:pre">        </span>%1, %%rdi<span class="Apple-tab-span" style="white-space:pre">                </span>\n\t"</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span>"callq<span class="Apple-tab-span" style="white-space:pre">        </span>*%2<span class="Apple-tab-span" style="white-space:pre">                                </span>\n\t"</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span></span>"movq<span class="Apple-tab-span" style="white-space:pre">        </span>%%r15, %%rsp<span class="Apple-tab-span" style="white-space:pre">        </span>\n\t"</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">                </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">                </span>:</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">                </span>: <span style="color: #c22007">"m"</span> (stack), <span style="color: #c22007">"m"</span> (ctx), <span style="color: #c22007">"m"</span> (foo)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(194, 32, 7);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">                </span>: </span>"%rsp"<span style="color: #000000">, </span>"%rdi"<span style="color: #000000">, </span>"%r15"<span style="color: #000000">, </span>"%rax"<span style="color: #000000">, </span>"cc"</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">                </span>);</div><p style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><span class="Apple-tab-span" style="white-space:pre">        </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">        </span></span>// Clean buffer.</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="color: #3263a8">free</span>(buffer);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span style="color: #3263a8">void</span> execute_block_stack(<span style="color: #b75400">size_t</span> size, dispatch_block_t block)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">{</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"><span class="Apple-tab-span" style="white-space:pre">        </span>execute_function_stack(size, call_block, (<span style="color: #3263a8">void</span> *)block);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;">}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"><br></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(20, 143, 19);">// ============================================================</div><div><br></div>
<p>&nbsp;</p>
<div>
<div>On 5 mars 2014, at 22:35, Michael Roitzsch &lt;<a href="mailto:mroi@os.inf.tu-dresden.de">mroi@os.inf.tu-dresden.de</a>&gt; wrote:</div>
<br class="Apple-interchange-newline">
<blockquote>Hi Thomas,<br><br>
<blockquote>We are using a library (out of our control) which is crashing our program apparently because it consumes all available stack space. We are not in control of the underlying threads since they are created by the libdispatch. Anything we can do here or are we back to managing pthreads?</blockquote>
<br>If stack space is a problem, could you just allocate a large slab of memory on the heap, point ESP there and then call into that problematic library? Then revert back to the original stack when control returns from the library to you. It’s just an idea, I have not tried that. You would have to know, how much memory you need, though, as the heap allocation obviously does not grow automatically.<br><br>Michael Roitzsch<br><br>_______________________________________________<br>libdispatch-dev mailing list<br><a href="mailto:libdispatch-dev@lists.macosforge.org">libdispatch-dev@lists.macosforge.org</a><br>https://lists.macosforge.org/mailman/listinfo/libdispatch-dev</blockquote>
</div>
<p>&nbsp;</p>

</body></html>