2014-11-10 13:47:48 +09:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Nicira, Inc.
|
|
|
|
* Copyright (c) 2014 Netronome.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "id-pool.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
#include "openvswitch/hmap.h"
|
2014-11-10 13:47:48 +09:00
|
|
|
#include "hash.h"
|
|
|
|
|
|
|
|
struct id_node {
|
|
|
|
struct hmap_node node;
|
|
|
|
uint32_t id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct id_pool {
|
|
|
|
struct hmap map;
|
|
|
|
uint32_t base; /* IDs in the range of [base, base + n_ids). */
|
|
|
|
uint32_t n_ids; /* Total number of ids in the pool. */
|
|
|
|
uint32_t next_free_id; /* Possible next free id. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void id_pool_init(struct id_pool *pool,
|
|
|
|
uint32_t base, uint32_t n_ids);
|
|
|
|
static void id_pool_uninit(struct id_pool *pool);
|
|
|
|
static struct id_node *id_pool_find(struct id_pool *pool, uint32_t id);
|
|
|
|
|
|
|
|
struct id_pool *
|
|
|
|
id_pool_create(uint32_t base, uint32_t n_ids)
|
|
|
|
{
|
|
|
|
struct id_pool *pool;
|
|
|
|
|
|
|
|
pool = xmalloc(sizeof *pool);
|
|
|
|
id_pool_init(pool, base, n_ids);
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
id_pool_destroy(struct id_pool *pool)
|
|
|
|
{
|
2014-11-11 12:39:19 +09:00
|
|
|
if (pool) {
|
|
|
|
id_pool_uninit(pool);
|
|
|
|
free(pool);
|
|
|
|
}
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
id_pool_init(struct id_pool *pool, uint32_t base, uint32_t n_ids)
|
|
|
|
{
|
|
|
|
pool->base = base;
|
|
|
|
pool->n_ids = n_ids;
|
|
|
|
pool->next_free_id = base;
|
|
|
|
hmap_init(&pool->map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
id_pool_uninit(struct id_pool *pool)
|
|
|
|
{
|
2016-04-06 18:53:59 -07:00
|
|
|
struct id_node *id_node;
|
2014-11-10 13:47:48 +09:00
|
|
|
|
2016-04-06 18:53:59 -07:00
|
|
|
HMAP_FOR_EACH_POP(id_node, node, &pool->map) {
|
2014-11-10 13:47:49 +09:00
|
|
|
free(id_node);
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
hmap_destroy(&pool->map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct id_node *
|
|
|
|
id_pool_find(struct id_pool *pool, uint32_t id)
|
|
|
|
{
|
|
|
|
size_t hash;
|
2014-11-10 13:47:49 +09:00
|
|
|
struct id_node *id_node;
|
2014-11-10 13:47:48 +09:00
|
|
|
|
|
|
|
hash = hash_int(id, 0);
|
2014-11-10 13:47:49 +09:00
|
|
|
HMAP_FOR_EACH_WITH_HASH(id_node, node, hash, &pool->map) {
|
|
|
|
if (id == id_node->id) {
|
|
|
|
return id_node;
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
id_pool_add(struct id_pool *pool, uint32_t id)
|
|
|
|
{
|
2014-11-10 13:47:49 +09:00
|
|
|
struct id_node *id_node = xmalloc(sizeof *id_node);
|
2014-11-10 13:47:48 +09:00
|
|
|
size_t hash;
|
|
|
|
|
2014-11-10 13:47:49 +09:00
|
|
|
id_node->id = id;
|
2014-11-10 13:47:48 +09:00
|
|
|
hash = hash_int(id, 0);
|
2014-11-10 13:47:49 +09:00
|
|
|
hmap_insert(&pool->map, &id_node->node, hash);
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
|
2014-11-10 13:47:49 +09:00
|
|
|
bool
|
|
|
|
id_pool_alloc_id(struct id_pool *pool, uint32_t *id_)
|
2014-11-10 13:47:48 +09:00
|
|
|
{
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
if (pool->n_ids == 0) {
|
2014-11-10 13:47:49 +09:00
|
|
|
return false;
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(id_pool_find(pool, pool->next_free_id))) {
|
|
|
|
id = pool->next_free_id;
|
|
|
|
goto found_free_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(id = pool->base; id < pool->base + pool->n_ids; id++) {
|
2014-11-10 13:47:49 +09:00
|
|
|
if (!id_pool_find(pool, id)) {
|
2014-11-10 13:47:48 +09:00
|
|
|
goto found_free_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not available. */
|
2014-11-10 13:47:49 +09:00
|
|
|
return false;
|
2014-11-10 13:47:48 +09:00
|
|
|
|
|
|
|
found_free_id:
|
|
|
|
id_pool_add(pool, id);
|
|
|
|
|
2017-03-12 11:57:37 +08:00
|
|
|
if (id + 1 < pool->base + pool->n_ids) {
|
2014-11-10 13:47:48 +09:00
|
|
|
pool->next_free_id = id + 1;
|
|
|
|
} else {
|
|
|
|
pool->next_free_id = pool->base;
|
|
|
|
}
|
|
|
|
|
2014-11-10 13:47:49 +09:00
|
|
|
*id_ = id;
|
|
|
|
return true;
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
id_pool_free_id(struct id_pool *pool, uint32_t id)
|
|
|
|
{
|
2014-11-10 13:47:49 +09:00
|
|
|
struct id_node *id_node;
|
2017-03-12 11:57:37 +08:00
|
|
|
if (id >= pool->base && (id < pool->base + pool->n_ids)) {
|
2014-11-10 13:47:49 +09:00
|
|
|
id_node = id_pool_find(pool, id);
|
|
|
|
if (id_node) {
|
|
|
|
hmap_remove(&pool->map, &id_node->node);
|
2017-02-21 17:49:25 +03:00
|
|
|
if (id < pool->next_free_id) {
|
|
|
|
pool->next_free_id = id;
|
|
|
|
}
|
2014-11-10 08:43:31 -08:00
|
|
|
free(id_node);
|
2014-11-10 13:47:48 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|