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 "lttng_prio_heap.h"
23 void lttng_check_heap(const struct lttng_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 lttng_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 lttng_ptr_heap
*heap
, size_t new_len
)
80 ret
= heap_grow(heap
, new_len
);
87 int lttng_heap_init(struct lttng_ptr_heap
*heap
, size_t alloc_len
,
88 gfp_t gfpmask
, int gt(void *a
, void *b
))
94 heap
->gfpmask
= gfpmask
;
96 * Minimum size allocated is 1 entry to ensure memory allocation
97 * never fails within heap_replace_max.
99 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
102 void lttng_heap_free(struct lttng_ptr_heap
*heap
)
107 static void heapify(struct lttng_ptr_heap
*heap
, size_t i
)
109 void **ptrs
= heap
->ptrs
;
110 size_t l
, r
, largest
;
117 if (l
< heap
->len
&& heap
->gt(ptrs
[l
], ptrs
[i
]))
121 if (r
< heap
->len
&& heap
->gt(ptrs
[r
], ptrs
[largest
]))
126 ptrs
[i
] = ptrs
[largest
];
130 lttng_check_heap(heap
);
133 void *lttng_heap_replace_max(struct lttng_ptr_heap
*heap
, void *p
)
138 (void) heap_set_len(heap
, 1);
140 lttng_check_heap(heap
);
144 /* Replace the current max and heapify */
151 int lttng_heap_insert(struct lttng_ptr_heap
*heap
, void *p
)
157 ret
= heap_set_len(heap
, heap
->len
+ 1);
162 while (pos
> 0 && heap
->gt(p
, ptrs
[parent(pos
)])) {
163 /* Move parent down until we find the right spot */
164 ptrs
[pos
] = ptrs
[parent(pos
)];
168 lttng_check_heap(heap
);
172 void *lttng_heap_remove(struct lttng_ptr_heap
*heap
)
178 (void) heap_set_len(heap
, 0);
179 return heap
->ptrs
[0];
181 /* Shrink, replace the current max by previous last entry and heapify */
182 heap_set_len(heap
, heap
->len
- 1);
183 /* len changed. previous last entry is at heap->len */
184 return lttng_heap_replace_max(heap
, heap
->ptrs
[heap
->len
]);
187 void *lttng_heap_cherrypick(struct lttng_ptr_heap
*heap
, void *p
)
189 size_t pos
, len
= heap
->len
;
191 for (pos
= 0; pos
< len
; pos
++)
192 if (heap
->ptrs
[pos
] == p
)
196 if (heap
->len
== 1) {
197 (void) heap_set_len(heap
, 0);
198 lttng_check_heap(heap
);
199 return heap
->ptrs
[0];
201 /* Replace p with previous last entry and heapify. */
202 heap_set_len(heap
, heap
->len
- 1);
203 /* len changed. previous last entry is at heap->len */
204 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
];
This page took 0.033268 seconds and 4 git commands to generate.