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 int heap_grow(struct ptr_heap
*heap
, size_t new_len
)
45 if (heap
->alloc_len
>= new_len
)
48 heap
->alloc_len
= max_t(size_t, new_len
, heap
->alloc_len
<< 1);
49 new_ptrs
= kmalloc(heap
->alloc_len
* sizeof(void *), heap
->gfpmask
);
53 memcpy(new_ptrs
, heap
->ptrs
, heap
->len
* sizeof(void *));
55 heap
->ptrs
= new_ptrs
;
60 int heap_set_len(struct ptr_heap
*heap
, size_t new_len
)
64 ret
= heap_grow(heap
, new_len
);
71 int heap_init(struct ptr_heap
*heap
, size_t alloc_len
,
72 gfp_t gfpmask
, int gt(void *a
, void *b
))
79 * Minimum size allocated is 1 entry to ensure memory allocation
80 * never fails within heap_replace_max.
82 return heap_grow(heap
, min_t(size_t, 1, alloc_len
));
85 void heap_free(struct ptr_heap
*heap
)
90 static void heapify(struct ptr_heap
*heap
, size_t i
)
92 void **ptrs
= heap
->ptrs
;
98 if (l
<= heap
->len
&& ptrs
[l
] > ptrs
[i
])
102 if (r
<= heap
->len
&& ptrs
[r
] > ptrs
[largest
])
108 ptrs
[i
] = ptrs
[largest
];
118 void *heap_replace_max(struct ptr_heap
*heap
, void *p
)
121 void **ptrs
= heap
->ptrs
;
124 (void) heap_set_len(heap
, 1);
129 /* Replace the current max and heapify */
136 int heap_insert(struct ptr_heap
*heap
, void *p
)
138 void **ptrs
= heap
->ptrs
;
141 ret
= heap_set_len(heap
, heap
->len
+ 1);
144 /* Add the element to the end */
145 ptrs
[heap
->len
- 1] = p
;
151 void *heap_remove(struct ptr_heap
*heap
)
153 void **ptrs
= heap
->ptrs
;
159 (void) heap_set_len(heap
, 0);
162 /* Shrink, replace the current max by previous last entry and heapify */
163 heap_set_len(heap
, heap
->len
- 1);
164 return heap_replace_max(heap
, ptrs
[heap
->len
- 1]);
167 void *heap_cherrypick(struct ptr_heap
*heap
, void *p
)
169 void **ptrs
= heap
->ptrs
;
170 size_t pos
, len
= heap
->len
;
172 for (pos
= 0; pos
< len
; pos
++)
177 if (heap
->len
== 1) {
178 (void) heap_set_len(heap
, 0);
181 /* Replace p with previous last entry and heapify. */
182 heap_set_len(heap
, heap
->len
- 1);
183 ptrs
[pos
] = ptrs
[heap
->len
- 1];
This page took 0.044484 seconds and 6 git commands to generate.