04438bbfec52951c8b7a2b62f9ef56a24583e02d
1 /* SPDX-License-Identifier: MIT
5 * Priority heap containing pointers. Based on CLRS, chapter 6.
7 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/slab.h>
11 #include <lib/prio_heap/lttng_prio_heap.h>
12 #include <wrapper/vmalloc.h>
15 void lttng_check_heap(const struct lttng_ptr_heap
*heap
)
22 for (i
= 1; i
< heap
->len
; i
++)
23 WARN_ON_ONCE(!heap
->gt(heap
->ptrs
[i
], heap
->ptrs
[0]));
28 size_t parent(size_t i
)
40 size_t right(size_t i
)
46 * Copy of heap->ptrs pointer is invalid after heap_grow.
49 int heap_grow(struct lttng_ptr_heap
*heap
, size_t new_len
)
53 if (heap
->alloc_len
>= new_len
)
56 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
57 new_ptrs
= lttng_kvmalloc(heap
->alloc_len
* sizeof(void *), heap
->gfpmask
);
61 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
62 lttng_kvfree(heap
->ptrs
);
63 heap
->ptrs
= new_ptrs
;
68 int heap_set_len(struct lttng_ptr_heap
*heap
, size_t new_len
)
72 ret
= heap_grow(heap
, new_len
);
79 int lttng_heap_init(struct lttng_ptr_heap
*heap
, size_t alloc_len
,
80 gfp_t gfpmask
, int gt(void *a
, void *b
))
86 heap
->gfpmask
= gfpmask
;
88 * Minimum size allocated is 1 entry to ensure memory allocation
89 * never fails within heap_replace_max.
91 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
94 void lttng_heap_free(struct lttng_ptr_heap
*heap
)
96 lttng_kvfree(heap
->ptrs
);
99 static void heapify(struct lttng_ptr_heap
*heap
, size_t i
)
101 void **ptrs
= heap
->ptrs
;
102 size_t l
, r
, largest
;
109 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
113 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
118 ptrs
[i
] = ptrs
[largest
];
122 lttng_check_heap(heap
);
125 void *lttng_heap_replace_max(struct lttng_ptr_heap
*heap
, void *p
)
130 (void) heap_set_len(heap
, 1);
132 lttng_check_heap(heap
);
136 /* Replace the current max and heapify */
143 int lttng_heap_insert(struct lttng_ptr_heap
*heap
, void *p
)
149 ret
= heap_set_len(heap
, heap
->len
+ 1);
154 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
155 /* Move parent down until we find the right spot */
156 ptrs
[pos
] = ptrs
[parent(pos
)];
160 lttng_check_heap(heap
);
164 void *lttng_heap_remove(struct lttng_ptr_heap
*heap
)
170 (void) heap_set_len(heap
, 0);
171 return heap
->ptrs
[0];
173 /* Shrink, replace the current max by previous last entry and heapify */
174 heap_set_len(heap
, heap
->len
- 1);
175 /* len changed. previous last entry is at heap->len */
176 return lttng_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
179 void *lttng_heap_cherrypick(struct lttng_ptr_heap
*heap
, void *p
)
181 size_t pos
, len
= heap
->len
;
183 for (pos
= 0; pos
< len
; pos
++)
184 if (heap
->ptrs
[pos
] == p
)
188 if (heap
->len
== 1) {
189 (void) heap_set_len(heap
, 0);
190 lttng_check_heap(heap
);
191 return heap
->ptrs
[0];
193 /* Replace p with previous last entry and heapify. */
194 heap_set_len(heap
, heap
->len
- 1);
195 /* len changed. previous last entry is at heap->len */
196 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.059384 seconds and 3 git commands to generate.