From: Daniel Phillips <phillips@arcor.de>
To: Thunder from the hill <thunder@lightweight.ods.org>,
William Lee Irwin III <wli@holomorphy.com>
Cc: <linux-kernel@vger.kernel.org>
Subject: Re: Generic list push/pop
Date: Tue, 20 Aug 2002 17:30:28 +0200 [thread overview]
Message-ID: <E17hAxg-00011z-00@starship> (raw)
In-Reply-To: <Pine.LNX.4.44.0208200701580.3234-100000@hawkeye.luckynet.adm>
On Tuesday 20 August 2002 15:08, Thunder from the hill wrote:
> ...Anyway, this work had already been done by
> Thomas 'Dent' Mirlacher. We might want to work on that.
>
> [...]
>
> +#define slist_add_front(_new, _head) \
> +do { \
> + _new->next = _head; \
> + _head = _new->next; \
> +} while (0)
The second line is equivalent to _head = _head.
> +#define slist_add(_new, _head) \
> +do { \
> + _new->next = _head->next; \
> + _head->next = _new; \
> +} while (0)
I don't see the point of this. Why doesn't the caller just push_list onto
head->next?
Anyway, since I went to the trouble of writing versions of push/pop_list
that at least avoid multiple argument evaluation, here they are in all their
glorious ugliness:
#define push_list(list, node) do { \
typeof(list) *_LIST_ = &(list), _NODE_ = (node); \
_NODE_->next = *_LIST_; \
*_LIST_ = _NODE_; } while (0)
#define pop_list(list) ({ \
typeof(list) *_LIST_ = &(list), _NODE_ = *_LIST_; \
*_LIST_ = (*_LIST_)->next; \
_NODE_; })
It's unecessary to obfuscate the macro parameter names. On the other hand,
if somebody passes in an expression that happens to contain one of the
obfuscated local variable names, bad things will happen. On the third hand,
if somebody does that they probably need bad things to happen to them.
This problem arises only because of C's idiotic policy of entering the new
local symbol before parsing the initializer, and there is nothing you can do
about it[1] except to avoid using obfuscated variable names in normal code,
and check carefully for nested obfuscated variables every time you write a
macro.
The other problems with these constructions are:
- How do we know gcc will successfully optimize these things to the
same code you'd get if you simply wrote the two required assignments
out in full? The local variables should disappear early in constant
expression evaluation, but do they always?
- We assume the link field is named 'next'.
- They are ugly (but I don't care. If you need to feast your eyes on
ugly, look at any pgtable.h)
As promised, I moved them to my scraps.c and just wrote the code out in full.
[1] If we uniformly adopt the convention of encoding the name of the macro
into any locals declared in macros, plus a convention to indicate the end of
name, I suppose we could gaurantee uniqueness. Or we can just keep muddling
along as usual (more likely).
--
Daniel
next prev parent reply other threads:[~2002-08-20 15:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-18 19:21 Generic list push/pop Daniel Phillips
2002-08-18 19:28 ` Thunder from the hill
2002-08-18 20:34 ` Daniel Phillips
2002-08-19 12:05 ` William Lee Irwin III
2002-08-19 12:32 ` Daniel Phillips
2002-08-20 13:08 ` Thunder from the hill
2002-08-20 15:30 ` Daniel Phillips [this message]
2002-08-20 15:45 ` Thunder from the hill
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=E17hAxg-00011z-00@starship \
--to=phillips@arcor.de \
--cc=linux-kernel@vger.kernel.org \
--cc=thunder@lightweight.ods.org \
--cc=wli@holomorphy.com \
/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.