astobj2_container_private.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * astobj2 - replacement containers for asterisk data structures.
  3. *
  4. * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Common, private definitions for astobj2 containers.
  19. *
  20. * \author Richard Mudgett <rmudgett@digium.com>
  21. */
  22. #ifndef ASTOBJ2_CONTAINER_PRIVATE_H_
  23. #define ASTOBJ2_CONTAINER_PRIVATE_H_
  24. #include "asterisk/astobj2.h"
  25. /*!
  26. * \internal
  27. * \brief Enum for internal_ao2_unlink_node.
  28. */
  29. enum ao2_unlink_node_flags {
  30. /*! Remove the node from the object's weak link list
  31. * OR unref the object if it's a strong reference. */
  32. AO2_UNLINK_NODE_UNLINK_OBJECT = (1 << 0),
  33. /*! Modified unlink_object to skip the unref of the object. */
  34. AO2_UNLINK_NODE_NOUNREF_OBJECT = (1 << 1),
  35. /*! Unref the node. */
  36. AO2_UNLINK_NODE_UNREF_NODE = (1 << 2),
  37. /*! Decrement the container's element count. */
  38. AO2_UNLINK_NODE_DEC_COUNT = (1 << 3),
  39. };
  40. enum ao2_callback_type {
  41. AO2_CALLBACK_DEFAULT,
  42. AO2_CALLBACK_WITH_DATA,
  43. };
  44. enum ao2_container_insert {
  45. /*! The node was inserted into the container. */
  46. AO2_CONTAINER_INSERT_NODE_INSERTED,
  47. /*! The node object replaced an existing node object. */
  48. AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED,
  49. /*! The node was rejected (duplicate). */
  50. AO2_CONTAINER_INSERT_NODE_REJECTED,
  51. };
  52. /*! Allow enough room for container specific traversal state structs */
  53. #define AO2_TRAVERSAL_STATE_SIZE 100
  54. /*!
  55. * \brief Generic container node.
  56. *
  57. * \details This is the base container node type that contains
  58. * values common to all container nodes.
  59. */
  60. struct ao2_container_node {
  61. /*! Stored object in node. */
  62. void *obj;
  63. /*! Container holding the node. (Does not hold a reference.) */
  64. struct ao2_container *my_container;
  65. /*! TRUE if the node is linked into the container. */
  66. unsigned int is_linked:1;
  67. };
  68. /*!
  69. * \brief Destroy this container.
  70. *
  71. * \param self Container to operate upon.
  72. *
  73. * \return Nothing
  74. */
  75. typedef void (*ao2_container_destroy_fn)(struct ao2_container *self);
  76. /*!
  77. * \brief Create an empty copy of this container.
  78. *
  79. * \param self Container to operate upon.
  80. *
  81. * \retval empty-container on success.
  82. * \retval NULL on error.
  83. */
  84. typedef struct ao2_container *(*ao2_container_alloc_empty_clone_fn)(struct ao2_container *self);
  85. /*!
  86. * \brief Create an empty copy of this container. (Debug version)
  87. *
  88. * \param self Container to operate upon.
  89. * \param tag used for debugging.
  90. * \param file Debug file name invoked from
  91. * \param line Debug line invoked from
  92. * \param func Debug function name invoked from
  93. * \param ref_debug TRUE if to output a debug reference message.
  94. *
  95. * \retval empty-container on success.
  96. * \retval NULL on error.
  97. */
  98. typedef struct ao2_container *(*ao2_container_alloc_empty_clone_debug_fn)(struct ao2_container *self, const char *tag, const char *file, int line, const char *func, int ref_debug);
  99. /*!
  100. * \brief Create a new container node.
  101. *
  102. * \param self Container to operate upon.
  103. * \param obj_new Object to put into the node.
  104. * \param tag used for debugging.
  105. * \param file Debug file name invoked from
  106. * \param line Debug line invoked from
  107. * \param func Debug function name invoked from
  108. *
  109. * \retval initialized-node on success.
  110. * \retval NULL on error.
  111. */
  112. typedef struct ao2_container_node *(*ao2_container_new_node_fn)(struct ao2_container *self, void *obj_new, const char *tag, const char *file, int line, const char *func);
  113. /*!
  114. * \brief Insert a node into this container.
  115. *
  116. * \param self Container to operate upon.
  117. * \param node Container node to insert into the container.
  118. *
  119. * \return enum ao2_container_insert value.
  120. */
  121. typedef enum ao2_container_insert (*ao2_container_insert_fn)(struct ao2_container *self, struct ao2_container_node *node);
  122. /*!
  123. * \brief Find the first container node in a traversal.
  124. *
  125. * \param self Container to operate upon.
  126. * \param flags search_flags to control traversing the container
  127. * \param arg Comparison callback arg parameter.
  128. * \param v_state Traversal state to restart container traversal.
  129. *
  130. * \retval node-ptr of found node (Reffed).
  131. * \retval NULL when no node found.
  132. */
  133. typedef struct ao2_container_node *(*ao2_container_find_first_fn)(struct ao2_container *self, enum search_flags flags, void *arg, void *v_state);
  134. /*!
  135. * \brief Find the next container node in a traversal.
  136. *
  137. * \param self Container to operate upon.
  138. * \param v_state Traversal state to restart container traversal.
  139. * \param prev Previous node returned by the traversal search functions.
  140. * The ref ownership is passed back to this function.
  141. *
  142. * \retval node-ptr of found node (Reffed).
  143. * \retval NULL when no node found.
  144. */
  145. typedef struct ao2_container_node *(*ao2_container_find_next_fn)(struct ao2_container *self, void *v_state, struct ao2_container_node *prev);
  146. /*!
  147. * \brief Cleanup the container traversal state.
  148. *
  149. * \param v_state Traversal state to cleanup.
  150. *
  151. * \return Nothing
  152. */
  153. typedef void (*ao2_container_find_cleanup_fn)(void *v_state);
  154. /*!
  155. * \brief Find the next non-empty iteration node in the container.
  156. *
  157. * \param self Container to operate upon.
  158. * \param prev Previous node returned by the iterator.
  159. * \param flags search_flags to control iterating the container.
  160. * Only AO2_ITERATOR_DESCENDING is useful by the method.
  161. *
  162. * \note The container is already locked.
  163. *
  164. * \retval node on success.
  165. * \retval NULL on error or no more nodes in the container.
  166. */
  167. typedef struct ao2_container_node *(*ao2_iterator_next_fn)(struct ao2_container *self, struct ao2_container_node *prev, enum ao2_iterator_flags flags);
  168. /*!
  169. * \brief Display contents of the specified container.
  170. *
  171. * \param self Container to dump.
  172. * \param where User data needed by prnt to determine where to put output.
  173. * \param prnt Print output callback function to use.
  174. * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
  175. *
  176. * \return Nothing
  177. */
  178. typedef void (*ao2_container_display)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj);
  179. /*!
  180. * \brief Display statistics of the specified container.
  181. *
  182. * \param self Container to display statistics.
  183. * \param where User data needed by prnt to determine where to put output.
  184. * \param prnt Print output callback function to use.
  185. *
  186. * \note The container is already locked for reading.
  187. *
  188. * \return Nothing
  189. */
  190. typedef void (*ao2_container_statistics)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt);
  191. /*!
  192. * \brief Perform an integrity check on the specified container.
  193. *
  194. * \param self Container to check integrity.
  195. *
  196. * \note The container is already locked for reading.
  197. *
  198. * \retval 0 on success.
  199. * \retval -1 on error.
  200. */
  201. typedef int (*ao2_container_integrity)(struct ao2_container *self);
  202. /*!
  203. * \internal
  204. * \brief Increment the container linked object statistic.
  205. * \since 12.4.0
  206. *
  207. * \param container Container to operate upon.
  208. * \param node Container node linking object to.
  209. *
  210. * \return Nothing
  211. */
  212. typedef void (*ao2_link_node_stat_fn)(struct ao2_container *container, struct ao2_container_node *node);
  213. /*!
  214. * \internal
  215. * \brief Decrement the container linked object statistic.
  216. * \since 12.4.0
  217. *
  218. * \param container Container to operate upon.
  219. * \param node Container node unlinking object from.
  220. *
  221. * \return Nothing
  222. */
  223. typedef void (*ao2_unlink_node_stat_fn)(struct ao2_container *container, struct ao2_container_node *node);
  224. /*! Container virtual methods template. */
  225. struct ao2_container_methods {
  226. /*! Destroy this container. */
  227. ao2_container_destroy_fn destroy;
  228. /*! \brief Create an empty copy of this container. */
  229. ao2_container_alloc_empty_clone_fn alloc_empty_clone;
  230. /*! \brief Create an empty copy of this container. (Debug version) */
  231. ao2_container_alloc_empty_clone_debug_fn alloc_empty_clone_debug;
  232. /*! Create a new container node. */
  233. ao2_container_new_node_fn new_node;
  234. /*! Insert a node into this container. */
  235. ao2_container_insert_fn insert;
  236. /*! Traverse the container, find the first node. */
  237. ao2_container_find_first_fn traverse_first;
  238. /*! Traverse the container, find the next node. */
  239. ao2_container_find_next_fn traverse_next;
  240. /*! Traverse the container, cleanup state. */
  241. ao2_container_find_cleanup_fn traverse_cleanup;
  242. /*! Find the next iteration element in the container. */
  243. ao2_iterator_next_fn iterator_next;
  244. #if defined(AO2_DEBUG)
  245. /*! Increment the container linked object statistic. */
  246. ao2_link_node_stat_fn link_stat;
  247. /*! Deccrement the container linked object statistic. */
  248. ao2_unlink_node_stat_fn unlink_stat;
  249. /*! Display container contents. (Method for debug purposes) */
  250. ao2_container_display dump;
  251. /*! Display container debug statistics. (Method for debug purposes) */
  252. ao2_container_statistics stats;
  253. /*! Perform an integrity check on the container. (Method for debug purposes) */
  254. ao2_container_integrity integrity;
  255. #endif /* defined(AO2_DEBUG) */
  256. };
  257. /*!
  258. * \brief Generic container type.
  259. *
  260. * \details This is the base container type that contains values
  261. * common to all container types.
  262. *
  263. * \todo Linking and unlinking container objects is typically
  264. * expensive, as it involves a malloc()/free() of a small object
  265. * which is very inefficient. To optimize this, we can allocate
  266. * larger arrays of container nodes when we run out of them, and
  267. * then manage our own freelist. This will be more efficient as
  268. * we can do the freelist management while we hold the lock
  269. * (that we need anyway).
  270. */
  271. struct ao2_container {
  272. /*! Container virtual method table. */
  273. const struct ao2_container_methods *v_table;
  274. /*! Container sort function if the container is sorted. */
  275. ao2_sort_fn *sort_fn;
  276. /*! Container traversal matching function for ao2_find. */
  277. ao2_callback_fn *cmp_fn;
  278. /*! The container option flags */
  279. uint32_t options;
  280. /*! Number of elements in the container. */
  281. int elements;
  282. #if defined(AO2_DEBUG)
  283. /*! Number of nodes in the container. */
  284. int nodes;
  285. /*! Maximum number of empty nodes in the container. (nodes - elements) */
  286. int max_empty_nodes;
  287. #endif /* defined(AO2_DEBUG) */
  288. /*!
  289. * \brief TRUE if the container is being destroyed.
  290. *
  291. * \note The destruction traversal should override any requested
  292. * search order to do the most efficient order for destruction.
  293. *
  294. * \note There should not be any empty nodes in the container
  295. * during destruction. If there are then an error needs to be
  296. * issued about container node reference leaks.
  297. */
  298. unsigned int destroying:1;
  299. };
  300. /*!
  301. * \internal
  302. * \brief Unlink a node from this container.
  303. *
  304. * \param node Node to operate upon.
  305. * \param flags ao2_unlink_node_flags governing behavior.
  306. *
  307. * \retval 0 on errors.
  308. * \retval 1 on success.
  309. */
  310. int __container_unlink_node_debug(struct ao2_container_node *node, uint32_t flags,
  311. const char *tag, const char *file, int line, const char *func);
  312. #define __container_unlink_node(node, flags) \
  313. __container_unlink_node_debug(node, flags, NULL, NULL, 0, NULL)
  314. void container_destruct(void *_c);
  315. void container_destruct_debug(void *_c);
  316. int container_init(void);
  317. #endif /* ASTOBJ2_CONTAINER_PRIVATE_H_ */