From: Pierre Habouzit <madcoder@debian.org>
To: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Cc: "Shawn O. Pearce" <spearce@spearce.org>,
Nicolas Pitre <nico@cam.org>, Junio C Hamano <gitster@pobox.com>,
Pieter de Bie <pdebie@ai.rug.nl>,
git@vger.kernel.org, Scott Chacon <schacon@gmail.com>
Subject: Re: libgit2 - a true git library
Date: Sat, 01 Nov 2008 12:01:20 +0100 [thread overview]
Message-ID: <20081101110120.GA3819@artemis.corp> (raw)
In-Reply-To: <alpine.DEB.1.00.0811010320370.22125@pacific.mpi-cbg.de.mpi-cbg.de>
[-- Attachment #1: Type: text/plain, Size: 4088 bytes --]
On Sat, Nov 01, 2008 at 02:26:45AM +0000, Johannes Schindelin wrote:
> Hi,
>
> On Fri, 31 Oct 2008, Shawn O. Pearce wrote:
>
> > Nicolas Pitre <nico@cam.org> wrote:
> > > On Fri, 31 Oct 2008, Shawn O. Pearce wrote:
> > >
> > > > > Both the negative code and errno style are lightweight in the
> > > > > common "no error" case. The errno style is probably more handy
> > > > > for those functions returning a pointer which should be NULL in
> > > > > the error case.
>
> Unfortunately, errno would not be thread-safe, unless you can guarantee
> that errno is a thread-local variable.
Well, TLS afaict is implemented on arches that have GNU ld, or on win32
with a recent enough mingw. Though this is quite a requirement.
> > Oh, good point. We could also stagger the errors so they are
> > always odd, and never return an odd-alignment pointer from a
> > successful function. Thus IS_ERR can be written as:
> >
> > #define IS_ERR(ptr) (((intptr_t)(ptr)) & 1)
> >
> > which is quite cheap, and given the (probably required anyway)
> > aligned allocation policy means we still have 2^31 possible
> > error codes available.
>
> Oh boy, both solutions are ugly as hell. Although the &1 method does not
> limit the memory space as much (except if you plan to work in
> space-contrained environments, where you do not want to be forced to
> word-align structs).
>
> The only pointer game I would remotely consider clean is if you had
>
> const char *errors[] = {
> ...
> };
>
> inline int is_error(void *ptr) {
> return ptr >= errors && ptr < errors + ARRAY_SIZE(errors);
> }
Well, you can't return _sanely_ an error through a pointer. The &1
method is broken as soon as you return a char* (there is an alignment
requirement for malloc, not for any pointer out there), hence shall not
be used, as it would not be the sole way to test for error.
Another option, that is _theorically_ not portable, but is ttbomk on all
the platforms we intend to support (IOW POSIX-ish and windows), is to
use "small" values of the pointers for errors. [NULL .. (void *)(PAGE_SIZE - 1)[
cannot exist, which gives us probably always 512 different errors, and
the test is ((uintptr_t)ptr < (PAGE_SIZE)) which is cheap. It's butt
ugly, but encoding errors into pointers is butt ugly in the first place.
Another option that is what I would prefer, would be for the use of
errno where it makes sense. E.g. if you want a function that fetches an
object, this is somehow what read(3) would look like on our store, more
or less, and errno's are enough. For the other functions where errno
cannot be used, I'm pretty sure we will always pass a handle to some
kind of libgit2 stuff, like the "repository" we're working on. The
_easiest_ way is to put the "last error" into that structure and use
that. I mean, if we want libgit2 to be useful for _everyone_ we *WILL*
have to pass a repository context around. I see almost no way around it.
And there, NULL means error, and if you want to know about the specific
error, git_repo_errno(&ctx) / git_repo_strerr(&ctx) is just easy.
Note: What is important is to be able to check for errors _fast_, I
don't think printing out the error and knowing which error it was would
be in the fast path, so it's less useful to have this information
immediately.
_My_ taste (but again, like the _t I would use what is there, and won't
make a fuss about it at all) is to see function return -1 or NULL for
errors, and "abuse" errno for system-like functions, or put the last
error into the context on which you're working (your "this" more or
less). We don't need a specific error context at all, because we already
have a repository context available.
I'm not really a fan of pointer semantics abuse (it's sometimes useful,
but as the public interface of a library, this is butt-ugly.
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org
[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]
next prev parent reply other threads:[~2008-11-01 11:02 UTC|newest]
Thread overview: 83+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-31 17:07 libgit2 - a true git library Shawn O. Pearce
2008-10-31 17:28 ` Pieter de Bie
2008-10-31 17:29 ` Pieter de Bie
2008-10-31 17:47 ` Pierre Habouzit
2008-10-31 18:41 ` Shawn O. Pearce
2008-10-31 18:54 ` Pierre Habouzit
2008-10-31 19:57 ` Shawn O. Pearce
2008-10-31 20:12 ` Pierre Habouzit
2008-10-31 20:05 ` Junio C Hamano
2008-10-31 21:58 ` Shawn O. Pearce
2008-11-01 17:30 ` Pierre Habouzit
2008-11-01 18:44 ` Andreas Ericsson
2008-11-01 18:48 ` Pierre Habouzit
2008-11-01 20:29 ` Shawn O. Pearce
2008-11-01 21:58 ` Andreas Ericsson
2008-11-02 1:50 ` Shawn O. Pearce
2008-11-03 10:17 ` Andreas Ericsson
2008-11-02 1:56 ` Shawn O. Pearce
2008-11-02 9:25 ` Pierre Habouzit
2008-10-31 20:24 ` Nicolas Pitre
2008-10-31 20:29 ` david
2008-10-31 20:56 ` Nicolas Pitre
2008-10-31 21:43 ` Shawn O. Pearce
2008-10-31 21:50 ` Shawn O. Pearce
2008-10-31 21:51 ` Pierre Habouzit
2008-10-31 21:31 ` Pierre Habouzit
2008-10-31 22:10 ` Nicolas Pitre
2008-11-01 10:52 ` Andreas Ericsson
2008-10-31 23:24 ` Pieter de Bie
2008-10-31 23:28 ` Shawn O. Pearce
2008-10-31 23:49 ` Junio C Hamano
2008-11-01 0:02 ` Pierre Habouzit
2008-11-01 0:19 ` Shawn O. Pearce
2008-11-01 1:02 ` Pierre Habouzit
2008-11-01 0:13 ` Shawn O. Pearce
2008-11-01 1:15 ` Nicolas Pitre
2008-11-01 1:19 ` Shawn O. Pearce
2008-11-01 1:45 ` Nicolas Pitre
2008-11-01 1:52 ` Shawn O. Pearce
2008-11-01 2:26 ` Johannes Schindelin
2008-11-01 11:01 ` Pierre Habouzit [this message]
2008-11-01 13:50 ` Nicolas Pitre
2008-11-01 17:01 ` Pierre Habouzit
2008-11-01 20:26 ` Johannes Schindelin
2008-10-31 23:14 ` Junio C Hamano
2008-10-31 23:33 ` Pierre Habouzit
2008-10-31 23:41 ` Shawn O. Pearce
2008-10-31 23:56 ` Jakub Narebski
2008-11-01 0:41 ` david
2008-11-01 1:00 ` Shawn O. Pearce
2008-11-01 1:04 ` david
2008-11-01 1:08 ` Pierre Habouzit
2008-11-01 1:33 ` Nicolas Pitre
2008-11-01 1:38 ` Pierre Habouzit
2008-11-01 1:49 ` Nicolas Pitre
2008-11-01 1:43 ` Shawn O. Pearce
2008-11-01 1:53 ` Nicolas Pitre
2008-11-01 22:57 ` Shawn O. Pearce
2008-11-02 0:26 ` Scott Chacon
2008-11-02 1:07 ` Scott Chacon
2008-11-02 1:36 ` Shawn O. Pearce
2008-11-02 5:09 ` David Brown
2008-11-03 16:20 ` Shawn O. Pearce
2008-11-01 1:06 ` Pierre Habouzit
2008-11-01 1:36 ` david
2008-10-31 20:24 ` Brian Gernhardt
2008-10-31 21:59 ` Andreas Ericsson
2008-10-31 22:01 ` Shawn O. Pearce
2008-10-31 22:51 ` Junio C Hamano
2008-11-01 11:17 ` Andreas Ericsson
2008-10-31 23:22 ` Johannes Schindelin
2008-10-31 23:18 ` Bruno Santos
2008-10-31 23:25 ` Shawn O. Pearce
2008-11-01 19:18 ` Andreas Ericsson
2008-11-01 20:42 ` Shawn O. Pearce
2008-11-02 2:30 ` Johannes Schindelin
2008-11-02 9:19 ` Pierre Habouzit
2008-11-03 13:08 ` Andreas Ericsson
2008-11-08 13:26 ` Steve Frécinaux
2008-11-08 14:35 ` Andreas Ericsson
2008-11-08 17:27 ` Pierre Habouzit
2008-11-09 10:17 ` Andreas Ericsson
2008-11-09 21:02 ` Shawn O. Pearce
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=20081101110120.GA3819@artemis.corp \
--to=madcoder@debian.org \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=nico@cam.org \
--cc=pdebie@ai.rug.nl \
--cc=schacon@gmail.com \
--cc=spearce@spearce.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).