X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fidtree%2Fidtree.c;h=e10c17239cdfc407e40da7c77dfb4e5f57edc42a;hb=4c1266e86aa8accb64ce116887e9ce2ec66dd798;hp=bf83c31856e8a6bdea8d8f4040b347c990ecbb99;hpb=184070eb4a4a2e130c0053eb82fd8c96a19e954f;p=ccan diff --git a/ccan/idtree/idtree.c b/ccan/idtree/idtree.c index bf83c318..e10c1723 100644 --- a/ccan/idtree/idtree.c +++ b/ccan/idtree/idtree.c @@ -25,7 +25,7 @@ */ #include -#include +#include #include #include @@ -42,9 +42,9 @@ #define MAX_LEVEL (MAX_ID_SHIFT + IDTREE_BITS - 1) / IDTREE_BITS #define IDTREE_FREE_MAX MAX_LEVEL + MAX_LEVEL -#define set_bit(bit, v) (v) |= (1<<(bit)) -#define clear_bit(bit, v) (v) &= ~(1<<(bit)) -#define test_bit(bit, v) ((v) & (1<<(bit))) +#define set_bit(bit, v) (v) |= (1U<<(bit)) +#define clear_bit(bit, v) (v) &= ~(1U<<(bit)) +#define test_bit(bit, v) ((v) & (1U<<(bit))) struct idtree_layer { uint32_t bitmap; @@ -87,7 +87,7 @@ static void free_layer(struct idtree *idp, struct idtree_layer *p) static int idtree_pre_get(struct idtree *idp) { while (idp->id_free_cnt < IDTREE_FREE_MAX) { - struct idtree_layer *pn = talloc_zero(idp, struct idtree_layer); + struct idtree_layer *pn = talz(idp, struct idtree_layer); if(pn == NULL) return (0); free_layer(idp, pn); @@ -99,8 +99,9 @@ static int sub_alloc(struct idtree *idp, const void *ptr, int *starting_id) { int n, m, sh; struct idtree_layer *p, *pn; - struct idtree_layer *pa[MAX_LEVEL]; - int l, id, oid; + struct idtree_layer *pa[MAX_LEVEL+1]; + unsigned int l; + int id, oid; uint32_t bm; memset(pa, 0, sizeof(pa)); @@ -121,7 +122,7 @@ restart: /* no space available go back to previous layer. */ l++; oid = id; - id = (id | ((1 << (IDTREE_BITS*l))-1)) + 1; + id = (id | ((1U << (IDTREE_BITS*l))-1)) + 1; /* if already at the top layer, we need to grow */ if (!(p = pa[l])) { @@ -134,7 +135,7 @@ restart: */ sh = IDTREE_BITS * (l + 1); if (oid >> sh == id >> sh) - continue; + continue; else goto restart; } @@ -236,7 +237,7 @@ build_up: static int sub_remove(struct idtree *idp, int shift, int id) { struct idtree_layer *p = idp->top; - struct idtree_layer **pa[MAX_LEVEL]; + struct idtree_layer **pa[1+MAX_LEVEL]; struct idtree_layer ***paa = &pa[0]; int n; @@ -276,7 +277,8 @@ void *idtree_lookup(const struct idtree *idp, int id) * This tests to see if bits outside the current tree are * present. If so, tain't one of ours! */ - if ((id & ~(~0 << MAX_ID_SHIFT)) >> (n + IDTREE_BITS)) + if (n + IDTREE_BITS < 31 && + (id & ~(~0U << MAX_ID_SHIFT)) >> (n + IDTREE_BITS)) return NULL; /* Mask off upper bits we don't use for the search. */ @@ -312,14 +314,14 @@ bool idtree_remove(struct idtree *idp, int id) } while (idp->id_free_cnt >= IDTREE_FREE_MAX) { p = alloc_layer(idp); - talloc_free(p); + tal_free(p); } return true; } struct idtree *idtree_new(void *mem_ctx) { - return talloc_zero(mem_ctx, struct idtree); + return talz(mem_ctx, struct idtree); } int idtree_add(struct idtree *idp, const void *ptr, int limit)