4 * Priority heap containing pointers. Based on CLRS, chapter 6.
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <linux/slab.h>
20 #include "prio_heap.h"
23 void check_heap(const struct ptr_heap
*heap
)
30 for (i
= 1; i
< heap
->len
; i
++)
31 WARN_ON_ONCE(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
36 size_t parent(size_t i
)
48 size_t right(size_t i
)
54 * Copy of heap->ptrs pointer is invalid after heap_grow.
57 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
61 if (heap
->alloc_len
>= new_len
)
64 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
65 new_ptrs
= kmalloc(heap
->alloc_len
* sizeof(void *), heap
->gfpmask
);
69 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
71 heap
->ptrs
= new_ptrs
;
76 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
80 ret
= heap_grow(heap
, new_len
);
87 int heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
88 gfp_t gfpmask
, int gt(void *a
, void *b
))
95 * Minimum size allocated is 1 entry to ensure memory allocation
96 * never fails within heap_replace_max.
98 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
101 void heap_free(struct ptr_heap
*heap
)
106 static void heapify(struct ptr_heap
*heap
, size_t i
)
108 void **ptrs
= heap
->ptrs
;
109 size_t l
, r
, largest
;
116 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
120 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
125 ptrs
[i
] = ptrs
[largest
];
132 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
137 (void) heap_set_len(heap
, 1);
143 /* Replace the current max and heapify */
150 int heap_insert(struct ptr_heap
*heap
, void *p
)
156 ret
= heap_set_len(heap
, heap
->len
+ 1);
161 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
162 /* Move parent down until we find the right spot */
163 ptrs
[pos
] = ptrs
[parent(pos
)];
171 void *heap_remove(struct ptr_heap
*heap
)
177 (void) heap_set_len(heap
, 0);
178 return heap
->ptrs
[0];
180 /* Shrink, replace the current max by previous last entry and heapify */
181 heap_set_len(heap
, heap
->len
- 1);
182 /* len changed. previous last entry is at heap->len */
183 return heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
186 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
188 size_t pos
, len
= heap
->len
;
190 for (pos
= 0; pos
< len
; pos
++)
191 if (heap
->ptrs
[pos
] == p
)
195 if (heap
->len
== 1) {
196 (void) heap_set_len(heap
, 0);
198 return heap
->ptrs
[0];
200 /* Replace p with previous last entry and heapify. */
201 heap_set_len(heap
, heap
->len
- 1);
202 /* len changed. previous last entry is at heap->len */
203 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.045792 seconds and 5 git commands to generate.