Asterisk - The Open Source Telephony Project  GIT-master-a1fa8df
astobj2_container_private.h
Go to the documentation of this file.
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 
17 /*! \file
18  *
19  * \brief Common, private definitions for astobj2 containers.
20  *
21  * \author Richard Mudgett <rmudgett@digium.com>
22  */
23 
24 #ifndef ASTOBJ2_CONTAINER_PRIVATE_H_
25 #define ASTOBJ2_CONTAINER_PRIVATE_H_
26 
27 #include "asterisk/astobj2.h"
28 
29 /*!
30  * \internal
31  * \brief Enum for internal_ao2_unlink_node.
32  */
34  /*! Remove the node from the object's weak link list
35  * OR unref the object if it's a strong reference. */
37  /*! Modified unlink_object to skip the unref of the object. */
39  /*! Unref the node. */
41  /*! Decrement the container's element count. */
43 };
44 
48 };
49 
51  /*! The node was inserted into the container. */
53  /*! The node object replaced an existing node object. */
55  /*! The node was rejected (duplicate). */
57 };
58 
59 /*! Allow enough room for container specific traversal state structs */
60 #define AO2_TRAVERSAL_STATE_SIZE 100
61 
62 /*!
63  * \brief Generic container node.
64  *
65  * \details This is the base container node type that contains
66  * values common to all container nodes.
67  */
69  /*! Stored object in node. */
70  void *obj;
71  /*! Container holding the node. (Does not hold a reference.) */
73  /*! TRUE if the node is linked into the container. */
74  unsigned int is_linked:1;
75 };
76 
77 /*!
78  * \brief Destroy this container.
79  *
80  * \param self Container to operate upon.
81  *
82  * \return Nothing
83  */
84 typedef void (*ao2_container_destroy_fn)(struct ao2_container *self);
85 
86 /*!
87  * \brief Create an empty copy of this container.
88  *
89  * \param self Container to operate upon.
90  * \param tag used for debugging.
91  * \param file Debug file name invoked from
92  * \param line Debug line invoked from
93  * \param func Debug function name invoked from
94  *
95  * \retval empty-container on success.
96  * \retval NULL on error.
97  */
98 typedef struct ao2_container *(*ao2_container_alloc_empty_clone_fn)(struct ao2_container *self, const char *tag, const char *file, int line, const char *func);
99 
100 /*!
101  * \brief Create a new container node.
102  *
103  * \param self Container to operate upon.
104  * \param obj_new Object to put into the node.
105  * \param tag used for debugging.
106  * \param file Debug file name invoked from
107  * \param line Debug line invoked from
108  * \param func Debug function name invoked from
109  *
110  * \retval initialized-node on success.
111  * \retval NULL on error.
112  */
113 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);
114 
115 /*!
116  * \brief Insert a node into this container.
117  *
118  * \param self Container to operate upon.
119  * \param node Container node to insert into the container.
120  *
121  * \return enum ao2_container_insert value.
122  */
124 
125 /*!
126  * \brief Find the first container node in a traversal.
127  *
128  * \param self Container to operate upon.
129  * \param flags search_flags to control traversing the container
130  * \param arg Comparison callback arg parameter.
131  * \param v_state Traversal state to restart container traversal.
132  *
133  * \retval node-ptr of found node (Reffed).
134  * \retval NULL when no node found.
135  */
136 typedef struct ao2_container_node *(*ao2_container_find_first_fn)(struct ao2_container *self, enum search_flags flags, void *arg, void *v_state);
137 
138 /*!
139  * \brief Find the next container node in a traversal.
140  *
141  * \param self Container to operate upon.
142  * \param v_state Traversal state to restart container traversal.
143  * \param prev Previous node returned by the traversal search functions.
144  * The ref ownership is passed back to this function.
145  *
146  * \retval node-ptr of found node (Reffed).
147  * \retval NULL when no node found.
148  */
149 typedef struct ao2_container_node *(*ao2_container_find_next_fn)(struct ao2_container *self, void *v_state, struct ao2_container_node *prev);
150 
151 /*!
152  * \brief Cleanup the container traversal state.
153  *
154  * \param v_state Traversal state to cleanup.
155  *
156  * \return Nothing
157  */
158 typedef void (*ao2_container_find_cleanup_fn)(void *v_state);
159 
160 /*!
161  * \brief Find the next non-empty iteration node in the container.
162  *
163  * \param self Container to operate upon.
164  * \param prev Previous node returned by the iterator.
165  * \param flags search_flags to control iterating the container.
166  * Only AO2_ITERATOR_DESCENDING is useful by the method.
167  *
168  * \note The container is already locked.
169  *
170  * \retval node on success.
171  * \retval NULL on error or no more nodes in the container.
172  */
173 typedef struct ao2_container_node *(*ao2_iterator_next_fn)(struct ao2_container *self, struct ao2_container_node *prev, enum ao2_iterator_flags flags);
174 
175 /*!
176  * \brief Display contents of the specified container.
177  *
178  * \param self Container to dump.
179  * \param where User data needed by prnt to determine where to put output.
180  * \param prnt Print output callback function to use.
181  * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
182  *
183  * \return Nothing
184  */
185 typedef void (*ao2_container_display)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj);
186 
187 /*!
188  * \brief Display statistics of the specified container.
189  *
190  * \param self Container to display statistics.
191  * \param where User data needed by prnt to determine where to put output.
192  * \param prnt Print output callback function to use.
193  *
194  * \note The container is already locked for reading.
195  *
196  * \return Nothing
197  */
198 typedef void (*ao2_container_statistics)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt);
199 
200 /*!
201  * \brief Perform an integrity check on the specified container.
202  *
203  * \param self Container to check integrity.
204  *
205  * \note The container is already locked for reading.
206  *
207  * \retval 0 on success.
208  * \retval -1 on error.
209  */
210 typedef int (*ao2_container_integrity)(struct ao2_container *self);
211 
212 /*!
213  * \internal
214  * \brief Increment the container linked object statistic.
215  * \since 12.4.0
216  *
217  * \param container Container to operate upon.
218  * \param node Container node linking object to.
219  *
220  * \return Nothing
221  */
223 
224 /*!
225  * \internal
226  * \brief Decrement the container linked object statistic.
227  * \since 12.4.0
228  *
229  * \param container Container to operate upon.
230  * \param node Container node unlinking object from.
231  *
232  * \return Nothing
233  */
235 
236 /*! Container virtual methods template. */
238  /*! Destroy this container. */
240  /*! \brief Create an empty copy of this container. */
242  /*! Create a new container node. */
244  /*! Insert a node into this container. */
246  /*! Traverse the container, find the first node. */
248  /*! Traverse the container, find the next node. */
250  /*! Traverse the container, cleanup state. */
252  /*! Find the next iteration element in the container. */
254 #if defined(AO2_DEBUG)
255  /*! Increment the container linked object statistic. */
256  ao2_link_node_stat_fn link_stat;
257  /*! Deccrement the container linked object statistic. */
258  ao2_unlink_node_stat_fn unlink_stat;
259  /*! Display container contents. (Method for debug purposes) */
261  /*! Display container debug statistics. (Method for debug purposes) */
263  /*! Perform an integrity check on the container. (Method for debug purposes) */
264  ao2_container_integrity integrity;
265 #endif /* defined(AO2_DEBUG) */
266 };
267 
268 /*!
269  * \brief Generic container type.
270  *
271  * \details This is the base container type that contains values
272  * common to all container types.
273  *
274  * \todo Linking and unlinking container objects is typically
275  * expensive, as it involves a malloc()/free() of a small object
276  * which is very inefficient. To optimize this, we can allocate
277  * larger arrays of container nodes when we run out of them, and
278  * then manage our own freelist. This will be more efficient as
279  * we can do the freelist management while we hold the lock
280  * (that we need anyway).
281  */
283  /*! Container virtual method table. */
285  /*! Container sort function if the container is sorted. */
287  /*! Container traversal matching function for ao2_find. */
289  /*! The container option flags */
290  uint32_t options;
291  /*! Number of elements in the container. */
292  int elements;
293 #if defined(AO2_DEBUG)
294  /*! Number of nodes in the container. */
295  int nodes;
296  /*! Maximum number of empty nodes in the container. (nodes - elements) */
297  int max_empty_nodes;
298 #endif /* defined(AO2_DEBUG) */
299  /*!
300  * \brief TRUE if the container is being destroyed.
301  *
302  * \note The destruction traversal should override any requested
303  * search order to do the most efficient order for destruction.
304  *
305  * \note There should not be any empty nodes in the container
306  * during destruction. If there are then an error needs to be
307  * issued about container node reference leaks.
308  */
309  unsigned int destroying:1;
310 };
311 
312 /*!
313  * \internal
314  * \brief Unlink a node from this container.
315  *
316  * \param node Node to operate upon.
317  * \param flags ao2_unlink_node_flags governing behavior.
318  *
319  * \retval 0 on errors.
320  * \retval 1 on success.
321  */
322 int __container_unlink_node_debug(struct ao2_container_node *node, uint32_t flags,
323  const char *tag, const char *file, int line, const char *func);
324 
325 #define __container_unlink_node(node, flags) \
326  __container_unlink_node_debug(node, flags, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
327 
328 void container_destruct(void *_c);
329 int container_init(void);
330 
331 #endif /* ASTOBJ2_CONTAINER_PRIVATE_H_ */
Definition: test_heap.c:38
struct ao2_container_node *(* ao2_iterator_next_fn)(struct ao2_container *self, struct ao2_container_node *prev, enum ao2_iterator_flags flags)
Find the next non-empty iteration node in the container.
ao2_container_destroy_fn destroy
enum ao2_container_insert(* ao2_container_insert_fn)(struct ao2_container *self, struct ao2_container_node *node)
Insert a node into this container.
int() ao2_sort_fn(const void *obj_left, const void *obj_right, int flags)
Type of generic container sort function.
Definition: astobj2.h:1280
int __container_unlink_node_debug(struct ao2_container_node *node, uint32_t flags, const char *tag, const char *file, int line, const char *func)
int container_init(void)
int(* ao2_container_integrity)(struct ao2_container *self)
Perform an integrity check on the specified container.
ao2_container_find_first_fn traverse_first
ao2_container_find_cleanup_fn traverse_cleanup
search_flags
Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
Definition: astobj2.h:1038
void() ao2_prnt_fn(void *where, const char *fmt,...)
Print output.
Definition: astobj2.h:1442
Generic container node.
int() ao2_callback_fn(void *obj, void *arg, int flags)
Type of a generic callback function.
Definition: astobj2.h:1230
struct ao2_container_node *(* ao2_container_find_first_fn)(struct ao2_container *self, enum search_flags flags, void *arg, void *v_state)
Find the first container node in a traversal.
void(* ao2_link_node_stat_fn)(struct ao2_container *container, struct ao2_container_node *node)
void container_destruct(void *_c)
const struct ao2_container_methods * v_table
void(* ao2_container_find_cleanup_fn)(void *v_state)
Cleanup the container traversal state.
struct ao2_container * container
Definition: res_fax.c:502
void(* ao2_container_destroy_fn)(struct ao2_container *self)
Destroy this container.
ao2_container_find_next_fn traverse_next
void(* ao2_container_statistics)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt)
Display statistics of the specified container.
ao2_iterator_next_fn iterator_next
void(* ao2_unlink_node_stat_fn)(struct ao2_container *container, struct ao2_container_node *node)
unsigned int destroying
TRUE if the container is being destroyed.
ao2_container_insert_fn insert
struct ao2_container *(* ao2_container_alloc_empty_clone_fn)(struct ao2_container *self, const char *tag, const char *file, int line, const char *func)
Create an empty copy of this container.
Generic container type.
void() ao2_prnt_obj_fn(void *v_obj, void *where, ao2_prnt_fn *prnt)
Print object key.
Definition: astobj2.h:1454
struct ao2_container * my_container
ao2_iterator_flags
Definition: astobj2.h:1855
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)
Create a new container node.
ao2_container_alloc_empty_clone_fn alloc_empty_clone
Create an empty copy of this container.
ao2_callback_fn * cmp_fn
ao2_container_new_node_fn new_node
struct ao2_container_node *(* ao2_container_find_next_fn)(struct ao2_container *self, void *v_state, struct ao2_container_node *prev)
Find the next container node in a traversal.
void(* ao2_container_display)(struct ao2_container *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
Display contents of the specified container.
static struct ao2_container * nodes
All the nodes that we&#39;re aware of.
Definition: res_corosync.c:65