0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-09-19 11:55:21 +00:00

ID Map: Introducing a maximum limit for the ID

This commit is contained in:
Maria Matejka 2019-04-09 13:38:39 +02:00
parent 4e27fb34a1
commit 0972274dc3
7 changed files with 29 additions and 23 deletions

View File

@ -196,7 +196,7 @@ ca_lookup(pool *p, const char *name, int f_type)
static int inited = 0;
if (!inited) {
idm_init(&ca_idm, &root_pool, 8);
idm_init(&ca_idm, &root_pool, 8, EA_CUSTOM_BIT);
HASH_INIT(ca_hash, &root_pool, CA_ORDER);
ca_storage_max = 256;
@ -212,7 +212,7 @@ ca_lookup(pool *p, const char *name, int f_type)
uint id = idm_alloc(&ca_idm);
if (id >= EA_CUSTOM_BIT)
if (!id)
cf_error("Too many custom attributes.");
if (id >= ca_storage_max) {

View File

@ -16,11 +16,12 @@
void
idm_init(struct idm *m, pool *p, uint size)
idm_init(struct idm *m, pool *p, u64 size, u64 max)
{
m->pos = 0;
m->used = 1;
m->size = size;
m->max = max;
m->data = mb_allocz(p, m->size * sizeof(u32));
/* ID 0 is reserved */
@ -29,17 +30,17 @@ idm_init(struct idm *m, pool *p, uint size)
static inline int u32_cto(uint x) { return ffs(~x) - 1; }
u32
u64
idm_alloc(struct idm *m)
{
uint i, j;
u64 i, j;
for (i = m->pos; i < m->size; i++)
if (m->data[i] != 0xffffffff)
goto found;
/* If we are at least 7/8 full, expand */
if (m->used > (m->size * 28))
/* If we are at least 7/8 full, expand (if we are allowed to) */
if ((m->used * 32 < m->max) && (m->used > m->size * 28))
{
m->size *= 2;
m->data = mb_realloc(m->data, m->size * sizeof(u32));
@ -51,24 +52,26 @@ idm_alloc(struct idm *m)
if (m->data[i] != 0xffffffff)
goto found;
ASSERT(0);
return 0;
found:
ASSERT(i < 0x8000000);
m->pos = i;
j = u32_cto(m->data[i]);
u64 id = 32 * i + j;
ASSERT(id < m->max);
m->data[i] |= (1 << j);
m->used++;
return 32 * i + j;
return id;
}
void
idm_free(struct idm *m, u32 id)
idm_free(struct idm *m, u64 id)
{
uint i = id / 32;
uint j = id % 32;
u64 i = id / 32;
u64 j = id % 32;
ASSERT((i < m->size) && (m->data[i] & (1 << j)));
m->data[i] &= ~(1 << j);

View File

@ -13,13 +13,14 @@
struct idm
{
u32 *data;
u32 pos;
u32 used;
u32 size;
u64 pos;
u64 used;
u64 size;
u64 max;
};
void idm_init(struct idm *m, pool *p, uint size);
u32 idm_alloc(struct idm *m);
void idm_free(struct idm *m, u32 id);
void idm_init(struct idm *m, pool *p, u64 size, u64 max);
u64 idm_alloc(struct idm *m);
void idm_free(struct idm *m, u64 id);
#endif

View File

@ -40,7 +40,7 @@ tindex_new(pool *p)
ti->unit_size = TI_MIN_UNIT_SIZE;
ti->address_size = TI_MIN_ADDRESS_SIZE;
ti->index_data = mb_allocz(p, ti->unit_size * (1 << ti->address_size));
idm_init(&(ti->idm), p, (1 << ti->address_size));
idm_init(&(ti->idm), p, (1 << (ti->address_size - 5)), (1 << ti->address_size));
u32 rootnode = idm_alloc(&(ti->idm));
ASSERT(rootnode == 1);
return ti;

View File

@ -111,7 +111,7 @@ rte_src_init(void)
{
rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE, 1ULL << 32);
HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
}
@ -137,6 +137,7 @@ rt_get_source(struct proto *p, u32 id)
src->proto = p;
src->private_id = id;
src->global_id = idm_alloc(&src_ids);
ASSERT(src->global_id);
src->uc = 0;
HASH_INSERT2(src_hash, RSH, rta_pool, src);

View File

@ -257,7 +257,7 @@ ospf_start(struct proto *P)
init_list(&(p->area_list));
fib_init(&p->rtf, P->pool, ospf_get_af(p), sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
if (ospf_is_v3(p))
idm_init(&p->idm, P->pool, 16);
idm_init(&p->idm, P->pool, 16, 1ULL << 32);
p->areano = 0;
p->gr = ospf_top_new(p, P->pool);
s_init_list(&(p->lsal));

View File

@ -563,6 +563,7 @@ ort_to_lsaid(struct ospf_proto *p, ort *nf)
if (!nf->lsa_id)
nf->lsa_id = idm_alloc(&p->idm);
ASSERT(nf->lsa_id);
return nf->lsa_id;
}