Asterisk - The Open Source Telephony Project  18.5.0
astobj2.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 #ifndef _ASTERISK_ASTOBJ2_H
18 #define _ASTERISK_ASTOBJ2_H
19 
20 #include "asterisk/compat.h"
21 #include "asterisk/lock.h"
22 #include "asterisk/linkedlists.h"
23 #include "asterisk/inline_api.h"
24 
25 /*! \file
26  * \ref AstObj2
27  *
28  * \page AstObj2 Object Model implementing objects and containers.
29 
30 This module implements an abstraction for objects (with locks and
31 reference counts), and containers for these user-defined objects,
32 also supporting locking, reference counting and callbacks.
33 
34 The internal implementation of objects and containers is opaque to the user,
35 so we can use different data structures as needs arise.
36 
37 \section AstObj2_UsageObjects USAGE - OBJECTS
38 
39 An ao2 object is a block of memory that the user code can access,
40 and for which the system keeps track (with a bit of help from the
41 programmer) of the number of references around. When an object has
42 no more references (refcount == 0), it is destroyed, by first
43 invoking whatever 'destructor' function the programmer specifies
44 (it can be NULL if none is necessary), and then freeing the memory.
45 This way objects can be shared without worrying who is in charge
46 of freeing them.
47 As an additional feature, ao2 objects are associated to individual
48 locks.
49 
50 Creating an object requires the size of the object and
51 a pointer to the destructor function:
52 
53  struct foo *o;
54 
55  o = ao2_alloc(sizeof(struct foo), my_destructor_fn);
56 
57 The value returned points to the user-visible portion of the objects
58 (user-data), but is also used as an identifier for all object-related
59 operations such as refcount and lock manipulations.
60 
61 On return from ao2_alloc():
62 
63  - the object has a refcount = 1;
64  - the memory for the object is allocated dynamically and zeroed;
65  - we cannot realloc() the object itself;
66  - we cannot call free(o) to dispose of the object. Rather, we
67  tell the system that we do not need the reference anymore:
68 
69  ao2_ref(o, -1)
70 
71  causing the destructor to be called (and then memory freed) when
72  the refcount goes to 0.
73 
74 - ao2_ref(o, +1) can be used to modify the refcount on the
75  object in case we want to pass it around.
76 
77 - ao2_lock(obj), ao2_unlock(obj), ao2_trylock(obj) can be used
78  to manipulate the lock associated with the object.
79 
80 
81 \section AstObj2_UsageContainers USAGE - CONTAINERS
82 
83 An ao2 container is an abstract data structure where we can store
84 ao2 objects, search them (hopefully in an efficient way), and iterate
85 or apply a callback function to them. A container is just an ao2 object
86 itself.
87 
88 A container must first be allocated, specifying the initial
89 parameters. At the moment, this is done as follows:
90 
91  <b>Sample Usage:</b>
92  \code
93 
94  struct ao2_container *c;
95 
96  c = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_BUCKETS,
97  my_hash_fn, NULL, my_cmp_fn);
98  \endcode
99 
100 where
101 
102 - MAX_BUCKETS is the number of buckets in the hash table,
103 - my_hash_fn() is the (user-supplied) function that returns a
104  hash key for the object (further reduced modulo MAX_BUCKETS
105  by the container's code);
106 - my_cmp_fn() is the default comparison function used when doing
107  searches on the container,
108 
109 A container knows little or nothing about the objects it stores,
110 other than the fact that they have been created by ao2_alloc().
111 All knowledge of the (user-defined) internals of the objects
112 is left to the (user-supplied) functions passed as arguments
113 to ao2_container_alloc_hash().
114 
115 If we want to insert an object in a container, we should
116 initialize its fields -- especially, those used by my_hash_fn() --
117 to compute the bucket to use.
118 Once done, we can link an object to a container with
119 
120  ao2_link(c, o);
121 
122 The function returns NULL in case of errors (and the object
123 is not inserted in the container). Other values mean success
124 (we are not supposed to use the value as a pointer to anything).
125 Linking an object to a container increases its refcount by 1
126 automatically.
127 
128 \note While an object o is in a container, we expect that
129 my_hash_fn(o) will always return the same value. The function
130 does not lock the object to be computed, so modifications of
131 those fields that affect the computation of the hash should
132 be done by extracting the object from the container, and
133 re-inserting it after the change (this is not terribly expensive).
134 
135 \note A container with a single buckets is effectively a linked
136 list. However there is no ordering among elements.
137 
138 - \ref AstObj2_Containers
139 - \ref astobj2.h All documentation for functions and data structures
140 
141  */
142 
143 /*
144 \note DEBUGGING REF COUNTS BIBLE:
145 An interface to help debug refcounting is provided
146 in this package. It is dependent on the refdebug being enabled in
147 asterisk.conf.
148 
149 Each of the reference manipulations will generate one line of output in the refs
150 log file. These lines look like this:
151 ...
152 0x8756f00,+1,1234,chan_sip.c,22240,load_module,**constructor**,allocate users
153 0x86e3408,+1,1234,chan_sip.c,22241,load_module,**constructor**,allocate peers
154 0x86dd380,+1,1234,chan_sip.c,22242,load_module,**constructor**,allocate peers_by_ip
155 0x822d020,+1,1234,chan_sip.c,22243,load_module,**constructor**,allocate dialogs
156 0x8930fd8,+1,1234,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct
157 0x8930fd8,+1,1234,chan_sip.c,21467,reload_config,1,link peer into peer table
158 0x8930fd8,-1,1234,chan_sip.c,2370,unref_peer,2,unref_peer: from reload_config
159 0x89318b0,1,5678,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct
160 0x89318b0,+1,5678,chan_sip.c,21467,reload_config,1,link peer into peer table
161 0x89318b0,-1,1234,chan_sip.c,2370,unref_peer,2,unref_peer: from reload_config
162 0x8930218,+1,1234,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct
163 0x8930218,+1,1234,chan_sip.c,21539,reload_config,1,link peer into peers table
164 0x868c040,-1,1234,chan_sip.c,2424,dialog_unlink_all,2,unset the relatedpeer->call field in tandem with relatedpeer field itself
165 0x868c040,-1,1234,chan_sip.c,2443,dialog_unlink_all,1,Let's unbump the count in the unlink so the poor pvt can disappear if it is time
166 0x868c040,-1,1234,chan_sip.c,2443,dialog_unlink_all,**destructor**,Let's unbump the count in the unlink so the poor pvt can disappear if it is time
167 0x8cc07e8,-1,1234,chan_sip.c,2370,unref_peer,3,unsetting a dialog relatedpeer field in sip_destroy
168 0x8cc07e8,+1,1234,chan_sip.c,3876,find_peer,2,ao2_find in peers table
169 0x8cc07e8,-1,1234,chan_sip.c,2370,unref_peer,3,unref_peer, from sip_devicestate, release ref from find_peer
170 ...
171 
172 This uses a comma delineated format. The columns in the format are as
173 follows:
174 - The first column is the object address.
175 - The second column reflects how the operation affected the ref count
176  for that object. A change in the ref count is reflected either as
177  an increment (+) or decrement (-), as well as the amount it changed
178  by.
179 - The third column is the ID of the thread that modified the reference
180  count.
181 - The fourth column is the source file that the change in reference was
182  issued from.
183 - The fifth column is the line number of the source file that the ref
184  change was issued from.
185 - The sixth column is the name of the function that the ref change was
186  issued from.
187 - The seventh column indicates either (a) construction of the object via
188  the special tag **constructor**; (b) destruction of the object via
189  the special tag **destructor**; (c) the previous reference count
190  prior to this reference change.
191 - The eighth column is a special tag added by the developer to provide
192  context for the ref change. Note that any subsequent columns are
193  considered to be part of this tag.
194 
195 Sometimes you have some helper functions to do object create/ref/unref
196 operations. Using these normally hides the place where these
197 functions were called. To get the location where these functions
198 were called to appear in refs log, you can do this sort of thing:
199 
200 #define my_t_alloc(data,tag) my_alloc_debug((data), tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
201 #define my_alloc(data) my_t_alloc((data), NULL)
202 
203 static struct mydata *my_alloc_debug(void *data,
204  const char *tag, const char *file, int line, const char *func)
205 {
206  struct mydata *p;
207 
208  p = __ao2_alloc(sizeof(*p), NULL, AO2_ALLOC_OPT_LOCK_MUTEX, tag, file, line, func);
209  if (p) {
210  p->data = data;
211  }
212  return p;
213 }
214 
215 To find out why objects are not destroyed (a common bug), you can
216 enable refdebug in asterisk.conf. Run asterisk, exit with "core stop gracefully".
217 This should result in every object being destroyed.
218 
219 Then, you can "sort -k 1 {AST_LOG_DIR}/refs > x1" to get a sorted list of
220 all the objects, or you can use "contrib/script/refcounter.py" to scan
221 the file for you and output any problems it finds.
222 
223 The above may seem astronomically more work than it is worth to debug
224 reference counts, which may be true in "simple" situations, but for
225 more complex situations, it is easily worth 100 times this effort to
226 help find problems.
227 
228 To debug, pair all calls so that each call that increments the
229 refcount is paired with a corresponding call that decrements the
230 count for the same reason. Hopefully, you will be left with one
231 or more unpaired calls. This is where you start your search!
232 
233 For instance, here is an example of this for a dialog object in
234 chan_sip, that was not getting destroyed, after I moved the lines around
235 to pair operations:
236 
237  0x83787a0,+1,1234,chan_sip.c,5733,sip_alloc,**constructor**,(allocate a dialog(pvt) struct)
238  0x83787a0,-1,1234,chan_sip.c,19173,sip_poke_peer,4,(unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope)
239 
240  0x83787a0,+1,1234,chan_sip.c,5854,sip_alloc,1,(link pvt into dialogs table)
241  0x83787a0,-1,1234,chan_sip.c,19150,sip_poke_peer,3,(About to change the callid -- remove the old name)
242  0x83787a0,+1,1234,chan_sip.c,19152,sip_poke_peer,2,(Linking in under new name)
243  0x83787a0,-1,1234,chan_sip.c,2399,dialog_unlink_all,5,(unlinking dialog via ao2_unlink)
244 
245  0x83787a0,+1,1234,chan_sip.c,19130,sip_poke_peer,2,(copy sip alloc from p to peer->call)
246 
247 
248  0x83787a0,+1,1234,chan_sip.c,2996,__sip_reliable_xmit,3,(__sip_reliable_xmit: setting pkt->owner)
249  0x83787a0,-1,1234,chan_sip.c,2425,dialog_unlink_all,4,(remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy)
250 
251  0x83787a0,+1,1234,chan_sip.c,22356,unload_module,4,(iterate thru dialogs)
252  0x83787a0,-1,1234,chan_sip.c,22359,unload_module,5,(toss dialog ptr from iterator_next)
253 
254 
255  0x83787a0,+1,1234,chan_sip.c,22373,unload_module,3,(iterate thru dialogs)
256  0x83787a0,-1,1234,chan_sip.c,22375,unload_module,2,(throw away iterator result)
257 
258  0x83787a0,+1,1234,chan_sip.c,2397,dialog_unlink_all,4,(Let's bump the count in the unlink so it doesn't accidentally become dead before we are done)
259  0x83787a0,-1,1234,chan_sip.c,2436,dialog_unlink_all,3,(Let's unbump the count in the unlink so the poor pvt can disappear if it is time)
260 
261 As you can see, only one unbalanced operation is in the list, a ref count increment when
262 the peer->call was set, but no corresponding decrement was made...
263 
264 Hopefully this helps you narrow your search and find those bugs.
265 
266 THE ART OF REFERENCE COUNTING
267 (by Steve Murphy)
268 SOME TIPS for complicated code, and ref counting:
269 
270 1. Theoretically, passing a refcounted object pointer into a function
271 call is an act of copying the reference, and could be refcounted.
272 But, upon examination, this sort of refcounting will explode the amount
273 of code you have to enter, and for no tangible benefit, beyond
274 creating more possible failure points/bugs. It will even
275 complicate your code and make debugging harder, slow down your program
276 doing useless increments and decrements of the ref counts.
277 
278 2. It is better to track places where a ref counted pointer
279 is copied into a structure or stored. Make sure to decrement the refcount
280 of any previous pointer that might have been there, if setting
281 this field might erase a previous pointer. ao2_find and iterate_next
282 internally increment the ref count when they return a pointer, so
283 you need to decrement the count before the pointer goes out of scope.
284 
285 3. Any time you decrement a ref count, it may be possible that the
286 object will be destroyed (freed) immediately by that call. If you
287 are destroying a series of fields in a refcounted object, and
288 any of the unref calls might possibly result in immediate destruction,
289 you can first increment the count to prevent such behavior, then
290 after the last test, decrement the pointer to allow the object
291 to be destroyed, if the refcount would be zero.
292 
293 Example:
294 
295  dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
296 
297  ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
298 
299  *//* Unlink us from the owner (channel) if we have one *//*
300  if (dialog->owner) {
301  if (lockowner) {
302  ast_channel_lock(dialog->owner);
303  }
304  ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
305  dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
306  if (lockowner) {
307  ast_channel_unlock(dialog->owner);
308  }
309  }
310  if (dialog->registry) {
311  if (dialog->registry->call == dialog) {
312  dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
313  }
314  dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
315  }
316  ...
317  dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
318 
319 In the above code, the ao2_t_unlink could end up destroying the dialog
320 object; if this happens, then the subsequent usages of the dialog
321 pointer could result in a core dump. So, we 'bump' the
322 count upwards before beginning, and then decrementing the count when
323 we are finished. This is analogous to 'locking' or 'protecting' operations
324 for a short while.
325 
326 4. One of the most insidious problems I've run into when converting
327 code to do ref counted automatic destruction, is in the destruction
328 routines. Where a "destroy" routine had previously been called to
329 get rid of an object in non-refcounted code, the new regime demands
330 that you tear that "destroy" routine into two pieces, one that will
331 tear down the links and 'unref' them, and the other to actually free
332 and reset fields. A destroy routine that does any reference deletion
333 for its own object, will never be called. Another insidious problem
334 occurs in mutually referenced structures. As an example, a dialog contains
335 a pointer to a peer, and a peer contains a pointer to a dialog. Watch
336 out that the destruction of one doesn't depend on the destruction of the
337 other, as in this case a dependency loop will result in neither being
338 destroyed!
339 
340 Given the above, you should be ready to do a good job!
341 
342 murf
343 
344 */
345 
346 
347 
348 /*!
349  * \brief Typedef for an object destructor.
350  *
351  * \param vdoomed Object to destroy.
352  *
353  * \details
354  * This is called just before freeing the memory for the object.
355  * It is passed a pointer to the user-defined data of the
356  * object.
357  *
358  * \return Nothing
359  */
360 typedef void (*ao2_destructor_fn)(void *vdoomed);
361 
362 /*! \brief Options available when allocating an ao2 object. */
364  /*! The ao2 object has a recursive mutex lock associated with it. */
366  /*! The ao2 object has a non-recursive read/write lock associated with it. */
368  /*! The ao2 object has no lock associated with it. */
370  /*! The ao2 object locking option field mask. */
372  /*!
373  * \internal The ao2 object uses a separate object for locking.
374  *
375  * \note This option is used internally by ao2_alloc_with_lockobj and
376  * should never be passed directly to ao2_alloc.
377  */
379  /*! The ao2 object will not record any REF_DEBUG entries */
381 };
382 
383 /*!
384  * \brief Allocate and initialize an object.
385  *
386  * \param data_size The sizeof() of the user-defined structure.
387  * \param destructor_fn The destructor function (can be NULL)
388  * \param options The ao2 object options (See enum ao2_alloc_opts)
389  * \param debug_msg An ao2 object debug tracing message.
390  * \return A pointer to user-data.
391  *
392  * \details
393  * Allocates a struct astobj2 with sufficient space for the
394  * user-defined structure.
395  * \note
396  * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
397  * - the refcount of the object just created is 1
398  * - the returned pointer cannot be free()'d or realloc()'ed;
399  * rather, we just call ao2_ref(o, -1);
400  *
401  * @{
402  */
403 
404 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
405  __ao2_alloc((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__)
406 #define ao2_alloc_options(data_size, destructor_fn, options) \
407  __ao2_alloc((data_size), (destructor_fn), (options), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
408 
409 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
410  __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__)
411 #define ao2_alloc(data_size, destructor_fn) \
412  __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
413 
414 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options,
415  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
416 
417 /*! @} */
418 
419 /*!
420  * \since 14.1.0
421  * \brief Allocate and initialize an object with separate locking.
422  *
423  * \param data_size The sizeof() of the user-defined structure.
424  * \param destructor_fn The destructor function (can be NULL)
425  * \param lockobj A separate ao2 object that will provide locking.
426  * \param debug_msg An ao2 object debug tracing message.
427  * \return A pointer to user-data.
428  *
429  * \see \ref ao2_alloc for additional details.
430  *
431  * \note lockobj must be a valid AO2 object.
432  */
433 #define ao2_alloc_with_lockobj(data_size, destructor_fn, lockobj, tag) \
434  __ao2_alloc_with_lockobj((data_size), (destructor_fn), (lockobj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
435 
436 void *__ao2_alloc_with_lockobj(size_t data_size, ao2_destructor_fn destructor_fn, void *lockobj,
437  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
438 
439 /*! \brief
440  * Reference/unreference an object and return the old refcount.
441  *
442  * \param o A pointer to the object
443  * \param delta Value to add to the reference counter.
444  * \param tag used for debugging
445  * \return The value of the reference counter before the operation.
446  *
447  * Increase/decrease the reference counter according
448  * the value of delta.
449  *
450  * If the refcount goes to zero, the object is destroyed.
451  *
452  * \note The object must not be locked by the caller of this function, as
453  * it is invalid to try to unlock it after releasing the reference.
454  *
455  * \note if we know the pointer to an object, it is because we
456  * have a reference count to it, so the only case when the object
457  * can go away is when we release our reference, and it is
458  * the last one in existence.
459  *
460  * @{
461  */
462 
463 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
464 #define ao2_ref(o,delta) __ao2_ref((o), (delta), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
465 
466 /*!
467  * \brief Retrieve the ao2 options used to create the object.
468  * \param obj pointer to the (user-defined part) of an object.
469  * \return options from enum ao2_alloc_opts.
470  */
471 unsigned int ao2_options_get(void *obj);
472 
473 /*!
474  * \since 12
475  * \brief Bump refcount on an AO2 object by one, returning the object.
476  *
477  * This is useful for inlining a ref bump, and you don't care about the ref
478  * count. Also \c NULL safe, for even more convenience.
479  *
480  * \param obj AO2 object to bump the refcount on.
481  * \retval The given \a obj pointer.
482  */
483 #define ao2_t_bump(obj, tag) \
484  ({ \
485  typeof(obj) __obj_ ## __LINE__ = (obj); \
486  if (__obj_ ## __LINE__) { \
487  ao2_t_ref(__obj_ ## __LINE__, +1, (tag)); \
488  } \
489  __obj_ ## __LINE__; \
490  })
491 #define ao2_bump(obj) \
492  ao2_t_bump((obj), NULL)
493 
494 int __ao2_ref(void *o, int delta, const char *tag, const char *file, int line, const char *func);
495 
496 /*!
497  * \since 12.4.0
498  * \brief Replace one object reference with another cleaning up the original.
499  *
500  * \param dst Pointer to the object that will be cleaned up.
501  * \param src Pointer to the object replacing it.
502  */
503 #define ao2_t_replace(dst, src, tag) \
504  {\
505  typeof(dst) *__dst_ ## __LINE__ = &dst; \
506  typeof(src) __src_ ## __LINE__ = src; \
507  if (__src_ ## __LINE__ != *__dst_ ## __LINE__) { \
508  if (__src_ ## __LINE__) {\
509  ao2_t_ref(__src_ ## __LINE__, +1, (tag)); \
510  } \
511  if (*__dst_ ## __LINE__) {\
512  ao2_t_ref(*__dst_ ## __LINE__, -1, (tag)); \
513  } \
514  *__dst_ ## __LINE__ = __src_ ## __LINE__; \
515  } \
516  }
517 #define ao2_replace(dst, src) \
518  ao2_t_replace((dst), (src), NULL)
519 
520 /*! @} */
521 
522 /*! \brief ao2_weakproxy
523  *
524  * @{
525  */
527 typedef void (*ao2_weakproxy_notification_cb)(void *weakproxy, void *data);
528 
529 /*! \brief This struct should be opaque, but it's size is needed. */
532 };
533 
534 /*! \brief Macro which must be used at the beginning of weakproxy capable objects.
535  *
536  * \note The primary purpose of user defined fields on weakproxy objects is to hold
537  * immutable container keys for the real object.
538  */
539 #define AO2_WEAKPROXY() struct ao2_weakproxy __weakproxy##__LINE__
540 
541 /*!
542  * \since 14.0.0
543  * \brief Allocate an ao2_weakproxy object
544  *
545  * \param data_size The sizeof() of the user-defined structure.
546  * \param destructor_fn The destructor function (can be NULL)
547  *
548  * \note "struct ao2_weakproxy" must be the first field of any object.
549  * This can be done by using AO2_WEAKPROXY to declare your structure.
550  */
551 void *__ao2_weakproxy_alloc(size_t data_size, ao2_destructor_fn destructor_fn,
552  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
553 
554 #define ao2_weakproxy_alloc(data_size, destructor_fn) \
555  __ao2_weakproxy_alloc(data_size, destructor_fn, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
556 
557 #define ao2_t_weakproxy_alloc(data_size, destructor_fn, tag) \
558  __ao2_weakproxy_alloc(data_size, destructor_fn, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
559 
560 /*!
561  * \since 14.0.0
562  * \brief Associate weakproxy with obj.
563  *
564  * \param weakproxy An object created by ao2_weakproxy_alloc.
565  * \param obj An ao2 object not created by ao2_weakproxy_alloc.
566  * \param flags OBJ_NOLOCK to avoid locking weakproxy.
567  *
568  * \retval 0 Success
569  * \retval -1 Failure
570  *
571  * \note obj must be newly created, this procedure is not thread safe
572  * if any other code can reach obj before this procedure ends.
573  *
574  * \note weakproxy may be previously existing, but must not currently
575  * have an object set.
576  *
577  * \note The only way to unset an object is for it to be destroyed.
578  * Any call to this function while an object is already set will fail.
579  */
580 int __ao2_weakproxy_set_object(void *weakproxy, void *obj, int flags,
581  const char *tag, const char *file, int line, const char *func);
582 
583 #define ao2_weakproxy_set_object(weakproxy, obj, flags) \
584  __ao2_weakproxy_set_object(weakproxy, obj, flags, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
585 
586 #define ao2_t_weakproxy_set_object(weakproxy, obj, flags, tag) \
587  __ao2_weakproxy_set_object(weakproxy, obj, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
588 
589 /*!
590  * \since 14.0.0
591  * \brief Run ao2_t_ref on the object associated with weakproxy.
592  *
593  * \param weakproxy The weakproxy to read from.
594  * \param delta Value to add to the reference counter.
595  * \param flags OBJ_NOLOCK to avoid locking weakproxy.
596  *
597  * \retval -2 weakproxy is not a valid ao2_weakproxy.
598  * \retval -1 weakproxy has no associated object.
599  *
600  * \return The value of the reference counter before the operation.
601  */
602 int __ao2_weakproxy_ref_object(void *weakproxy, int delta, int flags,
603  const char *tag, const char *file, int line, const char *func);
604 
605 #define ao2_t_weakproxy_ref_object(weakproxy, delta, flags, tag) \
606  __ao2_weakproxy_ref_object(weakproxy, delta, flags, \
607  tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
608 
609 #define ao2_weakproxy_ref_object(weakproxy, delta, flags) \
610  ao2_t_weakproxy_ref_object(weakproxy, delta, flags, NULL)
611 
612 /*!
613  * \since 14.0.0
614  * \brief Get the object associated with weakproxy.
615  *
616  * \param weakproxy The weakproxy to read from.
617  * \param flags OBJ_NOLOCK to avoid locking weakproxy.
618  *
619  * \return A reference to the object previously set by ao2_weakproxy_set_object.
620  * \retval NULL Either no object was set or the previously set object has been freed.
621  */
622 void *__ao2_weakproxy_get_object(void *weakproxy, int flags,
623  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
624 
625 #define ao2_weakproxy_get_object(weakproxy, flags) \
626  __ao2_weakproxy_get_object(weakproxy, flags, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
627 
628 #define ao2_t_weakproxy_get_object(weakproxy, flags, tag) \
629  __ao2_weakproxy_get_object(weakproxy, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
630 
631 /*!
632  * \since 14.0.0
633  * \brief Request notification when weakproxy points to NULL.
634  *
635  * \param weakproxy The weak object
636  * \param cb Procedure to call when no real object is associated
637  * \param data Passed to cb
638  * \param flags OBJ_NOLOCK to avoid locking weakproxy.
639  *
640  * \retval 0 Success
641  * \retval -1 Failure
642  *
643  * \note Callbacks are run in the reverse order of subscriptions.
644  *
645  * \note This procedure will allow the same cb / data pair to be added to
646  * the same weakproxy multiple times.
647  *
648  * \note It is the caller's responsibility to ensure that *data is valid
649  * until after cb() is run or ao2_weakproxy_unsubscribe is called.
650  *
651  * \note If the weakproxy currently points to NULL the callback will be run immediately,
652  * without being added to the subscriber list.
653  */
654 int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags);
655 
656 /*!
657  * \since 14.0.0
658  * \brief Remove notification of real object destruction.
659  *
660  * \param weakproxy The weak object
661  * \param cb Callback to remove from destroy notification list
662  * \param data Data pointer to match
663  * \param flags OBJ_NOLOCK to avoid locking weakproxy.
664  * OBJ_MULTIPLE to remove all copies of the same cb / data pair.
665  *
666  * \return The number of subscriptions removed.
667  * \retval 0 cb / data pair not found, nothing removed.
668  * \retval -1 Failure due to invalid parameters.
669  *
670  * \note Unless flags includes OBJ_MULTIPLE, this will only remove a single copy
671  * of the cb / data pair. If it was subscribed multiple times it must be
672  * unsubscribed as many times. The OBJ_MULTIPLE flag can be used to remove
673  * matching subscriptions.
674  *
675  * \note When it's time to run callbacks they are copied to a temporary list so the
676  * weakproxy can be unlocked before running. That means it's possible for
677  * this function to find nothing before the callback is run in another thread.
678  */
679 int ao2_weakproxy_unsubscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags);
680 
681 /*!
682  * \since 14.0.0
683  * \brief Get the weakproxy attached to obj
684  *
685  * \param obj The object to retrieve a weakproxy from
686  *
687  * \return The weakproxy object
688  */
689 void *__ao2_get_weakproxy(void *obj,
690  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
691 
692 #define ao2_get_weakproxy(obj) \
693  __ao2_get_weakproxy(obj, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
694 
695 #define ao2_t_get_weakproxy(obj, tag) \
696  __ao2_get_weakproxy(obj, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
697 /*! @} */
698 
699 
700 /*! \brief Which lock to request. */
702  /*! Request the mutex lock be acquired. */
704  /*! Request the read lock be acquired. */
706  /*! Request the write lock be acquired. */
708 };
709 
710 /*! \brief
711  * Lock an object.
712  *
713  * \param a A pointer to the object we want to lock.
714  * \param lock_how, file, func, line, var
715  * \return 0 on success, other values on error.
716  */
717 int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
718 #define ao2_lock(a) __ao2_lock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
719 #define ao2_rdlock(a) __ao2_lock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
720 #define ao2_wrlock(a) __ao2_lock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
721 
722 /*! \brief
723  * Unlock an object.
724  *
725  * \param a A pointer to the object we want unlock.
726  * \param file, func, line, var
727  * \return 0 on success, other values on error.
728  */
729 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
730 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
731 
732 /*! \brief
733  * Try locking-- (don't block if fail)
734  *
735  * \param a A pointer to the object we want to lock.
736  * \param lock_how, file, func, line, var
737  * \return 0 on success, other values on error.
738  */
739 int __ao2_trylock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
740 #define ao2_trylock(a) __ao2_trylock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
741 #define ao2_tryrdlock(a) __ao2_trylock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
742 #define ao2_trywrlock(a) __ao2_trylock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
743 
744 /*!
745  * \brief Return the mutex lock address of an object
746  *
747  * \param[in] obj A pointer to the object we want.
748  * \return the address of the mutex lock, else NULL.
749  *
750  * This function comes in handy mainly for debugging locking
751  * situations, where the locking trace code reports the
752  * lock address, this allows you to correlate against
753  * object address, to match objects to reported locks.
754  *
755  * \warning AO2 lock objects do not include tracking fields when
756  * DEBUG_THREADS is not enabled.
757  *
758  * \since 1.6.1
759  */
760 void *ao2_object_get_lockaddr(void *obj);
761 
762 
763 /*!
764  * \brief Increment reference count on an object and lock it
765  * \since 13.9.0
766  *
767  * \param[in] obj A pointer to the ao2 object
768  * \retval 0 The object is not an ao2 object or wasn't locked successfully
769  * \retval 1 The object's reference count was incremented and was locked
770  */
772 int ao2_ref_and_lock(void *obj),
773 {
774  ao2_ref(obj, +1);
775  if (ao2_lock(obj)) {
776  ao2_ref(obj, -1);
777  return 0;
778  }
779  return 1;
780 }
781 )
782 
783 /*!
784  * \brief Unlock an object and decrement its reference count
785  * \since 13.9.0
786  *
787  * \param[in] obj A pointer to the ao2 object
788  * \retval 0 The object is not an ao2 object or wasn't unlocked successfully
789  * \retval 1 The object was unlocked and it's reference count was decremented
790  */
792 int ao2_unlock_and_unref(void *obj),
793 {
794  if (ao2_unlock(obj)) {
795  return 0;
796  }
797  ao2_ref(obj, -1);
798 
799  return 1;
800 }
801 )
802 
803 /*! Global ao2 object holder structure. */
805  /*! Access lock to the held ao2 object. */
807  /*! Global ao2 object. */
808  void *obj;
809 };
810 
811 /*!
812  * \brief Define a global object holder to be used to hold an ao2 object, statically initialized.
813  * \since 11.0
814  *
815  * \param name This will be the name of the object holder.
816  *
817  * \details
818  * This macro creates a global object holder that can be used to
819  * hold an ao2 object accessible using the API. The structure is
820  * allocated and initialized to be empty.
821  *
822  * Example usage:
823  * \code
824  * static AO2_GLOBAL_OBJ_STATIC(global_cfg);
825  * \endcode
826  *
827  * This defines global_cfg, intended to hold an ao2 object
828  * accessible using an API.
829  */
830 #ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
831 #define AO2_GLOBAL_OBJ_STATIC(name) \
832  struct ao2_global_obj name; \
833  static void __attribute__((constructor)) __init_##name(void) \
834  { \
835  ast_rwlock_init(&name.lock); \
836  name.obj = NULL; \
837  } \
838  static void __attribute__((destructor)) __fini_##name(void) \
839  { \
840  if (name.obj) { \
841  ao2_ref(name.obj, -1); \
842  name.obj = NULL; \
843  } \
844  ast_rwlock_destroy(&name.lock); \
845  } \
846  struct __dummy_##name
847 #else
848 #define AO2_GLOBAL_OBJ_STATIC(name) \
849  struct ao2_global_obj name = { \
850  .lock = AST_RWLOCK_INIT_VALUE, \
851  }
852 #endif
853 
854 /*!
855  * \brief Release the ao2 object held in the global holder.
856  * \since 11.0
857  *
858  * \param holder Global ao2 object holder.
859  * \param tag used for debugging
860  *
861  * \return Nothing
862  */
863 #define ao2_t_global_obj_release(holder, tag) \
864  __ao2_global_obj_replace_unref(&holder, NULL, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
865 #define ao2_global_obj_release(holder) \
866  __ao2_global_obj_replace_unref(&holder, NULL, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
867 
868 /*!
869  * \brief Replace an ao2 object in the global holder.
870  * \since 11.0
871  *
872  * \param holder Global ao2 object holder.
873  * \param obj Object to put into the holder. Can be NULL.
874  * \param tag used for debugging
875  *
876  * \note This function automatically increases the reference
877  * count to account for the reference that the global holder now
878  * holds to the object.
879  *
880  * \retval Reference to previous global ao2 object stored.
881  * \retval NULL if no object available.
882  */
883 #define ao2_t_global_obj_replace(holder, obj, tag) \
884  __ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
885 #define ao2_global_obj_replace(holder, obj) \
886  __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
887 
888 void *__ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name) attribute_warn_unused_result;
889 
890 /*!
891  * \brief Replace an ao2 object in the global holder, throwing away any old object.
892  * \since 11.0
893  *
894  * \param holder Global ao2 object holder.
895  * \param obj Object to put into the holder. Can be NULL.
896  * \param tag used for debugging
897  *
898  * \note This function automatically increases the reference
899  * count to account for the reference that the global holder now
900  * holds to the object. It also decreases the reference count
901  * of any object being replaced.
902  *
903  * \retval 0 The global object was previously empty
904  * \retval 1 The global object was not previously empty
905  */
906 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
907  __ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
908 #define ao2_global_obj_replace_unref(holder, obj) \
909  __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
910 
911 int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name);
912 
913 /*!
914  * \brief Get a reference to the object stored in the global holder.
915  * \since 11.0
916  *
917  * \param holder Global ao2 object holder.
918  * \param tag used for debugging
919  *
920  * \retval Reference to current ao2 object stored in the holder.
921  * \retval NULL if no object available.
922  */
923 #define ao2_t_global_obj_ref(holder, tag) \
924  __ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
925 #define ao2_global_obj_ref(holder) \
926  __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
927 
928 void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name) attribute_warn_unused_result;
929 
930 
931 /*!
932  \page AstObj2_Containers AstObj2 Containers
933 
934 Containers are data structures meant to store several objects,
935 and perform various operations on them.
936 Internally, objects are stored in lists, hash tables or other
937 data structures depending on the needs.
938 
939 Operations on container include:
940 
941  - \b ao2_find(c, arg, flags)
942  returns zero or more elements matching a given criteria
943  (specified as arg). 'c' is the container pointer. Flags
944  can be:
945  OBJ_UNLINK - to remove the object, once found, from the container.
946  OBJ_NODATA - don't return the object if found (no ref count change)
947  OBJ_MULTIPLE - don't stop at first match
948  OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
949  search will be done. If not, a traversal is done.
950  OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
951  Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
952  OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
953  Similar to OBJ_SEARCH_KEY and mutually exclusive.
954 
955  - \b ao2_callback(c, flags, fn, arg)
956  apply fn(obj, arg) to all objects in the container.
957  Similar to find. fn() can tell when to stop, and
958  do anything with the object including unlinking it.
959  - c is the container;
960  - flags can be
961  OBJ_UNLINK - to remove the object, once found, from the container.
962  OBJ_NODATA - don't return the object if found (no ref count change)
963  OBJ_MULTIPLE - don't stop at first match
964  OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
965  search will be done. If not, a traversal is done through
966  all the hash table 'buckets'..
967  OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
968  Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
969  OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
970  Similar to OBJ_SEARCH_KEY and mutually exclusive.
971  - fn is a func that returns int, and takes 3 args:
972  (void *obj, void *arg, int flags);
973  obj is an object
974  arg is the same as arg passed into ao2_callback
975  flags is the same as flags passed into ao2_callback
976  fn returns:
977  0: no match, keep going
978  CMP_STOP: stop search, no match
979  CMP_MATCH: This object is matched.
980 
981  Note that the entire operation is run with the container
982  locked, so nobody else can change its content while we work on it.
983  However, we pay this with the fact that doing
984  anything blocking in the callback keeps the container
985  blocked.
986  The mechanism is very flexible because the callback function fn()
987  can do basically anything e.g. counting, deleting records, etc.
988  possibly using arg to store the results.
989 
990  - \b iterate on a container
991  this is done with the following sequence
992 
993 \code
994 
995  struct ao2_container *c = ... // our container
996  struct ao2_iterator i;
997  void *o;
998 
999  i = ao2_iterator_init(c, flags);
1000 
1001  while ((o = ao2_iterator_next(&i))) {
1002  ... do something on o ...
1003  ao2_ref(o, -1);
1004  }
1005 
1006  ao2_iterator_destroy(&i);
1007 \endcode
1008 
1009  The difference with the callback is that the control
1010  on how to iterate is left to us.
1011 
1012  - \b ao2_ref(c, -1)
1013  dropping a reference to a container destroys it, very simple!
1014 
1015 Containers are ao2 objects themselves, and this is why their
1016 implementation is simple too.
1017 
1018 Before declaring containers, we need to declare the types of the
1019 arguments passed to the constructor - in turn, this requires
1020 to define callback and hash functions and their arguments.
1021 
1022 - \ref AstObj2
1023 - \ref astobj2.h
1024  */
1025 
1026 /*! \brief
1027  * A callback function will return a combination of CMP_MATCH and CMP_STOP.
1028  * The latter will terminate the search in a container.
1029  */
1031  CMP_MATCH = 0x1, /*!< the object matches the request */
1032  CMP_STOP = 0x2, /*!< stop the search now */
1033 };
1034 
1035 /*!
1036  * \brief Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
1037  */
1039  /*!
1040  * Unlink the object for which the callback function returned
1041  * CMP_MATCH.
1042  */
1043  OBJ_UNLINK = (1 << 0),
1044  /*!
1045  * On match, don't return the object hence do not increase its
1046  * refcount.
1047  */
1048  OBJ_NODATA = (1 << 1),
1049  /*!
1050  * Don't stop at the first match in ao2_callback() unless the
1051  * result of the callback function has the CMP_STOP bit set.
1052  */
1053  OBJ_MULTIPLE = (1 << 2),
1054  /*!
1055  * \brief Assume that the ao2_container is already locked.
1056  *
1057  * \note For ao2_containers that have mutexes, no locking will
1058  * be done.
1059  *
1060  * \note For ao2_containers that have RWLOCKs, the lock will be
1061  * promoted to write mode as needed. The lock will be returned
1062  * to the original locked state.
1063  *
1064  * \note Only use this flag if the ao2_container is manually
1065  * locked already.
1066  */
1067  OBJ_NOLOCK = (1 << 4),
1068 
1069  /*!
1070  * \brief Search option field mask.
1071  *
1072  * \todo Eventually OBJ_SEARCH_MASK will shrink to a two bit
1073  * field when the codebase is made to use the search field
1074  * values as a field instead of independent bits.
1075  */
1076  OBJ_SEARCH_MASK = (0x07 << 5),
1077  /*! \brief The arg parameter has no meaning to the astobj2 code. */
1078  OBJ_SEARCH_NONE = (0 << 5),
1079  /*!
1080  * \brief The arg parameter is an object of the same type.
1081  *
1082  * \details
1083  * The arg parameter is an object of the same type as the one
1084  * being searched for, so use the object's ao2_hash_fn and/or
1085  * ao2_sort_fn functions for optimized searching.
1086  *
1087  * \note The supplied ao2_callback_fn is called after the
1088  * container nodes have been filtered by the ao2_hash_fn and/or
1089  * ao2_sort_fn functions.
1090  */
1091  OBJ_SEARCH_OBJECT = (1 << 5),
1092  /*!
1093  * \brief The arg parameter is a search key, but is not an object.
1094  *
1095  * \details
1096  * This can be used when you want to be able to pass custom data
1097  * to the container's stored ao2_hash_fn, ao2_sort_fn, and
1098  * ao2_find ao2_callback_fn functions that is not a full object,
1099  * but perhaps just a string.
1100  *
1101  * \note The supplied ao2_callback_fn is called after the
1102  * container nodes have been filtered by the ao2_hash_fn and/or
1103  * ao2_sort_fn functions.
1104  */
1105  OBJ_SEARCH_KEY = (2 << 5),
1106  /*!
1107  * \brief The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
1108  *
1109  * \details
1110  * The partial key can be used by the ao2_sort_fn to guide the
1111  * search to find a contiguous subset of a sorted container.
1112  * For example, a sorted container holds: "A", "B", "Bert",
1113  * "Beth", "Earnie". Doing a partial key search with "B" will
1114  * find the sorted subset of all held objects starting with "B".
1115  *
1116  * \note The supplied ao2_callback_fn is called after the
1117  * container nodes have been filtered by the ao2_sort_fn
1118  * function.
1119  */
1121 
1122  /*! \brief Traverse order option field mask. */
1123  OBJ_ORDER_MASK = (0x03 << 8),
1124  /*! \brief Traverse in ascending order (First to last container object) */
1126  /*! \brief Traverse in descending order (Last to first container object) */
1128  /*!
1129  * \brief Traverse in pre-order (Node then children, for tree container)
1130  *
1131  * \note For non-tree containers, it is up to the container type
1132  * to make the best interpretation of the order. For list and
1133  * hash containers, this also means ascending order because a
1134  * binary tree can degenerate into a list.
1135  */
1136  OBJ_ORDER_PRE = (2 << 8),
1137  /*!
1138  * \brief Traverse in post-order (Children then node, for tree container)
1139  *
1140  * \note For non-tree containers, it is up to the container type
1141  * to make the best interpretation of the order. For list and
1142  * hash containers, this also means descending order because a
1143  * binary tree can degenerate into a list.
1144  */
1145  OBJ_ORDER_POST = (3 << 8),
1146 };
1147 
1148 /*
1149  * Deprecated backward compatible flag names.
1150  *
1151  * Note: OBJ_POINTER, OBJ_KEY, and OBJ_PARTIAL_KEY are mutually
1152  * exclusive.
1153  */
1154 #define OBJ_POINTER OBJ_SEARCH_OBJECT /*!< Deprecated name */
1155 #define OBJ_KEY OBJ_SEARCH_KEY /*!< Deprecated name */
1156 #define OBJ_PARTIAL_KEY OBJ_SEARCH_PARTIAL_KEY /*!< Deprecated name */
1157 
1158 /*!
1159  * \brief Options available when allocating an ao2 container object.
1160  *
1161  * \note Each option is open to some interpretation by the
1162  * container type as long as it makes sense with the option
1163  * name.
1164  */
1166  /*!
1167  * \brief Insert objects at the beginning of the container.
1168  * (Otherwise it is the opposite; insert at the end.)
1169  *
1170  * \note If an ao2_sort_fn is provided, the object is inserted
1171  * before any objects with duplicate keys.
1172  *
1173  * \note Hash containers insert the object in the computed hash
1174  * bucket in the indicated manner.
1175  */
1177 
1178  /*!
1179  * \brief The ao2 container objects with duplicate keys option field mask.
1180  */
1182  /*!
1183  * \brief Allow objects with duplicate keys in container.
1184  */
1186  /*!
1187  * \brief Reject objects with duplicate keys in container.
1188  *
1189  * \note The container must be sorted. i.e. have an
1190  * ao2_sort_fn.
1191  */
1193  /*!
1194  * \brief Reject duplicate objects in container.
1195  *
1196  * \details Don't link the same object into the container twice.
1197  * However, you can link a different object with the same key.
1198  *
1199  * \note The container must be sorted. i.e. have an
1200  * ao2_sort_fn.
1201  *
1202  * \note It is assumed that the objects are located where the
1203  * search key says they should be located.
1204  */
1206  /*!
1207  * \brief Replace objects with duplicate keys in container.
1208  *
1209  * \details The existing duplicate object is removed and the new
1210  * object takes the old object's place.
1211  *
1212  * \note The container must be sorted. i.e. have an
1213  * ao2_sort_fn.
1214  */
1216 };
1217 
1218 /*!
1219  * \brief Type of a generic callback function
1220  * \param obj pointer to the (user-defined part) of an object.
1221  * \param arg callback argument from ao2_callback()
1222  * \param flags flags from ao2_callback()
1223  * OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
1224  * OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
1225  * OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
1226  *
1227  * The return values are a combination of enum _cb_results.
1228  * Callback functions are used to search or manipulate objects in a container.
1229  */
1230 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags);
1231 
1232 /*! \brief A common ao2_callback is one that matches by address. */
1233 int ao2_match_by_addr(void *obj, void *arg, int flags);
1234 
1235 /*!
1236  * \brief Type of a generic callback function
1237  * \param obj pointer to the (user-defined part) of an object.
1238  * \param arg callback argument from ao2_callback()
1239  * \param data arbitrary data from ao2_callback()
1240  * \param flags flags from ao2_callback()
1241  * OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
1242  * OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
1243  * OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
1244  *
1245  * The return values are a combination of enum _cb_results.
1246  * Callback functions are used to search or manipulate objects in a container.
1247  */
1248 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags);
1249 
1250 /*!
1251  * Type of a generic function to generate a hash value from an object.
1252  *
1253  * \param obj pointer to the (user-defined part) of an object.
1254  * \param flags flags from ao2_callback()
1255  * OBJ_SEARCH_OBJECT - if set, 'obj', is an object.
1256  * OBJ_SEARCH_KEY - if set, 'obj', is a search key item that is not an object.
1257  *
1258  * \note This function must be idempotent.
1259  *
1260  * \return Computed hash value.
1261  */
1262 typedef int (ao2_hash_fn)(const void *obj, int flags);
1263 
1264 /*!
1265  * \brief Type of generic container sort function.
1266  *
1267  * \param obj_left pointer to the (user-defined part) of an object.
1268  * \param obj_right pointer to the (user-defined part) of an object.
1269  * \param flags flags from ao2_callback()
1270  * OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
1271  * OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
1272  * OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
1273  *
1274  * \note This function must be idempotent.
1275  *
1276  * \retval <0 if obj_left < obj_right
1277  * \retval =0 if obj_left == obj_right
1278  * \retval >0 if obj_left > obj_right
1279  */
1280 typedef int (ao2_sort_fn)(const void *obj_left, const void *obj_right, int flags);
1281 
1282 /*! \name Object Containers
1283  * Here start declarations of containers.
1284  */
1285 /*@{ */
1286 struct ao2_container;
1287 
1288 /*!
1289  * \brief Allocate and initialize a hash container with the desired number of buckets.
1290  *
1291  * \details
1292  * We allocate space for a struct astobj_container, struct container
1293  * and the buckets[] array.
1294  *
1295  * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1296  * \param container_options Container behaviour options (See enum ao2_container_opts)
1297  * \param n_buckets Number of buckets for hash
1298  * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
1299  * \param sort_fn Pointer to a sort function. (NULL to not sort the buckets.)
1300  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1301  * \param tag used for debugging.
1302  *
1303  * \return A pointer to a struct container.
1304  *
1305  * \note Destructor is set implicitly.
1306  */
1307 
1308 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
1309  __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1310 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1311  __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1312 
1313 struct ao2_container *__ao2_container_alloc_hash(unsigned int ao2_options,
1314  unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
1316  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1317 
1318 /*!
1319  * \brief Allocate and initialize a list container.
1320  *
1321  * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1322  * \param container_options Container behaviour options (See enum ao2_container_opts)
1323  * \param sort_fn Pointer to a sort function. (NULL if list not sorted.)
1324  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1325  * \param tag used for debugging.
1326  *
1327  * \return A pointer to a struct container.
1328  *
1329  * \note Destructor is set implicitly.
1330  * \note Implemented as a degenerate hash table.
1331  */
1332 
1333 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1334  __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1335 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1336  __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1337 
1338 struct ao2_container *__ao2_container_alloc_list(unsigned int ao2_options,
1339  unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1340  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1341 
1342 /*!
1343  * \brief Allocate and initialize a red-black tree container.
1344  *
1345  * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1346  * \param container_options Container behaviour options (See enum ao2_container_opts)
1347  * \param sort_fn Pointer to a sort function.
1348  * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1349  * \param tag used for debugging.
1350  *
1351  * \return A pointer to a struct container.
1352  *
1353  * \note Destructor is set implicitly.
1354  */
1355 
1356 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1357  __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1358 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1359  __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1360 
1361 struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
1363  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1364 
1365 /*! \brief
1366  * Returns the number of elements in a container.
1367  */
1368 int ao2_container_count(struct ao2_container *c);
1369 
1370 /*!
1371  * \brief Copy all object references in the src container into the dest container.
1372  * \since 11.0
1373  *
1374  * \param dest Container to copy src object references into.
1375  * \param src Container to copy all object references from.
1376  * \param flags OBJ_NOLOCK if a lock is already held on both containers.
1377  * Otherwise, the src container is locked first.
1378  *
1379  * \pre The dest container must be empty. If the duplication fails, the
1380  * dest container will be returned empty.
1381  *
1382  * \note This can potentially be expensive because a malloc is
1383  * needed for every object in the src container.
1384  *
1385  * \retval 0 on success.
1386  * \retval -1 on error.
1387  */
1388 int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags);
1389 
1390 /*!
1391  * \brief Copy object references associated with src container weakproxies into the dest container.
1392  *
1393  * \param dest Container to copy src strong object references into.
1394  * \param src Container to copy all weak object references from.
1395  * \param flags OBJ_NOLOCK if a lock is already held on both containers.
1396  * Otherwise, the src container is locked first.
1397  *
1398  * \pre The dest container must be empty. If the duplication fails, the
1399  * dest container will be returned empty.
1400  *
1401  * \note This can potentially be expensive because a malloc is
1402  * needed for every object in the src container.
1403  *
1404  * \note Every object inside the container is locked by \ref ao2_weakproxy_get_object.
1405  * Any weakproxy in \ref src with no associated object is ignored.
1406  *
1407  * \retval 0 on success.
1408  * \retval -1 on error.
1409  */
1410 int ao2_container_dup_weakproxy_objs(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags);
1411 
1412 /*!
1413  * \brief Create a clone/copy of the given container.
1414  * \since 11.0
1415  *
1416  * \param orig Container to copy all object references from.
1417  * \param flags OBJ_NOLOCK if a lock is already held on the container.
1418  *
1419  * \note This can potentially be expensive because a malloc is
1420  * needed for every object in the orig container.
1421  *
1422  * \retval Clone container on success.
1423  * \retval NULL on error.
1424  */
1425 struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags,
1426  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1427 
1428 #define ao2_t_container_clone(orig, flags, tag) \
1429  __ao2_container_clone(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1430 #define ao2_container_clone(orig, flags) \
1431  __ao2_container_clone(orig, flags, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1432 
1433 /*!
1434  * \brief Print output.
1435  * \since 12.0.0
1436  *
1437  * \param where User data pointer needed to determine where to put output.
1438  * \param fmt printf type format string.
1439  *
1440  * \return Nothing
1441  */
1442 typedef void (ao2_prnt_fn)(void *where, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
1443 
1444 /*!
1445  * \brief Print object key.
1446  * \since 12.0.0
1447  *
1448  * \param v_obj A pointer to the object we want the key printed.
1449  * \param where User data needed by prnt to determine where to put output.
1450  * \param prnt Print output callback function to use.
1451  *
1452  * \return Nothing
1453  */
1454 typedef void (ao2_prnt_obj_fn)(void *v_obj, void *where, ao2_prnt_fn *prnt);
1455 
1456 /*!
1457  * \brief Display contents of the specified container.
1458  * \since 12.0.0
1459  *
1460  * \param self Container to dump.
1461  * \param flags OBJ_NOLOCK if a lock is already held on the container.
1462  * \param name Container name. (NULL if anonymous)
1463  * \param where User data needed by prnt to determine where to put output.
1464  * \param prnt Print output callback function to use.
1465  * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
1466  *
1467  * \return Nothing
1468  */
1469 void ao2_container_dump(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj);
1470 
1471 /*!
1472  * \brief Display statistics of the specified container.
1473  * \since 12.0.0
1474  *
1475  * \param self Container to display statistics.
1476  * \param flags OBJ_NOLOCK if a lock is already held on the container.
1477  * \param name Container name. (NULL if anonymous)
1478  * \param where User data needed by prnt to determine where to put output.
1479  * \param prnt Print output callback function to use.
1480  *
1481  * \return Nothing
1482  */
1483 void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt);
1484 
1485 /*!
1486  * \brief Perform an integrity check on the specified container.
1487  * \since 12.0.0
1488  *
1489  * \param self Container to check integrity.
1490  * \param flags OBJ_NOLOCK if a lock is already held on the container.
1491  *
1492  * \retval 0 on success.
1493  * \retval -1 on error.
1494  */
1495 int ao2_container_check(struct ao2_container *self, enum search_flags flags);
1496 
1497 /*!
1498  * \brief Register a container for CLI stats and integrity check.
1499  * \since 12.0.0
1500  *
1501  * \param name Name to register the container under.
1502  * \param self Container to register.
1503  * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
1504  *
1505  * \retval 0 on success.
1506  * \retval -1 on error.
1507  */
1508 int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj);
1509 
1510 /*!
1511  * \brief Unregister a container for CLI stats and integrity check.
1512  * \since 12.0.0
1513  *
1514  * \param name Name the container is registered under.
1515  *
1516  * \return Nothing
1517  */
1518 void ao2_container_unregister(const char *name);
1519 
1520 /*@} */
1521 
1522 /*! \name Object Management
1523  * Here we have functions to manage objects.
1524  *
1525  * We can use the functions below on any kind of
1526  * object defined by the user.
1527  */
1528 /*@{ */
1529 
1530 /*!
1531  * \brief Add an object to a container.
1532  *
1533  * \param container The container to operate on.
1534  * \param obj The object to be added.
1535  * \param tag used for debugging.
1536  *
1537  * \retval 0 on errors.
1538  * \retval 1 on success.
1539  *
1540  * This function inserts an object in a container according its key.
1541  *
1542  * \note Remember to set the key before calling this function.
1543  *
1544  * \note This function automatically increases the reference count to account
1545  * for the reference that the container now holds to the object.
1546  */
1547 #define ao2_t_link(container, obj, tag) \
1548  __ao2_link((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1549 #define ao2_link(container, obj) \
1550  __ao2_link((container), (obj), 0, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1551 
1552 /*!
1553  * \brief Add an object to a container.
1554  *
1555  * \param container The container to operate on.
1556  * \param obj The object to be added.
1557  * \param flags search_flags to control linking the object. (OBJ_NOLOCK)
1558  * \param tag used for debugging.
1559  *
1560  * \retval 0 on errors.
1561  * \retval 1 on success.
1562  *
1563  * This function inserts an object in a container according its key.
1564  *
1565  * \note Remember to set the key before calling this function.
1566  *
1567  * \note This function automatically increases the reference count to account
1568  * for the reference that the container now holds to the object.
1569  */
1570 #define ao2_t_link_flags(container, obj, flags, tag) \
1571  __ao2_link((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1572 #define ao2_link_flags(container, obj, flags) \
1573  __ao2_link((container), (obj), (flags), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1574 
1575 int __ao2_link(struct ao2_container *c, void *obj_new, int flags,
1576  const char *tag, const char *file, int line, const char *func);
1577 
1578 /*!
1579  * \brief Remove an object from a container
1580  *
1581  * \param container The container to operate on.
1582  * \param obj The object to unlink.
1583  * \param tag used for debugging.
1584  *
1585  * \retval NULL, always
1586  *
1587  * \note The object requested to be unlinked must be valid. However, if it turns
1588  * out that it is not in the container, this function is still safe to
1589  * be called.
1590  *
1591  * \note If the object gets unlinked from the container, the container's
1592  * reference to the object will be automatically released. (The
1593  * refcount will be decremented).
1594  */
1595 
1596 #define ao2_t_unlink(container, obj, tag) \
1597  __ao2_unlink((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1598 #define ao2_unlink(container, obj) \
1599  __ao2_unlink((container), (obj), 0, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1600 
1601 /*!
1602  * \brief Remove an object from a container
1603  *
1604  * \param container The container to operate on.
1605  * \param obj The object to unlink.
1606  * \param flags search_flags to control unlinking the object. (OBJ_NOLOCK)
1607  * \param tag used for debugging.
1608  *
1609  * \retval NULL, always
1610  *
1611  * \note The object requested to be unlinked must be valid. However, if it turns
1612  * out that it is not in the container, this function is still safe to
1613  * be called.
1614  *
1615  * \note If the object gets unlinked from the container, the container's
1616  * reference to the object will be automatically released. (The
1617  * refcount will be decremented).
1618  */
1619 
1620 #define ao2_t_unlink_flags(container, obj, flags, tag) \
1621  __ao2_unlink((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1622 #define ao2_unlink_flags(container, obj, flags) \
1623  __ao2_unlink((container), (obj), (flags), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1624 
1625 void *__ao2_unlink(struct ao2_container *c, void *obj, int flags,
1626  const char *tag, const char *file, int line, const char *func);
1627 
1628 /*@} */
1629 
1630 
1631 /*! \brief
1632  * ao2_callback() is a generic function that applies cb_fn() to all objects
1633  * in a container, as described below.
1634  *
1635  * \param c A pointer to the container to operate on.
1636  * \param flags A set of flags specifying the operation to perform,
1637  * partially used by the container code, but also passed to
1638  * the callback.
1639  * - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts
1640  * of any of the traversed objects will be incremented.
1641  * On the converse, if it is NOT set (the default), the ref count
1642  * of the first matching object will be incremented and returned.
1643  * - If OBJ_MULTIPLE is set, the ref count of all matching objects will
1644  * be incremented in an iterator for a temporary container and returned.
1645  * - If OBJ_SEARCH_OBJECT is set, the traversed items will be restricted
1646  * to the objects in the bucket that the object key hashes to.
1647  * - If OBJ_SEARCH_KEY is set, the traversed items will be restricted
1648  * to the objects in the bucket that the object key hashes to.
1649  * \param cb_fn A function pointer, that will be called on all
1650  * objects, to see if they match. This function returns CMP_MATCH
1651  * if the object is matches the criteria; CMP_STOP if the traversal
1652  * should immediately stop, or both (via bitwise ORing), if you find a
1653  * match and want to end the traversal, and 0 if the object is not a match,
1654  * but the traversal should continue. This is the function that is applied
1655  * to each object traversed. Its arguments are:
1656  * (void *obj, void *arg, int flags), where:
1657  * obj is an object
1658  * arg is the same as arg passed into ao2_callback
1659  * flags is the same as flags passed into ao2_callback (flags are
1660  * also used by ao2_callback).
1661  * \param arg passed to the callback.
1662  * \param tag used for debugging.
1663  *
1664  * \retval NULL on failure or no matching object found.
1665  *
1666  * \retval object found if OBJ_MULTIPLE is not set in the flags
1667  * parameter.
1668  *
1669  * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the
1670  * flags parameter. The iterator must be destroyed with
1671  * ao2_iterator_destroy() when the caller no longer needs it.
1672  *
1673  * If the function returns any objects, their refcount is incremented,
1674  * and the caller is in charge of decrementing them once done.
1675  *
1676  * Typically, ao2_callback() is used for two purposes:
1677  * - to perform some action (including removal from the container) on one
1678  * or more objects; in this case, cb_fn() can modify the object itself,
1679  * and to perform deletion should set CMP_MATCH on the matching objects,
1680  * and have OBJ_UNLINK set in flags.
1681  * - to look for a specific object in a container; in this case, cb_fn()
1682  * should not modify the object, but just return a combination of
1683  * CMP_MATCH and CMP_STOP on the desired object.
1684  * Other usages are also possible, of course.
1685  *
1686  * This function searches through a container and performs operations
1687  * on objects according on flags passed.
1688  * XXX describe better
1689  * The comparison is done calling the compare function set implicitly.
1690  * The arg pointer can be a pointer to an object or to a key,
1691  * we can say this looking at flags value.
1692  * If arg points to an object we will search for the object pointed
1693  * by this value, otherwise we search for a key value.
1694  * If the key is not unique we only find the first matching value.
1695  *
1696  * The use of flags argument is the follow:
1697  *
1698  * OBJ_UNLINK unlinks the object found
1699  * OBJ_NODATA on match, do not return an object
1700  * Callbacks use OBJ_NODATA as a default
1701  * functions such as find() do
1702  * OBJ_MULTIPLE return multiple matches
1703  * Default is no.
1704  * OBJ_SEARCH_OBJECT the pointer is to an object
1705  * OBJ_SEARCH_KEY the pointer is to a search key
1706  * OBJ_SEARCH_PARTIAL_KEY the pointer is to a partial search key
1707  *
1708  * \note When the returned object is no longer in use, ao2_ref() should
1709  * be used to free the additional reference possibly created by this function.
1710  *
1711  * @{
1712  */
1713 
1714 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
1715  __ao2_callback((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1716 #define ao2_callback(c, flags, cb_fn, arg) \
1717  __ao2_callback((c), (flags), (cb_fn), (arg), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1718 
1719 void *__ao2_callback(struct ao2_container *c, enum search_flags flags,
1720  ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line,
1721  const char *func);
1722 
1723 /*! @} */
1724 
1725 /*! \brief
1726  * ao2_callback_data() is a generic function that applies cb_fn() to all objects
1727  * in a container. It is functionally identical to ao2_callback() except that
1728  * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and
1729  * allows the caller to pass in arbitrary data.
1730  *
1731  * This call would be used instead of ao2_callback() when the caller needs to pass
1732  * OBJ_SEARCH_OBJECT, OBJ_SEARCH_KEY, or OBJ_SEARCH_PARTIAL_KEY as part of the flags
1733  * argument (which in turn requires passing in a known pointer type for 'arg') and
1734  * also needs access to other non-global data to complete it's comparison or task.
1735  *
1736  * See the documentation for ao2_callback() for argument descriptions.
1737  *
1738  * \see ao2_callback()
1739  */
1740 
1741 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
1742  __ao2_callback_data((container), (flags), (cb_fn), (arg), (data), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1743 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
1744  __ao2_callback_data((container), (flags), (cb_fn), (arg), (data), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1745 
1746 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
1747  ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file,
1748  int line, const char *func);
1749 
1750 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg)
1751  * XXX possibly change order of arguments ?
1752  */
1753 
1754 #define ao2_t_find(container, arg, flags, tag) \
1755  __ao2_find((container), (arg), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1756 #define ao2_find(container, arg, flags) \
1757  __ao2_find((container), (arg), (flags), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1758 
1759 void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags,
1760  const char *tag, const char *file, int line, const char *func);
1761 
1762 /*!
1763  * \brief Perform an ao2_find on a container with ao2_weakproxy objects, returning the real object.
1764  *
1765  * \note Only OBJ_SEARCH_* and OBJ_NOLOCK flags are supported by this function.
1766  * \see ao2_callback for description of arguments.
1767  */
1768 #define ao2_weakproxy_find(c, arg, flags, tag) \
1769  __ao2_weakproxy_find(c, arg, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1770 void *__ao2_weakproxy_find(struct ao2_container *c, const void *arg, enum search_flags flags,
1771  const char *tag, const char *file, int line, const char *func);
1772 
1773 /*! \brief
1774  *
1775  *
1776  * When we need to walk through a container, we use an
1777  * ao2_iterator to keep track of the current position.
1778  *
1779  * Because the navigation is typically done without holding the
1780  * lock on the container across the loop, objects can be
1781  * inserted or deleted or moved while we work. As a
1782  * consequence, there is no guarantee that we manage to touch
1783  * all the elements in the container, and it is possible that we
1784  * touch the same object multiple times.
1785  *
1786  * An iterator must be first initialized with
1787  * ao2_iterator_init(), then we can use o = ao2_iterator_next()
1788  * to move from one element to the next. Remember that the
1789  * object returned by ao2_iterator_next() has its refcount
1790  * incremented, and the reference must be explicitly released
1791  * when done with it.
1792  *
1793  * In addition, ao2_iterator_init() will hold a reference to the
1794  * container being iterated and the last container node found.
1795  * Thes objects will be unreffed when ao2_iterator_destroy() is
1796  * called to free up the resources used by the iterator (if
1797  * any).
1798  *
1799  * Example:
1800  *
1801  * \code
1802  *
1803  * struct ao2_container *c = ... // the container we want to iterate on
1804  * struct ao2_iterator i;
1805  * struct my_obj *o;
1806  *
1807  * i = ao2_iterator_init(c, flags);
1808  *
1809  * while ((o = ao2_iterator_next(&i))) {
1810  * ... do something on o ...
1811  * ao2_ref(o, -1);
1812  * }
1813  *
1814  * ao2_iterator_restart(&i);
1815  * while ((o = ao2_iterator_next(&i))) {
1816  * ... do something on o ...
1817  * ao2_ref(o, -1);
1818  * }
1819  *
1820  * ao2_iterator_destroy(&i);
1821  *
1822  * \endcode
1823  *
1824  */
1825 
1826 /*!
1827  * \brief The astobj2 iterator
1828  *
1829  * \note You are not supposed to know the internals of an iterator!
1830  * We would like the iterator to be opaque, unfortunately
1831  * its size needs to be known if we want to store it around
1832  * without too much trouble.
1833  * Anyways...
1834  * The iterator has a pointer to the container, and a flags
1835  * field specifying various things e.g. whether the container
1836  * should be locked or not while navigating on it.
1837  * The iterator "points" to the current container node.
1838  *
1839  * Details are in the implementation of ao2_iterator_next()
1840  */
1842  /*! The container (Has a reference) */
1843  struct ao2_container *c;
1844  /*! Last container node (Has a reference) */
1845  void *last_node;
1846  /*! Nonzero if the iteration has completed. */
1848  /*! operation flags (enum ao2_iterator_flags) */
1849  int flags;
1850 };
1851 
1852 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
1853  * of the iterator.
1854  */
1856  /*!
1857  * \brief Assume that the ao2_container is already locked.
1858  *
1859  * \note For ao2_containers that have mutexes, no locking will
1860  * be done.
1861  *
1862  * \note For ao2_containers that have RWLOCKs, the lock will be
1863  * promoted to write mode as needed. The lock will be returned
1864  * to the original locked state.
1865  *
1866  * \note Only use this flag if the ao2_container is manually
1867  * locked already. You should hold the lock until after
1868  * ao2_iterator_destroy(). If you must release the lock then
1869  * you must at least hold the lock whenever you call an
1870  * ao2_iterator_xxx function with this iterator.
1871  */
1873  /*!
1874  * Indicates that the iterator was dynamically allocated by
1875  * astobj2 API and should be freed by ao2_iterator_destroy().
1876  */
1878  /*!
1879  * Indicates that before the iterator returns an object from
1880  * the container being iterated, the object should be unlinked
1881  * from the container.
1882  */
1884  /*!
1885  * Iterate in descending order (Last to first container object)
1886  * (Otherwise ascending order)
1887  *
1888  * \note Other traversal orders such as pre-order and post-order
1889  * do not make sense because they require the container
1890  * structure to be static during the traversal. Iterators just
1891  * about guarantee that is not going to happen because the
1892  * container is allowed to change by other threads during the
1893  * iteration.
1894  */
1896 };
1897 
1898 /*!
1899  * \brief Create an iterator for a container
1900  *
1901  * \param c the container
1902  * \param flags one or more flags from ao2_iterator_flags.
1903  *
1904  * \retval the constructed iterator
1905  *
1906  * \note This function does \b not take a pointer to an iterator;
1907  * rather, it returns an iterator structure that should be
1908  * assigned to (overwriting) an existing iterator structure
1909  * allocated on the stack or on the heap.
1910  *
1911  * This function will take a reference on the container being iterated.
1912  */
1913 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result;
1914 
1915 /*!
1916  * \brief Destroy a container iterator
1917  *
1918  * \param iter the iterator to destroy
1919  *
1920  * \retval none
1921  *
1922  * This function will release the container reference held by the iterator
1923  * and any other resources it may be holding.
1924  */
1925 #if defined(TEST_FRAMEWORK)
1926 void ao2_iterator_destroy(struct ao2_iterator *iter) __attribute__((noinline));
1927 #else
1928 void ao2_iterator_destroy(struct ao2_iterator *iter);
1929 #endif /* defined(TEST_FRAMEWORK) */
1930 
1931 #define ao2_t_iterator_next(iter, tag) \
1932  __ao2_iterator_next((iter), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1933 #define ao2_iterator_next(iter) \
1934  __ao2_iterator_next((iter), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1935 
1936 void *__ao2_iterator_next(struct ao2_iterator *iter,
1937  const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1938 
1939 /*!
1940  * \brief Restart an iteration.
1941  *
1942  * \param iter the iterator to restart
1943  *
1944  * \note A restart is not going to have any effect if the
1945  * iterator was created with the AO2_ITERATOR_UNLINK flag. Any
1946  * previous objects returned were removed from the container.
1947  *
1948  * \retval none
1949  */
1950 void ao2_iterator_restart(struct ao2_iterator *iter);
1951 
1952 /*! gcc __attribute__(cleanup()) functions
1953  * \note they must be able to handle NULL parameters because most of the
1954  * allocation/find functions can fail and we don't want to try to tear
1955  * down a NULL */
1956 void __ao2_cleanup(void *obj);
1957 void __ao2_cleanup_debug(void *obj, const char *tag, const char *file, int line, const char *function);
1958 #define ao2_cleanup(obj) __ao2_cleanup_debug((obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1959 #define ao2_t_cleanup(obj, tag) __ao2_cleanup_debug((obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1960 void ao2_iterator_cleanup(struct ao2_iterator *iter);
1961 
1962 /*!
1963  * \brief Get a count of the iterated container objects.
1964  *
1965  * \param iter the iterator to query
1966  *
1967  * \retval The number of objects in the iterated container
1968  */
1969 int ao2_iterator_count(struct ao2_iterator *iter);
1970 
1971 /*!
1972  * \brief Creates a hash function for a structure field.
1973  * \param stype The structure type
1974  * \param field The string field in the structure to hash
1975  * \param hash_fn Function which hashes the field
1976  *
1977  * AO2_FIELD_HASH_FN(mystruct, myfield, ast_str_hash) will
1978  * produce a function named mystruct_hash_fn which hashes
1979  * mystruct->myfield with ast_str_hash.
1980  */
1981 #define AO2_FIELD_HASH_FN(stype, field, hash_fn) \
1982 static int stype ## _hash_fn(const void *obj, const int flags) \
1983 { \
1984  const struct stype *object = obj; \
1985  const char *key; \
1986  switch (flags & OBJ_SEARCH_MASK) { \
1987  case OBJ_SEARCH_KEY: \
1988  key = obj; \
1989  break; \
1990  case OBJ_SEARCH_OBJECT: \
1991  key = object->field; \
1992  break; \
1993  default: \
1994  ast_assert(0); \
1995  return 0; \
1996  } \
1997  return hash_fn(key); \
1998 }
1999 
2000 
2001 #define AO2_FIELD_TRANSFORM_CMP_FN(cmp) ((cmp) ? 0 : CMP_MATCH)
2002 #define AO2_FIELD_TRANSFORM_SORT_FN(cmp) (cmp)
2003 
2004 /*!
2005  * \internal
2006  *
2007  * \brief Creates a compare function for a structure string field.
2008  * \param stype The structure type
2009  * \param fn_suffix Function name suffix
2010  * \param field The string field in the structure to compare
2011  * \param key_cmp Key comparison function like strcmp
2012  * \param partial_key_cmp Partial key comparison function like strncmp
2013  * \param transform A macro that takes the cmp result as an argument
2014  * and transforms it to a return value.
2015  *
2016  * Do not use this macro directly, instead use macro's starting with
2017  * AST_STRING_FIELD.
2018  *
2019  * \warning The macro is an internal implementation detail, the API
2020  * may change at any time.
2021  */
2022 #define AO2_FIELD_CMP_FN(stype, fn_suffix, field, key_cmp, partial_key_cmp, transform, argconst) \
2023 static int stype ## fn_suffix(argconst void *obj, argconst void *arg, int flags) \
2024 { \
2025  const struct stype *object_left = obj, *object_right = arg; \
2026  const char *right_key = arg; \
2027  int cmp; \
2028  switch (flags & OBJ_SEARCH_MASK) { \
2029  case OBJ_SEARCH_OBJECT: \
2030  right_key = object_right->field; \
2031  case OBJ_SEARCH_KEY: \
2032  cmp = key_cmp(object_left->field, right_key); \
2033  break; \
2034  case OBJ_SEARCH_PARTIAL_KEY: \
2035  cmp = partial_key_cmp(object_left->field, right_key, strlen(right_key)); \
2036  break; \
2037  default: \
2038  cmp = 0; \
2039  break; \
2040  } \
2041  return transform(cmp); \
2042 }
2043 
2044 /*!
2045  * \brief Creates a hash function for a structure string field.
2046  * \param stype The structure type
2047  * \param field The string field in the structure to hash
2048  *
2049  * AO2_STRING_FIELD_HASH_FN(mystruct, myfield) will produce a function
2050  * named mystruct_hash_fn which hashes mystruct->myfield.
2051  *
2052  * AO2_STRING_FIELD_HASH_FN(mystruct, myfield) would do the same except
2053  * it uses the hash function which ignores case.
2054  */
2055 #define AO2_STRING_FIELD_HASH_FN(stype, field) \
2056  AO2_FIELD_HASH_FN(stype, field, ast_str_hash)
2057 #define AO2_STRING_FIELD_CASE_HASH_FN(stype, field) \
2058  AO2_FIELD_HASH_FN(stype, field, ast_str_case_hash)
2059 
2060 /*!
2061  * \brief Creates a compare function for a structure string field.
2062  * \param stype The structure type
2063  * \param field The string field in the structure to compare
2064  *
2065  * AO2_STRING_FIELD_CMP_FN(mystruct, myfield) will produce a function
2066  * named mystruct_cmp_fn which compares mystruct->myfield.
2067  *
2068  * AO2_STRING_FIELD_CASE_CMP_FN(mystruct, myfield) would do the same
2069  * except it performs case insensitive comparisons.
2070  */
2071 #define AO2_STRING_FIELD_CMP_FN(stype, field) \
2072  AO2_FIELD_CMP_FN(stype, _cmp_fn, field, strcmp, strncmp, AO2_FIELD_TRANSFORM_CMP_FN,)
2073 #define AO2_STRING_FIELD_CASE_CMP_FN(stype, field) \
2074  AO2_FIELD_CMP_FN(stype, _cmp_fn, field, strcasecmp, strncasecmp, AO2_FIELD_TRANSFORM_CMP_FN,)
2075 
2076 /*!
2077  * \brief Creates a sort function for a structure string field.
2078  * \param stype The structure type
2079  * \param field The string field in the structure to compare
2080  *
2081  * AO2_STRING_FIELD_SORT_FN(mystruct, myfield) will produce a function
2082  * named mystruct_sort_fn which compares mystruct->myfield.
2083  *
2084  * AO2_STRING_FIELD_CASE_SORT_FN(mystruct, myfield) would do the same
2085  * except it performs case insensitive comparisons.
2086  */
2087 #define AO2_STRING_FIELD_SORT_FN(stype, field) \
2088  AO2_FIELD_CMP_FN(stype, _sort_fn, field, strcmp, strncmp, AO2_FIELD_TRANSFORM_SORT_FN, const)
2089 #define AO2_STRING_FIELD_CASE_SORT_FN(stype, field) \
2090  AO2_FIELD_CMP_FN(stype, _sort_fn, field, strcasecmp, strncasecmp, AO2_FIELD_TRANSFORM_SORT_FN, const)
2091 
2092 #endif /* _ASTERISK_ASTOBJ2_H */
void * __ao2_weakproxy_alloc(size_t data_size, ao2_destructor_fn destructor_fn, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Allocate an ao2_weakproxy object.
Definition: astobj2.c:790
void ao2_container_dump(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj)
Display contents of the specified container.
Asterisk locking-related definitions:
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
void * last_node
Definition: astobj2.h:1845
void(* ao2_weakproxy_notification_cb)(void *weakproxy, void *data)
Definition: astobj2.h:527
ao2_lock_req
Which lock to request.
Definition: astobj2.h:701
void __ao2_cleanup_debug(void *obj, const char *tag, const char *file, int line, const char *function)
Definition: astobj2.c:667
int ao2_weakproxy_unsubscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags)
Remove notification of real object destruction.
Definition: astobj2.c:970
void * __ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags, const char *tag, const char *file, int line, const char *func)
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var)
Lock an object.
Definition: astobj2.c:222
Allow objects with duplicate keys in container.
Definition: astobj2.h:1185
void ao2_iterator_cleanup(struct ao2_iterator *iter)
void __ao2_cleanup(void *obj)
Definition: astobj2.c:674
int ao2_container_dup_weakproxy_objs(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
Copy object references associated with src container weakproxies into the dest container.
void * __ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name) attribute_warn_unused_result
int ao2_ref_and_lock(void *obj)
Increment reference count on an object and lock it.
Definition: astobj2.h:781
#define var
Definition: ast_expr2f.c:614
void ao2_iterator_restart(struct ao2_iterator *iter)
Restart an iteration.
void * __ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Definition: astobj2.c:765
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 __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var)
Unlock an object.
Definition: astobj2.c:288
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
void * __ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line, const char *func)
int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags)
Request notification when weakproxy points to NULL.
Definition: astobj2.c:931
#define ao2_unlock(a)
Definition: astobj2.h:730
static struct test_val c
search_flags
Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
Definition: astobj2.h:1038
void * __ao2_get_weakproxy(void *obj, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Get the weakproxy attached to obj.
Definition: astobj2.c:914
int complete
Definition: astobj2.h:1847
void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt)
Display statistics of the specified container.
struct ao2_container * __ao2_container_clone(struct ao2_container *orig, enum search_flags flags, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Create a clone/copy of the given container.
int() ao2_callback_data_fn(void *obj, void *arg, void *data, int flags)
Type of a generic callback function.
Definition: astobj2.h:1248
struct ao2_container * c
Definition: astobj2.h:1843
The arg parameter has no meaning to the astobj2 code.
Definition: astobj2.h:1078
Insert objects at the beginning of the container. (Otherwise it is the opposite; insert at the end...
Definition: astobj2.h:1176
void() ao2_prnt_fn(void *where, const char *fmt,...)
Print output.
Definition: astobj2.h:1442
void * __ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name) attribute_warn_unused_result
void * ao2_object_get_lockaddr(void *obj)
Return the mutex lock address of an object.
Definition: astobj2.c:476
Traverse in ascending order (First to last container object)
Definition: astobj2.h:1125
int ao2_container_check(struct ao2_container *self, enum search_flags flags)
Perform an integrity check on the specified container.
void ao2_container_unregister(const char *name)
Unregister a container for CLI stats and integrity check.
int __ao2_ref(void *o, int delta, const char *tag, const char *file, int line, const char *func)
Definition: astobj2.c:498
int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj)
Register a container for CLI stats and integrity check.
int() ao2_callback_fn(void *obj, void *arg, int flags)
Type of a generic callback function.
Definition: astobj2.h:1230
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
int __ao2_weakproxy_ref_object(void *weakproxy, int delta, int flags, const char *tag, const char *file, int line, const char *func)
Run ao2_t_ref on the object associated with weakproxy.
Definition: astobj2.c:859
The ao2 container objects with duplicate keys option field mask.
Definition: astobj2.h:1181
Inlinable API function macro.
struct ao2_container * __ao2_container_alloc_hash(unsigned int ao2_options, unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void * __ao2_weakproxy_get_object(void *weakproxy, int flags, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Get the object associated with weakproxy.
Definition: astobj2.c:886
#define ao2_lock(a)
Definition: astobj2.h:718
A set of macros to manage forward-linked lists.
void * __ao2_weakproxy_find(struct ao2_container *c, const void *arg, enum search_flags flags, const char *tag, const char *file, int line, const char *func)
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
Traverse in descending order (Last to first container object)
Definition: astobj2.h:1127
struct ao2_weakproxy::@224 destroyed_cb
int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name)
int __ao2_weakproxy_set_object(void *weakproxy, void *obj, int flags, const char *tag, const char *file, int line, const char *func)
Associate weakproxy with obj.
Definition: astobj2.c:815
ao2_alloc_opts
Options available when allocating an ao2 object.
Definition: astobj2.h:363
int() ao2_hash_fn(const void *obj, int flags)
Definition: astobj2.h:1262
void * __ao2_callback_data(struct ao2_container *c, enum search_flags flags, ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file, int line, const char *func)
int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
Copy all object references in the src container into the dest container.
void * __ao2_alloc_with_lockobj(size_t data_size, ao2_destructor_fn destructor_fn, void *lockobj, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Definition: astobj2.c:771
void * obj
Definition: astobj2.h:808
int __ao2_trylock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var)
Try locking– (don&#39;t block if fail)
Definition: astobj2.c:342
void(* ao2_destructor_fn)(void *vdoomed)
Typedef for an object destructor.
Definition: astobj2.h:360
Traverse in pre-order (Node then children, for tree container)
Definition: astobj2.h:1136
_cb_results
A callback function will return a combination of CMP_MATCH and CMP_STOP. The latter will terminate th...
Definition: astobj2.h:1030
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
static const char name[]
Definition: cdr_mysql.c:74
int ao2_match_by_addr(void *obj, void *arg, int flags)
A common ao2_callback is one that matches by address.
struct ao2_container * __ao2_container_alloc_list(unsigned int ao2_options, unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
#define attribute_warn_unused_result
Definition: compiler.h:71
ast_rwlock_t lock
Definition: astobj2.h:806
void * __ao2_iterator_next(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
int ao2_unlock_and_unref(void *obj)
Unlock an object and decrement its reference count.
Definition: astobj2.h:801
Assume that the ao2_container is already locked.
Definition: astobj2.h:1872
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Replace objects with duplicate keys in container.
Definition: astobj2.h:1215
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
Structure for rwlock and tracking information.
Definition: lock.h:156
Traverse in post-order (Children then node, for tree container)
Definition: astobj2.h:1145
unsigned int ao2_options_get(void *obj)
Retrieve the ao2 options used to create the object.
Definition: astobj2.c:778
This struct should be opaque, but it&#39;s size is needed.
Definition: astobj2.h:530
int __ao2_link(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func)
Reject objects with duplicate keys in container.
Definition: astobj2.h:1192
int ao2_iterator_count(struct ao2_iterator *iter)
Get a count of the iterated container objects.
Generic container type.
static struct test_options options
Search option field mask.
Definition: astobj2.h:1076
void() ao2_prnt_obj_fn(void *v_obj, void *where, ao2_prnt_fn *prnt)
Print object key.
Definition: astobj2.h:1454
ao2_iterator_flags
Definition: astobj2.h:1855
ao2_container_opts
Options available when allocating an ao2 container object.
Definition: astobj2.h:1165
static snd_pcm_format_t format
Definition: chan_alsa.c:102
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
Reject duplicate objects in container.
Definition: astobj2.h:1205
ao2_callback_fn * cmp_fn
Traverse order option field mask.
Definition: astobj2.h:1123
void * __ao2_unlink(struct ao2_container *c, void *obj, int flags, const char *tag, const char *file, int line, const char *func)
static struct test_val a
#define AST_INLINE_API(hdr, body)
Definition: inline_api.h:54
struct ao2_container * __ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result