<div dir="ltr">Spontaneously I'd say so, with one important point missing:<br><br>You need handling for heap overflow.<div><br></div><div>For the u8vector clearly, and I believe for each pair allocation too.</div><div>
<br></div><div>Please update your code and post again for another round of feedback.</div><div><br></div><div>For your reference find a sample routine with heap overflow allocation below, and also see places doing the same in Gambit's code.</div>
<div><br></div><div>I'm not clear on the difference between the <font face="courier new, monospace">___release_scmobj</font> call you do and the <font face="courier new, monospace">___still_obj_refcount_dec</font> call the code below does - anyone feel free to clarify/remind.</div>
<div><br></div><div>Mikael</div><div><br></div><div><div><font face="courier new, monospace">(define (make-still-u32vector len)</font></div><div><font face="courier new, monospace"> (let loop ()</font></div><div><font face="courier new, monospace"> (let ((r ((c-lambda (unsigned-int) scheme-object "___result = ___EXT(___alloc_scmobj)(___sU32VECTOR,___arg1*4,___STILL);</font></div>
<div><font face="courier new, monospace"> if (!___FIXNUMP(___result)) ___still_obj_refcount_dec(___result); // Inspired by ##make-u8vector of ___kernel.scm .</font></div>
<div><font face="courier new, monospace"> ") len)))</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> ; Recursion here inspired by ##make-u8vector of ___kernel.scm .</font></div>
<div><font face="courier new, monospace"> (if (not (u32vector? r))</font></div><div><font face="courier new, monospace"> (begin</font></div><div><font face="courier new, monospace"> (error "Heap overflow when allocating u32vector" len) ; (##raise-heap-overflow-exception) should do fine too.</font></div>
<div><font face="courier new, monospace"> (loop))</font></div><div><font face="courier new, monospace"> (begin</font></div><div><font face="courier new, monospace"> (u32vector-fill! r 0)</font></div>
<div><font face="courier new, monospace"> r)))))</font></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/9/23 Vijay Mathew <span dir="ltr"><<a href="mailto:vijay.the.lisper@gmail.com" target="_blank">vijay.the.lisper@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>The following C function is a wrapper for the `read' system call.</div><div>It returns a pair that contains the number of bytes read and the data. </div>
<div>Is this the right way to initialize a u8vector from C?</div>
<div><br></div><div> ___SCMOBJ read_bytes (int fd, size_t count)</div><div> {</div><div> void *data = malloc (count);</div><div> ___SCMOBJ result;</div><div><br></div><div> if (data == NULL)</div><div> result = ___make_pair (___BOOLEAN (0), ___BOOLEAN (0), ___STILL);</div>
<div> else</div><div> {</div><div> int r = read (fd, data, count);</div><div> ___SCMOBJ retcode;</div><div> ___SCMOBJ ___err;</div><div> ___BEGIN_SFUN_INT_TO_SCMOBJ (r, retcode, 1);</div><div>
___END_SFUN_INT_TO_SCMOBJ (r, retcode, 1);</div><div> if (r == 0)</div><div> result = ___make_pair (retcode, ___BOOLEAN (0), ___STILL);</div><div> else if (r > 0)</div><div> {</div><div>
___SCMOBJ data_vect;</div><div> data_vect = ___alloc_scmobj(___sU8VECTOR, count, ___STILL);</div><div> ___EXT (___release_scmobj) (data_vect);</div><div> memcpy (___BODY (data_vect), data, r);</div>
<div> result = ___make_pair (retcode, data_vect, ___STILL);</div><div> }</div><div> else </div><div> result = ___make_pair (retcode, ___BOOLEAN (0), ___STILL);</div><div> free (data);</div>
<div> ___release_scmobj (result);</div><div> }</div><div> return result;</div><div> }</div><div><br></div><div>Thanks,</div><div><br></div><div>--Vijay</div></div>
<br>_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<br></blockquote></div><br></div>