<div dir="ltr">Marc,<div><br></div><div>So what we get to then is that that Scheme object allocation could fail. In that case, would the mature way of dealing with it for the code return a special error, and then have a Scheme-side exception handler?</div><div><br></div><div>Also just to understand - is the only thin that would allocating objects in the C world to fail, would be that malloc() failed, or are there other more local things that could make the object allocation fail?</div><div><br><div><br><span style="font-size:12.8px">(define uint32_tag</span></div><div>  (lambda (a)<br style="font-size:12.8px"><span style="font-size:12.8px">  (let* ((r ((c-lambda</span><br style="font-size:12.8px"><span style="font-size:12.8px">   (TIFF* ttag_t)</span><br style="font-size:12.8px"><span style="font-size:12.8px">   scheme-object</span><br style="font-size:12.8px"><span style="font-size:12.8px">   "</span><br style="font-size:12.8px"><span style="font-size:12.8px">    ___U32 val;</span><br style="font-size:12.8px"><span style="font-size:12.8px">    if (TIFFGetField(___arg1, ___arg2, &val) == 1) {</span><br style="font-size:12.8px"><span style="font-size:12.8px">      if ((___err = ___U32_to_SCMOBJ (___PSTATE, val, &___result, ___RETURN_POS)) == ___FIX(___NO_ERR)) {</span><br style="font-size:12.8px"><span style="font-size:12.8px">        ___release_scmobj (___result);</span><div><span style="font-size:12.8px">      } else {</span><br style="font-size:12.8px"><span style="background-color:rgb(255,242,204)">        ___result = [             SOME ERROR CODE        ]; ?</span></div><div>      }<br><span style="font-size:12.8px">    } else {</span><br style="font-size:12.8px"><span style="font-size:12.8px">      ___result = ___FAL;</span><br style="font-size:12.8px"><span style="font-size:12.8px">    }</span><br style="font-size:12.8px"><span style="font-size:12.8px">   ") a)))</span><br></div></div></div><div><span style="font-size:12.8px">    (if (eq? a   some-error-code) (##raise-heap-overflow-exception)       a))</span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-01-06 23:02 GMT+08:00 Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On a 64 bit machine all uint32 values fit in a fixnum so there is no allocation possible.  On a 32 bit machine, converting a large uint32 will cause a heap allocation of a bignum.  However the code is written so that it detects heap overflows and raises a Scheme exception in that case (the Scheme exception can be handled in Scheme because the GC will have prereserved some room in the heap to do further processing in Scheme).<br>
<br>
A Scheme heap overflow exception could be raised by a malloc failure or if the Scheme heap size limit is reached (see -:h runtime option).<br>
<br>
Try<br>
<br>
% gsi -:h1000 -e "(let loop ((x 0)) (loop (list x)))"<br>
*** ERROR IN (string)@1.1 -- Heap overflow<br>
<br>
% gsi -:h1000 -e "(with-exception-catcher (lambda (e) (pp (list 'caught e))) (lambda () (let loop ((x 0)) (loop (list x)))))"<br>
(caught #<heap-overflow-exception #2>)<br>
<span class="HOEnZb"><font color="#888888"><br>
Marc<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
> On Jan 6, 2016, at 9:48 AM, Adam <<a href="mailto:adam.mlmb@gmail.com">adam.mlmb@gmail.com</a>> wrote:<br>
><br>
><br>
><br>
> 2016-01-06 22:37 GMT+08:00 Marc Feeley <<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>>:<br>
> Of course one way would be to return a double (which has 53 bits of precision) and use NaN to encode “missing tag”.  I’m not kidding!<br>
><br>
> If you don’t like this approach, and I can understand why you wouldn’t, then you have to do the conversion to ___SCMOBJ by directly calling the conversion macros.  Something like this:<br>
><br>
> (define uint32_tag<br>
>   (c-lambda<br>
>    (TIFF* ttag_t)<br>
>    scheme-object<br>
>    "<br>
>     ___U32 val;<br>
>     if (TIFFGetField(___arg1, ___arg2, &val) == 1) {<br>
>       if ((___err = ___U32_to_SCMOBJ (___PSTATE, val, &___result, ___RETURN_POS)) == ___FIX(___NO_ERR)) {<br>
>         ___release_scmobj (___result);<br>
>       }<br>
>     } else {<br>
>       ___result = ___FAL;<br>
>     }<br>
>    "))<br>
><br>
> Neat! What is the risk involved with this one - I mean, the Scheme object allocation on the Gambit heap could fail under certain circumstances. But would that only be on malloc() failure?<br>
<br>
</div></div></blockquote></div><br></div>