X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fhmap.c;h=97c6959de014497474e2fec3459ba421c4ed7567;hb=f40869bdf6feca4d3ff7c59a1fb1f7ac101bc967;hp=6bc5ea74bd79482897a353d5ad8361a7f360fe0c;hpb=f3099647623f2b13ece56cf8cf31761c00c1c297;p=sliver-openvswitch.git diff --git a/lib/hmap.c b/lib/hmap.c index 6bc5ea74b..97c6959de 100644 --- a/lib/hmap.c +++ b/lib/hmap.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2012 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,17 @@ #include #include "hmap.h" -#include #include #include #include "coverage.h" #include "random.h" #include "util.h" +COVERAGE_DEFINE(hmap_pathological); +COVERAGE_DEFINE(hmap_expand); +COVERAGE_DEFINE(hmap_shrink); +COVERAGE_DEFINE(hmap_reserve); + /* Initializes 'hmap' as an empty hash table. */ void hmap_init(struct hmap *hmap) @@ -86,8 +90,8 @@ resize(struct hmap *hmap, size_t new_mask) struct hmap tmp; size_t i; - assert(!(new_mask & (new_mask + 1))); - assert(new_mask != SIZE_MAX); + ovs_assert(!(new_mask & (new_mask + 1))); + ovs_assert(new_mask != SIZE_MAX); hmap_init(&tmp); if (new_mask) { @@ -213,3 +217,61 @@ hmap_random_node(const struct hmap *hmap) } return node; } + +/* Returns the next node in 'hmap' in hash order, or NULL if no nodes remain in + * 'hmap'. Uses '*bucketp' and '*offsetp' to determine where to begin + * iteration, and stores new values to pass on the next iteration into them + * before returning. + * + * It's better to use plain HMAP_FOR_EACH and related functions, since they are + * faster and better at dealing with hmaps that change during iteration. + * + * Before beginning iteration, store 0 into '*bucketp' and '*offsetp'. + */ +struct hmap_node * +hmap_at_position(const struct hmap *hmap, + uint32_t *bucketp, uint32_t *offsetp) +{ + size_t offset; + size_t b_idx; + + offset = *offsetp; + for (b_idx = *bucketp; b_idx <= hmap->mask; b_idx++) { + struct hmap_node *node; + size_t n_idx; + + for (n_idx = 0, node = hmap->buckets[b_idx]; node != NULL; + n_idx++, node = node->next) { + if (n_idx == offset) { + if (node->next) { + *bucketp = node->hash & hmap->mask; + *offsetp = offset + 1; + } else { + *bucketp = (node->hash & hmap->mask) + 1; + *offsetp = 0; + } + return node; + } + } + offset = 0; + } + + *bucketp = 0; + *offsetp = 0; + return NULL; +} + +/* Returns true if 'node' is in 'hmap', false otherwise. */ +bool +hmap_contains(const struct hmap *hmap, const struct hmap_node *node) +{ + struct hmap_node *p; + + for (p = hmap_first_in_bucket(hmap, node->hash); p; p = p->next) { + if (p == node) { + return true; + } + } + + return false; +}