[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] Suggestion: Improve hypercall Interface to get real return value
On 05/12/12 11:05, George Dunlap wrote: On Wed, Dec 5, 2012 at 6:21 AM, Yanzhang Li <liyz@xxxxxxxxxx <mailto:liyz@xxxxxxxxxx>> wrote:I think returning -1 instead of "the error" allows for simpler code when you do something like this:Do you think this would be a good modification? Also, I am curious why the original design didn't do that. Is it a bug or is it designed that way intentionally? Any suggestions and comments will be highly appreciated.The reason we just return -1 is because that is the standard practice for Unix system libraries: to return -1 but set the error value in "errno". I couldn't tell you why Unix does this, but there's an advantage to following standard interfaces, because it reduces the surprise factor, and reduces the amount of information programmers need to keep in their head. int func() { FILE *f = fopen(...); if(!f) return -1; while(...) { if (fread(f, ...) < 0) { fclose(f); return -1; } ... if (...) if (fwrite(f, ...) < 0) { fclose(f); return -1; } } fclose(f); return 0; }Now, we don't need extra code to "remember the errno from the failing function". [And I'm assuming here that fclose isn't "interfering" with the errno - if you REALLY need to know for sure what the errno was at fread or fwrite, you still need to "remember errno". (Note that some functions do not return -1 for failure in the above code, but for example NULL, and some function would not be able to return -errno, as that may well be a "valid" return value - so keeping the interface as alike as possible is a good idea) -- Mats -George _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |