linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: hch@infradead.org, darrick.wong@oracle.com
Cc: Damien.LeMoal@wdc.com, agruenba@redhat.com,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Christoph Hellwig <hch@lst.de>
Subject: [PATCH 1/6] list.h: add list_pop and list_pop_entry helpers
Date: Mon, 29 Jul 2019 18:17:46 -0700	[thread overview]
Message-ID: <156444946635.2682261.14776328020249327506.stgit@magnolia> (raw)
In-Reply-To: <156444945993.2682261.3926017251626679029.stgit@magnolia>

From: Christoph Hellwig <hch@lst.de>

We have a very common pattern where we want to delete the first entry
from a list and return it as the properly typed container structure.

Add two helpers to implement this behavior.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 include/linux/list.h |   33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)


diff --git a/include/linux/list.h b/include/linux/list.h
index 85c92555e31f..d3b00267446a 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -514,6 +514,39 @@ static inline void list_splice_tail_init(struct list_head *list,
 	pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
 })
 
+/**
+ * list_pop - delete the first entry from a list and return it
+ * @list:	the list to take the element from.
+ *
+ * Return the list entry after @list.  If @list is empty return NULL.
+ */
+static inline struct list_head *list_pop(struct list_head *list)
+{
+	struct list_head *pos = READ_ONCE(list->next);
+
+	if (pos == list)
+		return NULL;
+	list_del(pos);
+	return pos;
+}
+
+/**
+ * list_pop_entry - delete the first entry from a list and return the
+ *			containing structure
+ * @list:	the list to take the element from.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_head within the struct.
+ *
+ * Return the containing structure for the list entry after @list.  If @list
+ * is empty return NULL.
+ */
+#define list_pop_entry(list, type, member)			\
+({								\
+	struct list_head *pos__ = list_pop(list);		\
+								\
+	pos__ ? list_entry(pos__, type, member) : NULL;		\
+})
+
 /**
  * list_next_entry - get the next element in list
  * @pos:	the type * to cursor

WARNING: multiple messages have this Message-ID (diff)
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: hch@infradead.org, darrick.wong@oracle.com
Cc: Damien.LeMoal@wdc.com, agruenba@redhat.com,
	"Matthew Wilcox \(Oracle\)" <willy@infradead.org>,
	linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Christoph Hellwig <hch@lst.de>
Subject: [PATCH 1/6] list.h: add list_pop and list_pop_entry helpers
Date: Mon, 29 Jul 2019 18:17:46 -0700	[thread overview]
Message-ID: <156444946635.2682261.14776328020249327506.stgit@magnolia> (raw)
Message-ID: <20190730011746.xMTQJgjkMfjeby_Hs_PuuoSg99vB6aqIvDzGo1Wh-N0@z> (raw)
In-Reply-To: <156444945993.2682261.3926017251626679029.stgit@magnolia>

From: Christoph Hellwig <hch@lst.de>

We have a very common pattern where we want to delete the first entry
from a list and return it as the properly typed container structure.

Add two helpers to implement this behavior.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 include/linux/list.h |   33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)


diff --git a/include/linux/list.h b/include/linux/list.h
index 85c92555e31f..d3b00267446a 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -514,6 +514,39 @@ static inline void list_splice_tail_init(struct list_head *list,
 	pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
 })
 
+/**
+ * list_pop - delete the first entry from a list and return it
+ * @list:	the list to take the element from.
+ *
+ * Return the list entry after @list.  If @list is empty return NULL.
+ */
+static inline struct list_head *list_pop(struct list_head *list)
+{
+	struct list_head *pos = READ_ONCE(list->next);
+
+	if (pos == list)
+		return NULL;
+	list_del(pos);
+	return pos;
+}
+
+/**
+ * list_pop_entry - delete the first entry from a list and return the
+ *			containing structure
+ * @list:	the list to take the element from.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_head within the struct.
+ *
+ * Return the containing structure for the list entry after @list.  If @list
+ * is empty return NULL.
+ */
+#define list_pop_entry(list, type, member)			\
+({								\
+	struct list_head *pos__ = list_pop(list);		\
+								\
+	pos__ ? list_entry(pos__, type, member) : NULL;		\
+})
+
 /**
  * list_next_entry - get the next element in list
  * @pos:	the type * to cursor


  reply	other threads:[~2019-07-30  1:18 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-30  1:17 [PATCH v4 0/6] iomap: lift the xfs writepage code into iomap Darrick J. Wong
2019-07-30  1:17 ` Darrick J. Wong [this message]
2019-07-30  1:17   ` [PATCH 1/6] list.h: add list_pop and list_pop_entry helpers Darrick J. Wong
2019-07-30  1:17 ` [PATCH 2/6] iomap: copy the xfs writeback code to iomap.c Darrick J. Wong
2019-08-04 14:59   ` Andreas Gruenbacher
2019-08-06  5:33     ` Christoph Hellwig
2019-08-05 12:31   ` Andreas Gruenbacher
2019-08-06  5:32     ` Christoph Hellwig
2019-07-30  1:17 ` [PATCH 3/6] iomap: add tracing for the address space operations Darrick J. Wong
2019-07-30  1:18 ` [PATCH 4/6] iomap: warn on inline maps in iomap_writepage_map Darrick J. Wong
2019-07-30  1:18 ` [PATCH 5/6] xfs: set IOMAP_F_NEW more carefully Darrick J. Wong
2019-07-30  1:18 ` [PATCH 6/6] iomap: zero newly allocated mapped blocks Darrick J. Wong
2019-07-30 14:48 ` [PATCH v4 0/6] iomap: lift the xfs writepage code into iomap Christoph Hellwig
2019-08-05 12:34   ` Andreas Gruenbacher
2019-08-16  6:52 ` Christoph Hellwig
2019-08-17  1:46   ` Darrick J. Wong
2019-08-17  8:25     ` Andreas Gruenbacher
2019-08-17 13:15     ` Damien Le Moal
2019-08-20  7:30     ` Christoph Hellwig
2019-09-01  7:34     ` Christoph Hellwig
2019-09-01 20:44       ` Darrick J. Wong
2019-09-02 17:16         ` Christoph Hellwig
2019-09-10  7:01           ` Christoph Hellwig
2019-09-10 21:30             ` Dave Chinner

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=156444946635.2682261.14776328020249327506.stgit@magnolia \
    --to=darrick.wong@oracle.com \
    --cc=Damien.LeMoal@wdc.com \
    --cc=agruenba@redhat.com \
    --cc=hch@infradead.org \
    --cc=hch@lst.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=willy@infradead.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).