]> git.ozlabs.org Git - ccan/blob - ccan/idtree/idtree.h
idtree: import fix from SAMBA source
[ccan] / ccan / idtree / idtree.h
1 #ifndef CCAN_IDTREE_H
2 #define CCAN_IDTREE_H
3 #include <stdbool.h>
4
5 /**
6  * idtree_new - create an idr_context
7  * @mem_ctx: talloc parent to allocate from (may be NULL).
8  *
9  * Allocate an empty id tree.  You can free it with talloc_free().
10  *
11  * Example:
12  *      static struct idtree *ids;
13  *
14  *      static void init(void)
15  *      {
16  *              ids = idtree_new(NULL);
17  *              if (!ids)
18  *                      err(1, "Failed to allocate idtree");
19  *      }
20  */
21 struct idtree *idtree_new(void *mem_ctx);
22
23 /**
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).
28  *
29  * This returns a non-negative id number, or -1 if all are taken.
30  *
31  * Example:
32  *      struct foo {
33  *              unsigned int id;
34  *              // ...
35  *      };
36  *
37  *      // Create a new foo, assigning an id.
38  *      static struct foo *new_foo(void)
39  *      {
40  *              int id;
41  *              struct foo *foo = malloc(sizeof(*foo));
42  *              if (!foo)
43  *                      return NULL;
44  *
45  *              id = idtree_add(ids, foo, INT_MAX);
46  *              if (id < 0) {
47  *                      free(foo);
48  *                      return NULL;
49  *              }
50  *              foo->id = id;
51  *              return foo;
52  *      }
53  */
54 int idtree_add(struct idtree *idtree, const void *ptr, int limit);
55
56 /**
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).
62  *
63  * Example:
64  *      static int last_id = -1;
65  *
66  *      // Create a new foo, assigning a consecutive id.
67  *      // This maximizes the time before ids roll.
68  *      static struct foo *new_foo_inc_id(void)
69  *      {
70  *              int id;
71  *              struct foo *foo = malloc(sizeof(*foo));
72  *              if (!foo)
73  *                      return NULL;
74  *
75  *              id = idtree_add_above(ids, foo, last_id+1, INT_MAX);
76  *              if (id < 0) {
77  *                      id = idtree_add(ids, foo, INT_MAX);
78  *                      if (id < 0) {
79  *                              free(foo);
80  *                              return NULL;
81  *                      }
82  *              }
83  *              last_id = id;
84  *              foo->id = id;
85  *              return foo;
86  *      }
87  */
88 int idtree_add_above(struct idtree *idtree, const void *ptr,
89                      int starting_id, int limit);
90
91 /**
92  * idtree_lookup - look up a given id
93  * @idtree: the tree to look in
94  * @id: the id to look up
95  *
96  * Returns NULL if the value is not found, otherwise the pointer value
97  * set with the idtree_add()/idtree_add_above().
98  *
99  * Example:
100  *      // Look up a foo for a given ID.
101  *      static struct foo *find_foo(unsigned int id)
102  *      {
103  *              return idtree_lookup(ids, id);
104  *      }
105  */
106 void *idtree_lookup(const struct idtree *idtree, int id);
107
108 /**
109  * idtree_remove - remove a given id.
110  * @idtree: the tree to remove from
111  * @id: the id to remove.
112  *
113  * Returns false if the id was not in the tree.
114  *
115  * Example:
116  *      // Look up a foo for a given ID.
117  *      static void free_foo(struct foo *foo)
118  *      {
119  *              bool exists = idtree_remove(ids, foo->id);
120  *              assert(exists);
121  *              free(foo);
122  *      }
123  */
124 bool idtree_remove(struct idtree *idtree, int id);
125 #endif /* CCAN_IDTREE_H */