Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1918082ybd; Thu, 27 Jun 2019 03:50:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqyzoD5m8N6DPW9UHQlQe8mKJ3CTKuvOizAWdjk0sENzLTK7cKS/mLDLLBF1+vcdDy13qjGF X-Received: by 2002:a63:f852:: with SMTP id v18mr3215576pgj.429.1561632642364; Thu, 27 Jun 2019 03:50:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561632642; cv=none; d=google.com; s=arc-20160816; b=mkF1nVLr2uQgNXjKybqYOfLNSIwWlPC4lPBhBGRBZeBWlkVzn0PxWfXTnN7RfGO3/U 8q0Lyo/N8MicQ/2JrSyE/BVvRZHHSuBudmZ/RWu2zAUrUu/42jFxYMvfp49/0knxTOGe +hBWR1zPxLsDFPsaQ9TvyO8q7ZUeKf1gI0JV9tklDANNStr8EO5RmyiXS5lCTSoPA1SU lkpZeSSQnJg1psR+iRVoorCGyy9Tlnwg6b9q4qeUzzqGUemM6xXK2pAZTsO8k2AfaY0y 2oOl2j7b7vJJvhxvh9qofsOroKXGgCeu3jvFQEk0L+J20MUycNIicbCsOqzlrT41/LHJ E9TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O/m/0mITlwkr33AYZ7K25oFGl9VFRsPi+VkSt0vj1f0=; b=RVXwsnrMfdUmL2DE78eAXipLboGl3Kp2i4MJElmS99XPLSSiBvgJAApSqTnTibXVtV KVc2PMlZ7VGZOtFMHK4DVVv4rItPYA+BQPT4R6NBsFRHXWUdFcL9XMxKNwLdOeyyK8bp ijBp40hBbAZLVRIMdQogTFhQq9OjKxNzE0YX0Ku8U5dWy9+hwE1dVsNJwXZbM0x3bFRM jYoNq6OYQ89er4QoLaHDFeEwKG3wLd6A8Du9ynMFBUWH/a+gYLUZOzYYI+8Fc40gpOqr 36pyNYrtRUWzGeM7mSKtVmTaNRfOZD3lS5AtnoBgPxgkiYnVohJFRCKLb2Bc9foTfo9S /3sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=CIOQvgjl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y187si1910496pgb.480.2019.06.27.03.50.26; Thu, 27 Jun 2019 03:50:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=CIOQvgjl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726664AbfF0Ksz (ORCPT + 99 others); Thu, 27 Jun 2019 06:48:55 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:52062 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726382AbfF0Ksx (ORCPT ); Thu, 27 Jun 2019 06:48:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=O/m/0mITlwkr33AYZ7K25oFGl9VFRsPi+VkSt0vj1f0=; b=CIOQvgjlgqk7W/bqo845BtZRB1 fGrrqGQBmXkU4I1WzpZKRyf2DJytDLYx+tNfjTCNuLuQrEgbBKlsPcD9V2SxydjCvGCR8n/xHXAoN vSeCZnBxli4WYAjTr2m81/TTXTWWdmiDH5fDYO+Y/wyF33T229TmmgbtzGi/f8scksTv6hO8gY7n3 NBf+2+GcHbF0kcb0Jobtr3wvrsU62qPNqWwSZRcYt2opxJ3UTEEIsG1q90t2Ft0gRFwJ9FNOITodc UKbfhJCU8s9yhMPu4c0WDZdFQ2YxdxyDb+ZPa+mcMcxOSUbt3S+/FhLB0MVQQ10WIMovXEUgTQwqY YmqUAgXw==; Received: from 089144214055.atnat0023.highway.a1.net ([89.144.214.55] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1hgRxL-00053F-RM; Thu, 27 Jun 2019 10:48:44 +0000 From: Christoph Hellwig To: "Darrick J . Wong" Cc: Damien Le Moal , Andreas Gruenbacher , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 01/13] list.h: add list_pop and list_pop_entry helpers Date: Thu, 27 Jun 2019 12:48:24 +0200 Message-Id: <20190627104836.25446-2-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190627104836.25446-1-hch@lst.de> References: <20190627104836.25446-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- include/linux/list.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/linux/list.h b/include/linux/list.h index e951228db4b2..ba6e27d2235a 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -500,6 +500,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 -- 2.20.1