[PATCH] lib min_heap: add min_heap_empty()/min_heap_empty_inline()

ColtenOuO posted 1 patch 3 days, 6 hours ago
include/linux/min_heap.h | 13 +++++++++++++
lib/min_heap.c           |  6 ++++++
2 files changed, 19 insertions(+)
[PATCH] lib min_heap: add min_heap_empty()/min_heap_empty_inline()
Posted by ColtenOuO 3 days, 6 hours ago
Documentation/core-api/min_heap.rst documents min_heap_empty() and
min_heap_empty_inline() as part of the Min Heap API, but these
functions were never actually implemented. Add them, following the
same pattern as the existing min_heap_full()/min_heap_full_inline().

Signed-off-by: ColtenOuO <jun930436@gmail.com>
---
 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 79ddc0adbf2b..931101c29e1d 100644
--- a/include/linux/min_heap.h
+++ b/include/linux/min_heap.h
@@ -252,6 +252,16 @@ bool __min_heap_full_inline(min_heap_char *heap)
 #define min_heap_full_inline(_heap)	\
 	__min_heap_full_inline(container_of(&(_heap)->nr, min_heap_char, nr))
 
+/* Check if the heap is empty. */
+static __always_inline
+bool __min_heap_empty_inline(min_heap_char *heap)
+{
+	return heap->nr == 0;
+}
+
+#define min_heap_empty_inline(_heap)	\
+	__min_heap_empty_inline(container_of(&(_heap)->nr, min_heap_char, nr))
+
 /* Sift the element at pos down the heap. */
 static __always_inline
 void __min_heap_sift_down_inline(min_heap_char *heap, size_t pos, size_t elem_size,
@@ -431,6 +441,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, size_t 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, size_t 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,
@@ -452,6 +463,8 @@ bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx,
 	(__minheap_cast(_heap) __min_heap_peek(container_of(&(_heap)->nr, min_heap_char, nr)))
 #define min_heap_full(_heap)	\
 	__min_heap_full(container_of(&(_heap)->nr, min_heap_char, nr))
+#define min_heap_empty(_heap)	\
+	__min_heap_empty(container_of(&(_heap)->nr, min_heap_char, nr))
 #define min_heap_sift_down(_heap, _pos, _func, _args)	\
 	__min_heap_sift_down(container_of(&(_heap)->nr, min_heap_char, nr), _pos,	\
 			     __minheap_obj_size(_heap), _func, _args)
diff --git a/lib/min_heap.c b/lib/min_heap.c
index 96f01a4c5fb6..9e3978a78a93 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, size_t pos, size_t elem_size,
 			  const struct min_heap_callbacks *func, void *args)
 {
-- 
2.43.0
Re: [PATCH] lib min_heap: add min_heap_empty()/min_heap_empty_inline()
Posted by Kuan-Wei Chiu 3 days, 5 hours ago
On Tue, Jul 21, 2026 at 05:21:08PM +0000, ColtenOuO wrote:
> Documentation/core-api/min_heap.rst documents min_heap_empty() and
> min_heap_empty_inline() as part of the Min Heap API, but these
> functions were never actually implemented. Add them, following the

NAK.

We generally do not introduce new APIs or helper functions that have no
in-tree users. Just clean up the doc to reflect reality, rather than
adding unused code.

> same pattern as the existing min_heap_full()/min_heap_full_inline().
> 
> Signed-off-by: ColtenOuO <jun930436@gmail.com>

Please use your real name for the Sob tag.

Regards,
Kuan-Wei

> ---
>  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 79ddc0adbf2b..931101c29e1d 100644
> --- a/include/linux/min_heap.h
> +++ b/include/linux/min_heap.h
> @@ -252,6 +252,16 @@ bool __min_heap_full_inline(min_heap_char *heap)
>  #define min_heap_full_inline(_heap)	\
>  	__min_heap_full_inline(container_of(&(_heap)->nr, min_heap_char, nr))
>  
> +/* Check if the heap is empty. */
> +static __always_inline
> +bool __min_heap_empty_inline(min_heap_char *heap)
> +{
> +	return heap->nr == 0;
> +}
> +
> +#define min_heap_empty_inline(_heap)	\
> +	__min_heap_empty_inline(container_of(&(_heap)->nr, min_heap_char, nr))
> +
>  /* Sift the element at pos down the heap. */
>  static __always_inline
>  void __min_heap_sift_down_inline(min_heap_char *heap, size_t pos, size_t elem_size,
> @@ -431,6 +441,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, size_t 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, size_t 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,
> @@ -452,6 +463,8 @@ bool __min_heap_del(min_heap_char *heap, size_t elem_size, size_t idx,
>  	(__minheap_cast(_heap) __min_heap_peek(container_of(&(_heap)->nr, min_heap_char, nr)))
>  #define min_heap_full(_heap)	\
>  	__min_heap_full(container_of(&(_heap)->nr, min_heap_char, nr))
> +#define min_heap_empty(_heap)	\
> +	__min_heap_empty(container_of(&(_heap)->nr, min_heap_char, nr))
>  #define min_heap_sift_down(_heap, _pos, _func, _args)	\
>  	__min_heap_sift_down(container_of(&(_heap)->nr, min_heap_char, nr), _pos,	\
>  			     __minheap_obj_size(_heap), _func, _args)
> diff --git a/lib/min_heap.c b/lib/min_heap.c
> index 96f01a4c5fb6..9e3978a78a93 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, size_t pos, size_t elem_size,
>  			  const struct min_heap_callbacks *func, void *args)
>  {
> -- 
> 2.43.0
>