X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fprio_heap%2Fprio_heap.h;h=2b55adf2e68401ab7a66627fe35e8c4fa85a3104;hb=0b6d2ab30831e9385a4796d45012ebb749a63cae;hp=12b1638f9d46592584fb1f7a62829604fd94b1ca;hpb=162769efd7bca728713c4bf8e609fc408d58f512;p=lttng-modules.git diff --git a/lib/prio_heap/prio_heap.h b/lib/prio_heap/prio_heap.h index 12b1638f..2b55adf2 100644 --- a/lib/prio_heap/prio_heap.h +++ b/lib/prio_heap/prio_heap.h @@ -4,8 +4,7 @@ /* * prio_heap.h * - * Static-sized priority heap containing pointers. Based on CLRS, - * chapter 6. + * Priority heap containing pointers. Based on CLRS, chapter 6. * * Copyright 2011 - Mathieu Desnoyers * @@ -20,10 +19,23 @@ * all copies or substantial portions of the Software. */ -/* - * TODO: - * implement struct ptr_head, heap_init, heap_free, heap_insert. - */ +#include + +struct ptr_heap { + size_t len, alloc_len; + void **ptrs; + int (*gt)(void *a, void *b); + gfp_t gfpmask; +}; + +#ifdef DEBUG_HEAP +void check_heap(const struct ptr_heap *heap); +#else +static inline +void check_heap(const struct ptr_heap *heap) +{ +} +#endif /** * heap_maximum - return the largest element in the heap @@ -34,9 +46,39 @@ */ static inline void *heap_maximum(const struct ptr_heap *heap) { - return heap->size ? heap->ptrs[0] : NULL; + check_heap(heap); + return heap->len ? heap->ptrs[0] : NULL; } +/** + * heap_init - initialize the heap + * @heap: the heap to initialize + * @alloc_len: number of elements initially allocated + * @gfp: allocation flags + * @gt: function to compare the elements + * + * Returns -ENOMEM if out of memory. + */ +extern int heap_init(struct ptr_heap *heap, + size_t alloc_len, gfp_t gfpmask, + int gt(void *a, void *b)); + +/** + * heap_free - free the heap + * @heap: the heap to free + */ +extern void heap_free(struct ptr_heap *heap); + +/** + * heap_insert - insert an element into the heap + * @heap: the heap to be operated on + * @p: the element to add + * + * Insert an element into the heap. + * + * Returns -ENOMEM if out of memory. + */ +extern int heap_insert(struct ptr_heap *heap, void *p); /** * heap_remove - remove the largest element from the heap @@ -68,7 +110,7 @@ extern void *heap_cherrypick(struct ptr_heap *heap, void *p); * the heap is empty. * * This is the equivalent of calling heap_remove() and then heap_insert(), but - * it only rebalances the heap once. + * it only rebalances the heap once. It never allocates memory. */ extern void *heap_replace_max(struct ptr_heap *heap, void *p);