id_pool_alloc_id() was created by breaking out the recirculation allocation code. As it is now a library call it makes sense to remove the restriction that id 0 is reserved.
Signed-off-by: Simon Horman <simon.hor...@netronome.com> --- lib/id-pool.c | 43 ++++++++++++++++++++++--------------------- lib/id-pool.h | 3 ++- ofproto/ofproto-dpif-rid.c | 7 ++++++- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/id-pool.c b/lib/id-pool.c index e6d7827..1ee3256 100644 --- a/lib/id-pool.c +++ b/lib/id-pool.c @@ -68,11 +68,11 @@ id_pool_init(struct id_pool *pool, uint32_t base, uint32_t n_ids) static void id_pool_uninit(struct id_pool *pool) { - struct id_node *rid, *next; + struct id_node *id_node, *next; - HMAP_FOR_EACH_SAFE(rid, next, node, &pool->map) { - hmap_remove(&pool->map, &rid->node); - free(rid); + HMAP_FOR_EACH_SAFE(id_node, next, node, &pool->map) { + hmap_remove(&pool->map, &id_node->node); + free(id_node); } hmap_destroy(&pool->map); @@ -82,12 +82,12 @@ static struct id_node * id_pool_find(struct id_pool *pool, uint32_t id) { size_t hash; - struct id_node *rid; + struct id_node *id_node; hash = hash_int(id, 0); - HMAP_FOR_EACH_WITH_HASH(rid, node, hash, &pool->map) { - if (id == rid->id) { - return rid; + HMAP_FOR_EACH_WITH_HASH(id_node, node, hash, &pool->map) { + if (id == id_node->id) { + return id_node; } } return NULL; @@ -96,21 +96,21 @@ id_pool_find(struct id_pool *pool, uint32_t id) void id_pool_add(struct id_pool *pool, uint32_t id) { - struct id_node *rid = xmalloc(sizeof *rid); + struct id_node *id_node = xmalloc(sizeof *id_node); size_t hash; - rid->id = id; + id_node->id = id; hash = hash_int(id, 0); - hmap_insert(&pool->map, &rid->node, hash); + hmap_insert(&pool->map, &id_node->node, hash); } -uint32_t -id_pool_alloc_id(struct id_pool *pool) +bool +id_pool_alloc_id(struct id_pool *pool, uint32_t *id_) { uint32_t id; if (pool->n_ids == 0) { - return 0; + return false; } if (!(id_pool_find(pool, pool->next_free_id))) { @@ -119,13 +119,13 @@ id_pool_alloc_id(struct id_pool *pool) } for(id = pool->base; id < pool->base + pool->n_ids; id++) { - if (id_pool_find(pool, id)) { + if (!id_pool_find(pool, id)) { goto found_free_id; } } /* Not available. */ - return 0; + return false; found_free_id: id_pool_add(pool, id); @@ -136,17 +136,18 @@ found_free_id: pool->next_free_id = pool->base; } - return id; + *id_ = id; + return true; } void id_pool_free_id(struct id_pool *pool, uint32_t id) { - struct id_node *rid; + struct id_node *id_node; if (id > pool->base && (id <= pool->base + pool->n_ids)) { - rid = id_pool_find(pool, id); - if (rid) { - hmap_remove(&pool->map, &rid->node); + id_node = id_pool_find(pool, id); + if (id) { + hmap_remove(&pool->map, &id_node->node); } } } diff --git a/lib/id-pool.h b/lib/id-pool.h index 71784ba..14a089e 100644 --- a/lib/id-pool.h +++ b/lib/id-pool.h @@ -20,12 +20,13 @@ #include <stddef.h> #include <stdint.h> +#include <stdbool.h> struct id_pool; struct id_pool *id_pool_create(uint32_t base, uint32_t n_ids); void id_pool_destroy(struct id_pool *pool); -uint32_t id_pool_alloc_id(struct id_pool *pool); +bool id_pool_alloc_id(struct id_pool *pool, uint32_t *id); void id_pool_free_id(struct id_pool *pool, uint32_t id); void id_pool_add(struct id_pool *pool, uint32_t id); diff --git a/ofproto/ofproto-dpif-rid.c b/ofproto/ofproto-dpif-rid.c index 55d5c2b..afad3ce 100644 --- a/ofproto/ofproto-dpif-rid.c +++ b/ofproto/ofproto-dpif-rid.c @@ -52,11 +52,16 @@ uint32_t recirc_id_alloc(struct recirc_id_pool *pool) { uint32_t id; + bool ret; ovs_mutex_lock(&pool->lock); - id = id_pool_alloc_id(pool->rids); + ret = id_pool_alloc_id(pool->rids, &id); ovs_mutex_unlock(&pool->lock); + if (!ret) { + return 0; + } + return id; } -- 2.1.1 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev