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 size_t parent(size_t i
)
35 size_t right(size_t i
)
41 * Copy of heap->ptrs pointer is invalid after heap_grow.
44 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
48 if (heap
->alloc_len
>= new_len
)
51 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
52 new_ptrs
= kmalloc(heap
->alloc_len
* sizeof(void *), heap
->gfpmask
);
56 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
58 heap
->ptrs
= new_ptrs
;
63 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
67 ret
= heap_grow(heap
, new_len
);
74 int heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
75 gfp_t gfpmask
, int gt(void *a
, void *b
))
82 * Minimum size allocated is 1 entry to ensure memory allocation
83 * never fails within heap_replace_max.
85 return heap_grow(heap
, max_t(size_t, 1, alloc_len
));
88 void heap_free(struct ptr_heap
*heap
)
93 static void heapify(struct ptr_heap
*heap
, size_t i
)
95 void **ptrs
= heap
->ptrs
;
101 if (l
<= heap
->len
&& ptrs
[l
] > ptrs
[i
])
105 if (r
<= heap
->len
&& ptrs
[r
] > ptrs
[largest
])
111 ptrs
[i
] = ptrs
[largest
];
121 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
126 (void) heap_set_len(heap
, 1);
131 /* Replace the current max and heapify */
138 int heap_insert(struct ptr_heap
*heap
, void *p
)
144 ret
= heap_set_len(heap
, heap
->len
+ 1);
148 /* Add the element to the end */
149 ptrs
[heap
->len
- 1] = p
;
151 /* Bubble it up to the appropriate position. */
153 if (pos
> 0 && heap
->gt(ptrs
[pos
], ptrs
[parent(pos
)])) {
156 /* Need to exchange */
158 ptrs
[pos
] = ptrs
[parent(pos
)];
159 ptrs
[parent(pos
)] = tmp
;
170 void *heap_remove(struct ptr_heap
*heap
)
176 (void) heap_set_len(heap
, 0);
177 return heap
->ptrs
[0];
179 /* Shrink, replace the current max by previous last entry and heapify */
180 heap_set_len(heap
, heap
->len
- 1);
181 return heap_replace_max(heap
, heap
->ptrs
[heap
->len
- 1]);
184 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
186 size_t pos
, len
= heap
->len
;
188 for (pos
= 0; pos
< len
; pos
++)
189 if (heap
->ptrs
[pos
] == p
)
193 if (heap
->len
== 1) {
194 (void) heap_set_len(heap
, 0);
195 return heap
->ptrs
[0];
197 /* Replace p with previous last entry and heapify. */
198 heap_set_len(heap
, heap
->len
- 1);
199 heap
->ptrs
[pos
] = heap
->ptrs
[heap
->len
- 1];
This page took 0.035037 seconds and 5 git commands to generate.