[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20241207131948.1881301-1-visitorckw@gmail.com>
Date: Sat, 7 Dec 2024 21:19:48 +0800
From: Kuan-Wei Chiu <visitorckw@...il.com>
To: kent.overstreet@...ux.dev
Cc: irogers@...gle.com,
colyli@...e.de,
peterz@...radead.org,
akpm@...ux-foundation.org,
jserv@...s.ncku.edu.tw,
linux-bcachefs@...r.kernel.org,
linux-kernel@...r.kernel.org,
Kuan-Wei Chiu <visitorckw@...il.com>
Subject: [PATCH] lib min_heap: Add min_heap_empty()
Add min_heap_empty() which returns a boolean value indicating whether
the heap is empty.
Signed-off-by: Kuan-Wei Chiu <visitorckw@...il.com>
---
Hi Kent,
If you plan to use min_heap_empty(), feel free to take this patch.
include/linux/min_heap.h | 13 +++++++++++++
lib/min_heap.c | 6 ++++++
2 files changed, 19 insertions(+)
diff --git a/include/linux/min_heap.h b/include/linux/min_heap.h
index e781727c8916..bba01453c522 100644
--- a/include/linux/min_heap.h
+++ b/include/linux/min_heap.h
@@ -240,6 +240,16 @@ bool __min_heap_full_inline(min_heap_char *heap)
#define min_heap_full_inline(_heap) \
__min_heap_full_inline((min_heap_char *)_heap)
+/* Check if the heap is empty. */
+static __always_inline
+bool __min_heap_empty_inline(min_heap_char *heap)
+{
+ return !heap->nr;
+}
+
+#define min_heap_empty_inline(_heap) \
+ __min_heap_empty_inline(container_of(&(_heap)->nr))
+
/* Sift the element at pos down the heap. */
static __always_inline
void __min_heap_sift_down_inline(min_heap_char *heap, int pos, size_t elem_size,
@@ -417,6 +427,7 @@ bool __min_heap_del_inline(min_heap_char *heap, size_t elem_size, size_t idx,
void __min_heap_init(min_heap_char *heap, void *data, int size);
void *__min_heap_peek(struct min_heap_char *heap);
bool __min_heap_full(min_heap_char *heap);
+bool __min_heap_empty(min_heap_char *heap);
void __min_heap_sift_down(min_heap_char *heap, int pos, size_t elem_size,
const struct min_heap_callbacks *func, void *args);
void __min_heap_sift_up(min_heap_char *heap, size_t elem_size, size_t idx,
@@ -438,6 +449,8 @@ bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx,
(__minheap_cast(_heap) __min_heap_peek((min_heap_char *)_heap))
#define min_heap_full(_heap) \
__min_heap_full((min_heap_char *)_heap)
+#define min_heap_empty(_heap) \
+ __min_heap_empty(container_of(&(_heap)->nr))
#define min_heap_sift_down(_heap, _pos, _func, _args) \
__min_heap_sift_down((min_heap_char *)_heap, _pos, __minheap_obj_size(_heap), _func, _args)
#define min_heap_sift_up(_heap, _idx, _func, _args) \
diff --git a/lib/min_heap.c b/lib/min_heap.c
index 4485372ff3b1..f546cd110cea 100644
--- a/lib/min_heap.c
+++ b/lib/min_heap.c
@@ -20,6 +20,12 @@ bool __min_heap_full(min_heap_char *heap)
}
EXPORT_SYMBOL(__min_heap_full);
+bool __min_heap_empty(min_heap_char *heap)
+{
+ return __min_heap_empty_inline(heap);
+}
+EXPORT_SYMBOL(__min_heap_empty);
+
void __min_heap_sift_down(min_heap_char *heap, int pos, size_t elem_size,
const struct min_heap_callbacks *func, void *args)
{
--
2.34.1
Powered by blists - more mailing lists