[gambit-list] Converting uint32 to scheme object
Marc Feeley
feeley at iro.umontreal.ca
Wed Jan 6 09:37:10 EST 2016
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;
}
"))
Marc
> On Jan 6, 2016, at 9:06 AM, Adam <adam.mlmb at gmail.com> wrote:
>
> Right, that the boolean value not has a mapping within the int32 "namespace".
>
> How would you suggest to pass it then, Marc?
>
>
> I don't see any clear way this can be done with Scheme structures, so simply passing a C structure as result value that has accessors, per the example in my previous email, seems like the best thing. That one has the issue that it requires a malloc()/free() combo which is a bit unfortunate.
>
>
> Returning an uint64 would work of course even if it would be a hack - Marc, what is the C FFI integer type names specified to specific numbers of bits? (Usually you just say "integer" etc.)
>
>
> 2016-01-06 21:22 GMT+08:00 Marc Feeley <feeley at iro.umontreal.ca>:
> The FFI supports all the C integer types so it is simplest to let Gambit do the conversion with (c-lambda (TIFF* ttag_t) unsigned-int32 "uint32_tag").
>
> Is the issue that you need to return an integer or #f? Could one of the integer values be used instead of #f?
>
> Marc
>
> > On Jan 6, 2016, at 6:47 AM, Adam <adam.mlmb at gmail.com> wrote:
> >
> > One way would be to return a special C "int-or-boolean" structure and then access it via special accessors. E.g. (let* ((r (your-c-call))) (if (struct=false? r) #f (struct-int r))) ?
> >
> > Marc, any better ideas?
> >
> >
> > 2016-01-06 19:41 GMT+08:00 Alex Silva <asandroq at gmail.com>:
> > Hello list,
> >
> > I am currently writing some FFI code that interfaces with libtiff to
> > read tags from TIFF image files. The values of these tags have different
> > C types and using the right type is needed to avoid memory corruption.
> >
> > For reading tags with uint16 values the code is straightforward:
> >
> > ```
> > static ___SCMOBJ uint16_tag(TIFF *tiff, ttag_t tag)
> > {
> > uint16 val;
> > int ret = TIFFGetField(tiff, tag, &val);
> >
> > return ret == 1 ? ___FIX(val) : ___FAL;
> > }
> > ```
> >
> > But, since fixnums have less than 32 bits I cannot use the same code. It
> > seems the solution would be something similar to:
> >
> > ```
> > static ___SCMOBJ uint32_tag(TIFF *tiff, ttag_t tag)
> > {
> > uint32 val;
> >
> > if(TIFFGetField(tiff, tag, &val) == 1)
> > {
> > ___SCMOBJ ret;
> > ___U32_to_SCMOBJ(___ps, val, &ret, ___RETURN_POS); <---- ERROR
> > return ret;
> > }
> > else
> > {
> > return ___FAL;
> > }
> > }
> >
> > ```
> >
> > except that I don't know how to get hold of the processor state. Can
> > anybody shed a light here?
> >
> > Thanks,
> > -alex
> > http://unendli.ch/
> > _______________________________________________
> > Gambit-list mailing list
> > Gambit-list at iro.umontreal.ca
> > https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
> >
>
>
More information about the Gambit-list
mailing list