Skip to content

Commit

Permalink
id-pool: Refactor id_pool_alloc_id to allow any 32 bit value to be an id
Browse files Browse the repository at this point in the history
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.horman@netronome.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
  • Loading branch information
shorman-netronome authored and blp committed Nov 10, 2014
1 parent c3bd4bf commit 27c2474
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
43 changes: 22 additions & 21 deletions lib/id-pool.c
Expand Up @@ -67,11 +67,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);
Expand All @@ -81,12 +81,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;
Expand All @@ -95,21 +95,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))) {
Expand All @@ -118,13 +118,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);
Expand All @@ -135,17 +135,18 @@ id_pool_alloc_id(struct id_pool *pool)
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_node) {
hmap_remove(&pool->map, &id_node->node);
}
}
}
3 changes: 2 additions & 1 deletion lib/id-pool.h
Expand Up @@ -18,14 +18,15 @@
#ifndef ID_POOL_H
#define ID_POOL_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

struct id_pool;

struct id_pool *id_pool_create(uint32_t base, uint32_t n_ids);
void id_pool_destroy(struct id_pool *);
uint32_t id_pool_alloc_id(struct id_pool *);
bool id_pool_alloc_id(struct id_pool *, uint32_t *id);
void id_pool_free_id(struct id_pool *, uint32_t id);
void id_pool_add(struct id_pool *, uint32_t id);

Expand Down
7 changes: 6 additions & 1 deletion ofproto/ofproto-dpif-rid.c
Expand Up @@ -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;
}

Expand Down

0 comments on commit 27c2474

Please sign in to comment.