6 * idtree_new - create an idr_context
7 * @mem_ctx: talloc parent to allocate from (may be NULL).
9 * Allocate an empty id tree. You can free it with talloc_free().
14 * static struct idtree *ids;
17 * ids = idtree_new(NULL);
19 * err(1, "Failed to allocate idtree");
21 struct idtree *idtree_new(void *mem_ctx);
24 * idtree_add - get lowest available id, and assign a pointer to it.
25 * @idtree: the tree to allocate from
26 * @ptr: the non-NULL pointer to associate with the id
27 * @limit: the maximum id to allocate (ie. INT_MAX means no limit).
29 * This returns a non-negative id number, or -1 if all are taken.
37 * // Create a new foo, assigning an id.
38 * struct foo *new_foo(void)
41 * foo = malloc(sizeof(*foo));
45 * id = idtree_add(ids, foo, INT_MAX);
54 int idtree_add(struct idtree *idtree, const void *ptr, int limit);
57 * idtree_add_above - get lowest available id, starting at a given value.
58 * @idtree: the tree to allocate from
59 * @ptr: the non-NULL pointer to associate with the id
60 * @starting_id: the minimum id value to consider.
61 * @limit: the maximum id to allocate (ie. INT_MAX means no limit).
64 * static int last_id = -1;
70 * // Create a new foo, assigning a consecutive id.
71 * // This maximizes the time before ids roll.
72 * struct foo *new_foo(void)
75 * foo = malloc(sizeof(*foo));
79 * id = idtree_add_above(ids, foo, last_id+1, INT_MAX);
81 * id = idtree_add(ids, foo, INT_MAX);
92 int idtree_add_above(struct idtree *idtree, const void *ptr,
93 int starting_id, int limit);
96 * idtree_lookup - look up a given id
97 * @idtree: the tree to look in
98 * @id: the id to look up
100 * Returns NULL if the value is not found, otherwise the pointer value
101 * set with the idtree_add()/idtree_add_above().
104 * // Look up a foo for a given ID.
105 * struct foo *find_foo(unsigned int id)
107 * return idtree_lookup(ids, id);
110 void *idtree_lookup(const struct idtree *idtree, int id);
113 * idtree_remove - remove a given id.
114 * @idtree: the tree to remove from
115 * @id: the id to remove.
117 * Returns false if the id was not in the tree.
120 * #include <assert.h>
122 * // Look up a foo for a given ID.
123 * void *free_foo(struct foo *foo)
125 * bool exists = idtree_remove(ids, foo->id);
130 bool idtree_remove(struct idtree *idtree, int id);
131 #endif /* CCAN_IDTREE_H */