Skip to contentSkip to conversation

Prio_heap: heap_remove(), heap_maximum(), heap_replace() and heap_cherrypick() (v2)

PATCH New Mathieu Desnoyers 1 participant 1 message Oct 9, 2010 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Prio_heap: heap_remove(), heap_maximum(), heap_replace() and heap_cherrypick() (v2)

These added interfaces lets prio_heap users lookup the top of heap item without performing any insertion, perform removal of the topmost heap entry, and also replacement of topmost heap entry. This is useful if one need to use the result of the lookup to determine if the current maximum should simply be removed or if it should be replaced.

This is used by the Generic Ring Buffer to perform timestamp-based fusion-merge of per-cpu buffer records into a single stream.

Changelog since v1:
- Apply cleanups recommended by Lai Jiangshan.
- Apply cleanups recommended by Paul Menage.

---
 include/linux/prio_heap.h |   47 ++++++++++++++++++++-
 lib/prio_heap.c           |   99 ++++++++++++++++++++++++++++++++++++----------
 2 files changed, 123 insertions(+), 23 deletions(-)
include/linux/prio_heap.h +45 -2
Index: linux.trees.git/include/linux/prio_heap.h
===================================================================
@@ -2,8 +2,7 @@
 #define _LINUX_PRIO_HEAP_H
 
 /*
- * Simple insertion-only static-sized priority heap containing
- * pointers, based on CLR, chapter 7
+ * Static-sized priority heap containing pointers. Based on CLR, chapter 7.
  */
 
 #include <linux/gfp.h>
@@ -23,6 +22,18 @@ struct ptr_heap {
 };
 
 /**
+ * heap_maximum - return the largest element in the heap
+ * @heap: the heap to be operated on
+ *
+ * Returns the largest element in the heap, without performing any modification
+ * to the heap structure. Returns NULL if the heap is empty.
+ */
+static inline void *heap_maximum(const struct ptr_heap *heap)
+{
+	return heap->size ? heap->ptrs[0] : NULL;
+}
+
+/**
  * heap_init - initialize an empty heap with a given memory size
  * @heap: the heap structure to be initialized
  * @size: amount of memory to use in bytes
@@ -53,6 +64,38 @@ void heap_free(struct ptr_heap *heap);
  */
 extern void *heap_insert(struct ptr_heap *heap, void *p);
 
+/**
+ * heap_remove - remove the largest element from the heap
+ * @heap: the heap to be operated on
+ *
+ * Returns the largest element in the heap. It removes this element from the
+ * heap. Returns NULL if the heap is empty.
+ */
+extern void *heap_remove(struct ptr_heap *heap);
 
+/**
+ * heap_cherrypick - remove a given element from the heap
+ * @heap: the heap to be operated on
+ * @p: the element
+ *
+ * Remove the given element from the heap. Return the element if present, else
+ * return NULL. This algorithm has a complexity of O(n), which is higher than
+ * O(log(n)) provided by the rest of this API.
+ */
+extern void *heap_cherrypick(struct ptr_heap *heap, void *p);
+
+/**
+ * heap_replace_max - replace the the largest element from the heap
+ * @heap: the heap to be operated on
+ * @p: the pointer to be inserted as topmost element replacement
+ *
+ * Returns the largest element in the heap. It removes this element from the
+ * heap. The heap is rebalanced only once after the insertion. Returns NULL if
+ * the heap is empty.
+ *
+ * This is the equivalent of calling heap_remove() and then heap_insert(), but
+ * it only rebalances the heap once.
+ */
+extern void *heap_replace_max(struct ptr_heap *heap, void *p);
 
 #endif /* _LINUX_PRIO_HEAP_H */
lib/prio_heap.c +78 -21

118 lines not on this page · raw message

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com