Re: Generic list push/pop

Daniel Phillips (phillips@arcor.de)
Mon, 19 Aug 2002 14:32:48 +0200


On Monday 19 August 2002 14:05, William Lee Irwin III wrote:
> On Sun, Aug 18, 2002 at 09:21:41PM +0200, Daniel Phillips wrote:
> > I took a run at writing generic single-linked list push and pop macros, to be
> > used in the form:
>
> Dear gawd, I've gone blind.
>
> How's this look?

Unfortunately, not good. You get code like:

foo = (struct mylist *) slist_pop((slist *) &somelist->next);

So type safety goes out the window, and you gain some niceness in the
definition in exchange for ugliness in usage, the wrong tradeoff imho.

> struct slist
> {
> struct slist *next;
> };
>
>
> static inline void slist_add(struct slist *head, struct slist *elem)
> {
> elem->next = head->next;
> head->next = elem;
> }
>
> #define slist_push(head, elem) slist_add(head, elem)
>
> static inline struct slist *slist_pop(struct slist *head)
> {
> struct slist *elem = head->next;
>
> if (elem) {
> head->next = elem->next;
> elem->next = NULL;
> }
> return elem;
> }

-- 
Daniel
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/