prio_heap.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef _LINUX_PRIO_HEAP_H
  2. #define _LINUX_PRIO_HEAP_H
  3. /*
  4. * Simple insertion-only static-sized priority heap containing
  5. * pointers, based on CLR, chapter 7
  6. */
  7. #include <linux/gfp.h>
  8. /**
  9. * struct ptr_heap - simple static-sized priority heap
  10. * @ptrs - pointer to data area
  11. * @max - max number of elements that can be stored in @ptrs
  12. * @size - current number of valid elements in @ptrs (in the range 0..@size-1
  13. * @gt: comparison operator, which should implement "greater than"
  14. */
  15. struct ptr_heap {
  16. void **ptrs;
  17. int max;
  18. int size;
  19. int (*gt)(void *, void *);
  20. };
  21. /**
  22. * heap_init - initialize an empty heap with a given memory size
  23. * @heap: the heap structure to be initialized
  24. * @size: amount of memory to use in bytes
  25. * @gfp_mask: mask to pass to kmalloc()
  26. * @gt: comparison operator, which should implement "greater than"
  27. */
  28. extern int heap_init(struct ptr_heap *heap, size_t size, gfp_t gfp_mask,
  29. int (*gt)(void *, void *));
  30. /**
  31. * heap_free - release a heap's storage
  32. * @heap: the heap structure whose data should be released
  33. */
  34. void heap_free(struct ptr_heap *heap);
  35. /**
  36. * heap_insert - insert a value into the heap and return any overflowed value
  37. * @heap: the heap to be operated on
  38. * @p: the pointer to be inserted
  39. *
  40. * Attempts to insert the given value into the priority heap. If the
  41. * heap is full prior to the insertion, then the resulting heap will
  42. * consist of the smallest @max elements of the original heap and the
  43. * new element; the greatest element will be removed from the heap and
  44. * returned. Note that the returned element will be the new element
  45. * (i.e. no change to the heap) if the new element is greater than all
  46. * elements currently in the heap.
  47. */
  48. extern void *heap_insert(struct ptr_heap *heap, void *p);
  49. #endif /* _LINUX_PRIO_HEAP_H */