[PATCH 1/3] list: add hlist_cut_number()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a function to pull a sub list containing up to @cnt nodes from @old
list. This is beneficial to performance in scenarios where N free nodes
are pulled from the global list to the local list, or N free nodes are
pushed from the local list to the global list. Because the old sub list
is no longer split and then spliced into the new list one by one, this
also enhances readability.

Signed-off-by: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
---
 include/linux/list.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/include/linux/list.h b/include/linux/list.h
index 5f4b0a39cf46a37..9fb58cbceb1245c 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -1210,4 +1210,48 @@ static inline size_t hlist_count_nodes(struct hlist_head *head)
 	return count;
 }
 
+/**
+ * hlist_cut_number - cut a list into two
+ * @new: a new list to add all removed entries
+ * @old: a list with entries
+ * @cnt: maximum of nodes need to be cut
+ * @last: save the last node of the new list
+ *
+ * This helper moves the initial part of @old, maximum of @cnt nodes, from
+ * @old to @new. @new should be an empty list or a list you do not care about
+ * losing its data.
+ *
+ * Returns the number of nodes in the @new list, maybe zero.
+ *
+ */
+static inline int hlist_cut_number(struct hlist_head *new,
+		struct hlist_head *old, int cnt, struct hlist_node **last)
+{
+	int nr = 0;
+	struct hlist_node *tmp, *pos = NULL;
+
+	if (cnt <= 0)
+		return 0;
+
+	hlist_for_each(tmp, old) {
+		nr++;
+		pos = tmp;
+		if (nr >= cnt)
+			break;
+	}
+
+	*last = pos;
+	new->first = old->first;
+
+	if (pos) {
+		new->first->pprev = &new->first;
+		old->first = pos->next;
+		if (old->first)
+			old->first->pprev = &old->first;
+		pos->next = NULL;
+	}
+
+	return nr;
+}
+
 #endif
-- 
2.34.1





[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux