Spontaneously I'd say so, with one important point missing:

You need handling for heap overflow.

For the u8vector clearly, and I believe for each pair allocation too.

Please update your code and post again for another round of feedback.

For your reference find a sample routine with heap overflow allocation below, and also see places doing the same in Gambit's code.

I'm not clear on the difference between the ___release_scmobj call you do and the ___still_obj_refcount_dec call the code below does - anyone feel free to clarify/remind.

Mikael

(define (make-still-u32vector len)
  (let loop ()
    (let ((r ((c-lambda (unsigned-int) scheme-object "___result = ___EXT(___alloc_scmobj)(___sU32VECTOR,___arg1*4,___STILL);
                                                      if (!___FIXNUMP(___result)) ___still_obj_refcount_dec(___result); // Inspired by ##make-u8vector of ___kernel.scm .
                                                      ") len)))

      ; Recursion here inspired by ##make-u8vector of ___kernel.scm .
      (if (not (u32vector? r))
          (begin
            (error "Heap overflow when allocating u32vector" len) ; (##raise-heap-overflow-exception) should do fine too.
            (loop))
          (begin
            (u32vector-fill! r 0)
            r)))))


2013/9/23 Vijay Mathew <vijay.the.lisper@gmail.com>
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

_______________________________________________
Gambit-list mailing list
Gambit-list@iro.umontreal.ca
https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list