[gambit-list] Returning a byte array from C

Vijay Mathew vijay.the.lisper at gmail.com
Thu Sep 26 00:21:49 EDT 2013


Thanks again for all the great feedback!

I think the call to ____FIX() is not required if Scheme code access the C
function through a declaration like this:

(define read-bytes (c-lambda (int unsigned-int scheme-object) int
"read_bytes"))

Is that correct?

Thanks,

--Vijay


On Wed, Sep 25, 2013 at 8:34 PM, Marc Feeley <feeley at iro.umontreal.ca>wrote:

>
> On 2013-09-25, at 4:48 AM, Vijay Mathew <vijay.the.lisper at gmail.com>
> wrote:
>
> > Hi,
> >
> > Thanks for reviewing my code.
> >
> > I have updated the C function as follows:
> >
> > int read_bytes (int fd, size_t count, ___SCMOBJ data_vect)
> >  {
> >    void *data = malloc (count);
> >    int result;
> >
> >    if (data == NULL)
> >      result = -1;
> >    else
> >      {
> >        result = read (fd, data, count);
> >        if (result > 0)
> >          memcpy (___BODY (data_vect), data, result);
> >        free (data);
> >      }
> >    return result;
> >  }
> >
> > A call from scheme will look like:
> >
> > (read-bytes fd count (make-u8vector count 0))
> >
> > I suppose now I don't have to worry about leaks or
> heap-overflow-exceptions.
>
> You can simplify you code further to:
>
>  int read_bytes (int fd, size_t count, ___SCMOBJ data_vect)
>  {
>    return ___FIX(read (fd, ___BODY(data_vect), count));
>  }
>
> Marc
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20130926/b2bb1bb5/attachment.htm>


More information about the Gambit-list mailing list