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.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include <linux/slab.h>
28 #include <lib/prio_heap/lttng_prio_heap.h>
29 #include <wrapper/vmalloc.h>
32 void lttng_check_heap(const struct lttng_ptr_heap
*heap
)
39 for (i
= 1; i
< heap
->len
; i
++)
40 WARN_ON_ONCE(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
45 size_t parent(size_t i
)
57 size_t right(size_t i
)
63 * Copy of heap->ptrs pointer is invalid after heap_grow.
66 int heap_grow(struct lttng_ptr_heap
*heap
, size_t new_len
)
70 if (heap
->alloc_len
>= new_len
)
73 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
74 new_ptrs
= lttng_kvmalloc(heap
->alloc_len
* sizeof(void *), heap
->gfpmask
);
78 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
79 lttng_kvfree(heap
->ptrs
);
80 heap
->ptrs
= new_ptrs
;
85 int heap_set_len(struct lttng_ptr_heap
*heap
, size_t new_len
)
89 ret
= heap_grow(heap
, new_len
);
96 int lttng_heap_init(struct lttng_ptr_heap
*heap
, size_t alloc_len
,
97 gfp_t gfpmask
, int gt(void *a
, void *b
))
103 heap
->gfpmask
= gfpmask
;
105 * Minimum size allocated is 1 entry to ensure memory allocation
106 * never fails within heap_replace_max.
108 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
111 void lttng_heap_free(struct lttng_ptr_heap
*heap
)
113 lttng_kvfree(heap
->ptrs
);
116 static void heapify(struct lttng_ptr_heap
*heap
, size_t i
)
118 void **ptrs
= heap
->ptrs
;
119 size_t l
, r
, largest
;
126 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
130 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
135 ptrs
[i
] = ptrs
[largest
];
139 lttng_check_heap(heap
);
142 void *lttng_heap_replace_max(struct lttng_ptr_heap
*heap
, void *p
)
147 (void) heap_set_len(heap
, 1);
149 lttng_check_heap(heap
);
153 /* Replace the current max and heapify */
160 int lttng_heap_insert(struct lttng_ptr_heap
*heap
, void *p
)
166 ret
= heap_set_len(heap
, heap
->len
+ 1);
171 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
172 /* Move parent down until we find the right spot */
173 ptrs
[pos
] = ptrs
[parent(pos
)];
177 lttng_check_heap(heap
);
181 void *lttng_heap_remove(struct lttng_ptr_heap
*heap
)
187 (void) heap_set_len(heap
, 0);
188 return heap
->ptrs
[0];
190 /* Shrink, replace the current max by previous last entry and heapify */
191 heap_set_len(heap
, heap
->len
- 1);
192 /* len changed. previous last entry is at heap->len */
193 return lttng_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
196 void *lttng_heap_cherrypick(struct lttng_ptr_heap
*heap
, void *p
)
198 size_t pos
, len
= heap
->len
;
200 for (pos
= 0; pos
< len
; pos
++)
201 if (heap
->ptrs
[pos
] == p
)
205 if (heap
->len
== 1) {
206 (void) heap_set_len(heap
, 0);
207 lttng_check_heap(heap
);
208 return heap
->ptrs
[0];
210 /* Replace p with previous last entry and heapify. */
211 heap_set_len(heap
, heap
->len
- 1);
212 /* len changed. previous last entry is at heap->len */
213 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.034661 seconds and 5 git commands to generate.