2 * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Binary heap and hash tables, used in dummynet
30 * $Id: dn_heap.c 5646 2010-03-08 12:48:30Z luigi $
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
36 __FBSDID("$FreeBSD: user/luigi/ipfw3-head/sys/netinet/ipfw/dn_heap.c 203279 2010-01-31 12:20:29Z luigi $");
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <netinet/ipfw/dn_heap.h>
42 #define log(x, arg...)
53 #define log(x, arg...) fprintf(stderr, ## arg)
54 #define panic(x...) fprintf(stderr, ## x), exit(1)
55 #define MALLOC_DEFINE(a, b, c)
56 static void *my_malloc(int s) { return malloc(s); }
57 static void my_free(void *p) { free(p); }
58 #define malloc(s, t, w) my_malloc(s)
59 #define free(p, t) my_free(p)
62 MALLOC_DEFINE(M_DN_HEAP, "dummynet", "dummynet heap");
65 * Heap management functions.
67 * In the heap, first node is element 0. Children of i are 2i+1 and 2i+2.
68 * Some macros help finding parent/children so we can optimize them.
70 * heap_init() is called to expand the heap when needed.
71 * Increment size in blocks of 16 entries.
72 * Returns 1 on error, 0 on success
74 #define HEAP_FATHER(x) ( ( (x) - 1 ) / 2 )
75 #define HEAP_LEFT(x) ( (x)+(x) + 1 )
76 #define HEAP_SWAP(a, b, buffer) { buffer = a ; a = b ; b = buffer ; }
77 #define HEAP_INCREMENT 15
80 heap_resize(struct dn_heap *h, unsigned int new_size)
82 struct dn_heap_entry *p;
84 if (h->size >= new_size ) /* have enough room */
86 #if 1 /* round to the next power of 2 */
87 new_size |= new_size >> 1;
88 new_size |= new_size >> 2;
89 new_size |= new_size >> 4;
90 new_size |= new_size >> 8;
91 new_size |= new_size >> 16;
93 new_size = (new_size + HEAP_INCREMENT ) & ~HEAP_INCREMENT;
95 p = malloc(new_size * sizeof(*p), M_DN_HEAP, M_NOWAIT);
97 printf("--- %s, resize %d failed\n", __func__, new_size );
101 bcopy(h->p, p, h->size * sizeof(*p) );
102 free(h->p, M_DN_HEAP);
110 heap_init(struct dn_heap *h, int size, int ofs)
112 if (heap_resize(h, size))
120 * Insert element in heap. Normally, p != NULL, we insert p in
121 * a new position and bubble up. If p == NULL, then the element is
122 * already in place, and key is the position where to start the
124 * Returns 1 on failure (cannot allocate new heap entry)
126 * If ofs > 0 the position (index, int) of the element in the heap is
127 * also stored in the element itself at the given offset in bytes.
129 #define SET_OFFSET(h, i) do { \
131 *((int32_t *)((char *)(h->p[i].object) + h->ofs)) = i; \
134 * RESET_OFFSET is used for sanity checks. It sets ofs
135 * to an invalid value.
137 #define RESET_OFFSET(h, i) do { \
139 *((int32_t *)((char *)(h->p[i].object) + h->ofs)) = -16; \
143 heap_insert(struct dn_heap *h, uint64_t key1, void *p)
145 int son = h->elements;
147 //log("%s key %llu p %p\n", __FUNCTION__, key1, p);
148 if (p == NULL) { /* data already there, set starting point */
150 } else { /* insert new element at the end, possibly resize */
152 if (son == h->size) /* need resize... */
153 // XXX expand by 16 or so
154 if (heap_resize(h, h->elements+16) )
155 return 1; /* failure... */
156 h->p[son].object = p;
157 h->p[son].key = key1;
160 /* make sure that son >= father along the path */
162 int father = HEAP_FATHER(son);
163 struct dn_heap_entry tmp;
165 if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
166 break; /* found right position */
167 /* son smaller than father, swap and repeat */
168 HEAP_SWAP(h->p[son], h->p[father], tmp);
177 * remove top element from heap, or obj if obj != NULL
180 heap_extract(struct dn_heap *h, void *obj)
182 int child, father, max = h->elements - 1;
185 printf("--- %s: empty heap 0x%p\n", __FUNCTION__, h);
189 father = 0; /* default: move up smallest child */
190 else { /* extract specific element, index is at offset */
192 panic("%s: extract from middle not set on %p\n",
194 father = *((int *)((char *)obj + h->ofs));
195 if (father < 0 || father >= h->elements) {
196 panic("%s: father %d out of bound 0..%d\n",
197 __FUNCTION__, father, h->elements);
201 * below, father is the index of the empty element, which
202 * we replace at each step with the smallest child until we
203 * reach the bottom level.
205 // XXX why removing RESET_OFFSET increases runtime by 10% ?
206 RESET_OFFSET(h, father);
207 while ( (child = HEAP_LEFT(father)) <= max ) {
209 DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
210 child++; /* take right child, otherwise left */
211 h->p[father] = h->p[child];
212 SET_OFFSET(h, father);
218 * Fill hole with last entry and bubble up,
219 * reusing the insert code
221 h->p[father] = h->p[max];
222 heap_insert(h, father, NULL);
228 * change object position and update references
229 * XXX this one is never used!
232 heap_move(struct dn_heap *h, uint64_t new_key, void *object)
234 int temp, i, max = h->elements-1;
235 struct dn_heap_entry *p, buf;
238 panic("cannot move items on this heap");
239 p = h->p; /* shortcut */
241 i = *((int *)((char *)object + h->ofs));
242 if (DN_KEY_LT(new_key, p[i].key) ) { /* must move up */
245 DN_KEY_LT(new_key, p[(temp = HEAP_FATHER(i))].key);
246 i = temp ) { /* bubble up */
247 HEAP_SWAP(p[i], p[temp], buf);
250 } else { /* must move down */
252 while ( (temp = HEAP_LEFT(i)) <= max ) {
253 /* found left child */
255 DN_KEY_LT(p[temp+1].key, p[temp].key))
256 temp++; /* select child with min key */
257 if (DN_KEY_LT(>p[temp].key, new_key)) {
259 HEAP_SWAP(p[i], p[temp], buf);
268 #endif /* heap_move, unused */
271 * heapify() will reorganize data inside an array to maintain the
272 * heap property. It is needed when we delete a bunch of entries.
275 heapify(struct dn_heap *h)
279 for (i = 0; i < h->elements; i++ )
280 heap_insert(h, i , NULL);
284 heap_scan(struct dn_heap *h, int (*fn)(void *, uintptr_t),
289 for (i = found = 0 ; i < h->elements ;) {
290 ret = fn(h->p[i].object, arg);
291 if (ret & HEAP_SCAN_DEL) {
293 h->p[i] = h->p[h->elements] ;
297 if (ret & HEAP_SCAN_END)
306 * cleanup the heap and free data structure
309 heap_free(struct dn_heap *h)
312 free(h->p, M_DN_HEAP);
313 bzero(h, sizeof(*h) );
317 * hash table support.
321 int buckets; /* how many buckets, really buckets - 1*/
322 int entries; /* how many entries */
323 int ofs; /* offset of link field */
324 uint32_t (*hash)(uintptr_t, int, void *arg);
325 int (*match)(void *_el, uintptr_t key, int, void *);
326 void *(*newh)(uintptr_t, int, void *);
327 void **ht; /* bucket heads */
330 * Initialize, allocating bucket pointers inline.
331 * Recycle previous record if possible.
332 * If the 'newh' function is not supplied, we assume that the
333 * key passed to ht_find is the same object to be stored in.
336 dn_ht_init(struct dn_ht *ht, int buckets, int ofs,
337 uint32_t (*h)(uintptr_t, int, void *),
338 int (*match)(void *, uintptr_t, int, void *),
339 void *(*newh)(uintptr_t, int, void *))
344 * Notes about rounding bucket size to a power of two.
345 * Given the original bucket size, we compute the nearest lower and
346 * higher power of two, minus 1 (respectively b_min and b_max) because
347 * this value will be used to do an AND with the index returned
349 * To choice between these two values, the original bucket size is
350 * compared with b_min. If the original size is greater than 4/3 b_min,
351 * we round the bucket size to b_max, else to b_min.
352 * This ratio try to round to the nearest power of two, advantaging
353 * the greater size if the different between two power is relatively
355 * Rounding the bucket size to a power of two avoid the use of
356 * module when calculating the correct bucket.
357 * The ht->buckets variable store the bucket size - 1 to simply
358 * do an AND between the index returned by hash function and ht->bucket
359 * instead of a module.
361 int b_min; /* min buckets */
362 int b_max; /* max buckets */
363 int b_ori; /* original buckets */
365 if (h == NULL || match == NULL) {
366 printf("--- missing hash or match function");
369 if (buckets < 1 || buckets > 65536)
373 /* calculate next power of 2, - 1*/
374 buckets |= buckets >> 1;
375 buckets |= buckets >> 2;
376 buckets |= buckets >> 4;
377 buckets |= buckets >> 8;
378 buckets |= buckets >> 16;
380 b_max = buckets; /* Next power */
381 b_min = buckets >> 1; /* Previous power */
383 /* Calculate the 'nearest' bucket size */
384 if (b_min * 4000 / 3000 < b_ori)
389 if (ht) { /* see if we can reuse */
390 if (buckets <= ht->buckets) {
391 ht->buckets = buckets;
393 /* free pointers if not allocated inline */
394 if (ht->ht != (void *)(ht + 1))
395 free(ht->ht, M_DN_HEAP);
401 /* Allocate buckets + 1 entries because buckets is use to
402 * do the AND with the index returned by hash function
404 l = sizeof(*ht) + (buckets + 1) * sizeof(void **);
405 ht = malloc(l, M_DN_HEAP, M_NOWAIT | M_ZERO);
408 ht->ht = (void **)(ht + 1);
409 ht->buckets = buckets;
418 /* dummy callback for dn_ht_free to unlink all */
420 do_del(void *obj, void *arg)
422 return DNHT_SCAN_DEL;
426 dn_ht_free(struct dn_ht *ht, int flags)
430 if (flags & DNHT_REMOVE) {
431 (void)dn_ht_scan(ht, do_del, NULL);
433 if (ht->ht && ht->ht != (void *)(ht + 1))
434 free(ht->ht, M_DN_HEAP);
440 dn_ht_entries(struct dn_ht *ht)
442 return ht ? ht->entries : 0;
445 /* lookup and optionally create or delete element */
447 dn_ht_find(struct dn_ht *ht, uintptr_t key, int flags, void *arg)
452 if (ht == NULL) /* easy on an empty hash */
454 i = (ht->buckets == 1) ? 0 :
455 (ht->hash(key, flags, arg) & ht->buckets);
457 for (pp = &ht->ht[i]; (p = *pp); pp = (void **)((char *)p + ht->ofs)) {
458 if (flags & DNHT_MATCH_PTR) {
459 if (key == (uintptr_t)p)
461 } else if (ht->match(p, key, flags, arg)) /* found match */
465 if (flags & DNHT_REMOVE) {
466 /* link in the next element */
467 *pp = *(void **)((char *)p + ht->ofs);
468 *(void **)((char *)p + ht->ofs) = NULL;
471 } else if (flags & DNHT_INSERT) {
472 // printf("%s before calling new, bucket %d ofs %d\n",
473 // __FUNCTION__, i, ht->ofs);
474 p = ht->newh ? ht->newh(key, flags, arg) : (void *)key;
475 // printf("%s newh returns %p\n", __FUNCTION__, p);
478 *(void **)((char *)p + ht->ofs) = ht->ht[i];
486 * do a scan with the option to delete the object. Extract next before
487 * running the callback because the element may be destroyed there.
490 dn_ht_scan(struct dn_ht *ht, int (*fn)(void *, void *), void *arg)
492 int i, ret, found = 0;
493 void **curp, *cur, *next;
495 if (ht == NULL || fn == NULL)
497 for (i = 0; i <= ht->buckets; i++) {
499 while ( (cur = *curp) != NULL) {
500 next = *(void **)((char *)cur + ht->ofs);
502 if (ret & DNHT_SCAN_DEL) {
507 curp = (void **)((char *)cur + ht->ofs);
509 if (ret & DNHT_SCAN_END)
517 * Similar to dn_ht_scan(), except thah the scan is performed only
518 * in the bucket 'bucket'. The function returns a correct bucket number if
519 * the original is invalid
522 dn_ht_scan_bucket(struct dn_ht *ht, int *bucket, int (*fn)(void *, void *),
525 int i, ret, found = 0;
526 void **curp, *cur, *next;
528 if (ht == NULL || fn == NULL)
530 if (*bucket > ht->buckets)
535 while ( (cur = *curp) != NULL) {
536 next = *(void **)((char *)cur + ht->ofs);
538 if (ret & DNHT_SCAN_DEL) {
543 curp = (void **)((char *)cur + ht->ofs);
545 if (ret & DNHT_SCAN_END)