[gambit-list] Converting uint32 to scheme object

Marc Feeley feeley at iro.umontreal.ca
Fri Jan 8 08:35:56 EST 2016


I see you are passing the processor state as a parameter.  The macro ___PSTATE can get the processor state at any point in the code.  This avoids having to thread the processor state from one function to the next.  Note that inside an “inline” c-lambda, you can access the processor state using ___ps, which is faster than using ___PSTATE (because that macro may access a global or thread local storage).

Marc

> On Jan 7, 2016, at 11:24 AM, Alex Silva <asandroq at gmail.com> wrote:
> 
> Hallo,
> 
> On 06/01/16 15:37, Marc Feeley wrote:
>> 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!
>> 
>> 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:
>> 
>> (define uint32_tag
>>  (c-lambda
>>   (TIFF* ttag_t)
>>   scheme-object
>>   "
>>    ___U32 val;
>>    if (TIFFGetField(___arg1, ___arg2, &val) == 1) {
>>      if ((___err = ___U32_to_SCMOBJ (___PSTATE, val, &___result, ___RETURN_POS)) == ___FIX(___NO_ERR)) {
>>        ___release_scmobj (___result);
>>      }
>>    } else {
>>      ___result = ___FAL;
>>    }
>>   "))
>> 
> 
> I ended up mimicking Lua and passing the state:
> 
> tiff.scm.c:
> ```
> static ___SCMOBJ uint32_tag(___processor_state ps, TIFF *tiff, ttag_t
> tag, ___SCMOBJ *err)
> {
>  uint32 val;
> 
>  if(TIFFGetField(tiff, tag, &val) == 1)
>  {
>    ___SCMOBJ ret;
>    if((*err = ___U32_to_SCMOBJ(ps, val, &ret, ___RETURN_POS)) ==
> ___FIX(___NO_ERR))
>    {
>       ___release_scmobj(ret);
>    }
>    return ret;
>  }
>  else
>  {
>    return ___FAL;
>  }
> }
> 
> // example usage
> static ___SCMOBJ tag_badfaxlines(___processor_state ps, TIFF *tiff,
> ___SCMOBJ *err)
> {
>   return uint32_tag(ps, tiff, TIFFTAG_BADFAXLINES, err);
> }
> ```
> 
> tiff.scm:
> ```
> ;; example usage
> (define tiff:tag-badfaxlines (c-lambda (TIFF) scheme-object "___result =
> tag_badfaxlines(___PSTATE, ___arg1, &___err);"))
> ```
> 
> I usually split the code into two files because I really dislike to
> write C code inside strings.
> 
> Cheers,
> -- 
> -alex
> http://unendli.ch/
> 




More information about the Gambit-list mailing list