All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mats Petersson <mats.petersson@citrix.com>
To: xen-devel@lists.xen.org
Subject: Re: Suggestion: Improve hypercall Interface to get real return value
Date: Wed, 5 Dec 2012 11:54:58 +0000	[thread overview]
Message-ID: <50BF3612.9020606@citrix.com> (raw)
In-Reply-To: <CAFLBxZa+Cq0Zzbg69Zr-o6fo4g+P+h5mb+3eCgFOWQoxx_J9sw@mail.gmail.com>

On 05/12/12 11:05, George Dunlap wrote:
> On Wed, Dec 5, 2012 at 6:21 AM, Yanzhang Li <liyz@pku.edu.cn 
> <mailto:liyz@pku.edu.cn>> wrote:
>
>
>
>     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.
I think returning -1 instead of "the error" allows for simpler code when 
you do something like this:
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

  reply	other threads:[~2012-12-05 11:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-05  6:21 Suggestion: Improve hypercall Interface to get real return value Yanzhang Li 
2012-12-05 11:05 ` George Dunlap
2012-12-05 11:54   ` Mats Petersson [this message]
     [not found] <1320657526.120.1354947728395.JavaMail.root@bj-mail05.pku.edu.cn>
2012-12-08  6:44 ` Yanzhang Li 

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50BF3612.9020606@citrix.com \
    --to=mats.petersson@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.