[gambit-list] Returning a byte array from C

Vijay Mathew vijay.the.lisper at gmail.com
Mon Sep 23 11:14:35 EDT 2013


The following C function is a wrapper for the `read' system call.
It returns a pair that contains the number of bytes read and the data.
Is this the right way to initialize a u8vector from C?

 ___SCMOBJ read_bytes (int fd, size_t count)
 {
   void *data = malloc (count);
   ___SCMOBJ result;

   if (data == NULL)
     result = ___make_pair (___BOOLEAN (0), ___BOOLEAN (0), ___STILL);
   else
     {
       int r = read (fd, data, count);
       ___SCMOBJ retcode;
       ___SCMOBJ ___err;
       ___BEGIN_SFUN_INT_TO_SCMOBJ (r, retcode, 1);
       ___END_SFUN_INT_TO_SCMOBJ (r, retcode, 1);
       if (r == 0)
         result = ___make_pair (retcode, ___BOOLEAN (0), ___STILL);
       else if (r > 0)
         {
           ___SCMOBJ data_vect;
           data_vect = ___alloc_scmobj(___sU8VECTOR, count, ___STILL);
           ___EXT (___release_scmobj) (data_vect);
           memcpy (___BODY (data_vect), data, r);
           result = ___make_pair (retcode, data_vect, ___STILL);
         }
       else
         result = ___make_pair (retcode, ___BOOLEAN (0), ___STILL);
       free (data);
       ___release_scmobj (result);
     }
   return result;
 }

Thanks,

--Vijay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20130923/f867d02b/attachment.htm>


More information about the Gambit-list mailing list