idtree.h 2.9 KB

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