From patchwork Fri Jul 5 20:41:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsODwrZybiBFbmdlbA==?= X-Patchwork-Id: 2824389 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B74B59F3EB for ; Fri, 5 Jul 2013 22:13:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D940620140 for ; Fri, 5 Jul 2013 22:13:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 089EB20125 for ; Fri, 5 Jul 2013 22:13:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751817Ab3GEWMV (ORCPT ); Fri, 5 Jul 2013 18:12:21 -0400 Received: from longford.logfs.org ([213.229.74.203]:59751 "EHLO longford.logfs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751780Ab3GEWMR (ORCPT ); Fri, 5 Jul 2013 18:12:17 -0400 Received: from joern by longford.logfs.org with local (Exim 4.72) (envelope-from ) id 1UvCor-0004XP-Qh; Fri, 05 Jul 2013 16:41:29 -0400 Date: Fri, 5 Jul 2013 16:41:29 -0400 From: =?utf-8?B?SsO2cm4=?= Engel To: linux-kernel@vger.kernel.org Cc: Chris Mason , linux-btrfs@vger.kernel.org Subject: [PATCH 1/2] list: add list_del_each_entry Message-ID: <20130705204129.GB15943@logfs.org> References: <1370280485-10047-1-git-send-email-joern@logfs.org> <20130705204100.GA15943@logfs.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20130705204100.GA15943@logfs.org> User-Agent: Mutt/1.5.20 (2009-06-14) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP I have seen a lot of boilerplate code that either follows the pattern of while (!list_empty(head)) { pos = list_entry(head->next, struct foo, list); list_del(pos->list); ... } or some variant thereof. With this patch in, people can use list_del_each_entry(pos, head, list) { ... } The patch also adds a list_del_each variant, even though I have only found a single user for that one so far. Signed-off-by: Joern Engel --- include/linux/list.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/list.h b/include/linux/list.h index 6a1f8df..ab39c7d 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -557,6 +557,24 @@ static inline void list_splice_tail_init(struct list_head *list, #define list_safe_reset_next(pos, n, member) \ n = list_entry(pos->member.next, typeof(*pos), member) +/** + * list_del_each - removes an entry from the list until it is empty + * @pos: the &struct list_head to use as a loop cursor. + * @head: the head of your list. + */ +#define list_del_each(pos, head) \ + while (list_empty(head) ? 0 : (pos = (head)->next, list_del(pos), 1)) + +/** + * list_del_each_entry - removes an entry from the list until it is empty + * @pos: the type * to use as loop cursor. + * @head: the head of your list. + * @member: the name of the list_struct within the struct + */ +#define list_del_each_entry(pos, head, member) \ + while (list_empty(head) && (pos = list_first_entry((head), \ + typeof(*pos), member), list_del((head)->next), 1)) + /* * Double linked lists with a single pointer list head. * Mostly useful for hash tables where the two pointer list head is