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 "lttng_prio_heap.h"
31 void lttng_check_heap(const struct lttng_ptr_heap
*heap
)
38 for (i
= 1; i
< heap
->len
; i
++)
39 WARN_ON_ONCE(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
44 size_t parent(size_t i
)
56 size_t right(size_t i
)
62 * Copy of heap->ptrs pointer is invalid after heap_grow.
65 int heap_grow(struct lttng_ptr_heap
*heap
, size_t new_len
)
69 if (heap
->alloc_len
>= new_len
)
72 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
73 new_ptrs
= kmalloc(heap
->alloc_len
* sizeof(void *), heap
->gfpmask
);
77 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
79 heap
->ptrs
= new_ptrs
;
84 int heap_set_len(struct lttng_ptr_heap
*heap
, size_t new_len
)
88 ret
= heap_grow(heap
, new_len
);
95 int lttng_heap_init(struct lttng_ptr_heap
*heap
, size_t alloc_len
,
96 gfp_t gfpmask
, int gt(void *a
, void *b
))
102 heap
->gfpmask
= gfpmask
;
104 * Minimum size allocated is 1 entry to ensure memory allocation
105 * never fails within heap_replace_max.
107 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
110 void lttng_heap_free(struct lttng_ptr_heap
*heap
)
115 static void heapify(struct lttng_ptr_heap
*heap
, size_t i
)
117 void **ptrs
= heap
->ptrs
;
118 size_t l
, r
, largest
;
125 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
129 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
134 ptrs
[i
] = ptrs
[largest
];
138 lttng_check_heap(heap
);
141 void *lttng_heap_replace_max(struct lttng_ptr_heap
*heap
, void *p
)
146 (void) heap_set_len(heap
, 1);
148 lttng_check_heap(heap
);
152 /* Replace the current max and heapify */
159 int lttng_heap_insert(struct lttng_ptr_heap
*heap
, void *p
)
165 ret
= heap_set_len(heap
, heap
->len
+ 1);
170 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
171 /* Move parent down until we find the right spot */
172 ptrs
[pos
] = ptrs
[parent(pos
)];
176 lttng_check_heap(heap
);
180 void *lttng_heap_remove(struct lttng_ptr_heap
*heap
)
186 (void) heap_set_len(heap
, 0);
187 return heap
->ptrs
[0];
189 /* Shrink, replace the current max by previous last entry and heapify */
190 heap_set_len(heap
, heap
->len
- 1);
191 /* len changed. previous last entry is at heap->len */
192 return lttng_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
195 void *lttng_heap_cherrypick(struct lttng_ptr_heap
*heap
, void *p
)
197 size_t pos
, len
= heap
->len
;
199 for (pos
= 0; pos
< len
; pos
++)
200 if (heap
->ptrs
[pos
] == p
)
204 if (heap
->len
== 1) {
205 (void) heap_set_len(heap
, 0);
206 lttng_check_heap(heap
);
207 return heap
->ptrs
[0];
209 /* Replace p with previous last entry and heapify. */
210 heap_set_len(heap
, heap
->len
- 1);
211 /* len changed. previous last entry is at heap->len */
212 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.032837 seconds and 4 git commands to generate.