[gambit-list] Returning a byte array from C
Marc Feeley
feeley at iro.umontreal.ca
Wed Sep 25 11:04:48 EDT 2013
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
More information about the Gambit-list
mailing list