2 * astobj2 - replacement containers for asterisk data structures.
4 * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
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.
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.
17 #ifndef _ASTERISK_ASTOBJ2_H
18 #define _ASTERISK_ASTOBJ2_H
20 #include "asterisk/compat.h"
21 #include "asterisk/lock.h"
26 * \page AstObj2 Object Model implementing objects and containers.
28 This module implements an abstraction for objects (with locks and
29 reference counts), and containers for these user-defined objects,
30 also supporting locking, reference counting and callbacks.
32 The internal implementation of objects and containers is opaque to the user,
33 so we can use different data structures as needs arise.
35 \section AstObj2_UsageObjects USAGE - OBJECTS
37 An ao2 object is a block of memory that the user code can access,
38 and for which the system keeps track (with a bit of help from the
39 programmer) of the number of references around. When an object has
40 no more references (refcount == 0), it is destroyed, by first
41 invoking whatever 'destructor' function the programmer specifies
42 (it can be NULL if none is necessary), and then freeing the memory.
43 This way objects can be shared without worrying who is in charge
45 As an additional feature, ao2 objects are associated to individual
48 Creating an object requires the size of the object and
49 a pointer to the destructor function:
53 o = ao2_alloc(sizeof(struct foo), my_destructor_fn);
55 The value returned points to the user-visible portion of the objects
56 (user-data), but is also used as an identifier for all object-related
57 operations such as refcount and lock manipulations.
59 On return from ao2_alloc():
61 - the object has a refcount = 1;
62 - the memory for the object is allocated dynamically and zeroed;
63 - we cannot realloc() the object itself;
64 - we cannot call free(o) to dispose of the object. Rather, we
65 tell the system that we do not need the reference anymore:
69 causing the destructor to be called (and then memory freed) when
70 the refcount goes to 0.
72 - ao2_ref(o, +1) can be used to modify the refcount on the
73 object in case we want to pass it around.
75 - ao2_lock(obj), ao2_unlock(obj), ao2_trylock(obj) can be used
76 to manipulate the lock associated with the object.
79 \section AstObj2_UsageContainers USAGE - CONTAINERS
81 An ao2 container is an abstract data structure where we can store
82 ao2 objects, search them (hopefully in an efficient way), and iterate
83 or apply a callback function to them. A container is just an ao2 object
86 A container must first be allocated, specifying the initial
87 parameters. At the moment, this is done as follows:
92 struct ao2_container *c;
94 c = ao2_container_alloc(MAX_BUCKETS, my_hash_fn, my_cmp_fn);
99 - MAX_BUCKETS is the number of buckets in the hash table,
100 - my_hash_fn() is the (user-supplied) function that returns a
101 hash key for the object (further reduced modulo MAX_BUCKETS
102 by the container's code);
103 - my_cmp_fn() is the default comparison function used when doing
104 searches on the container,
106 A container knows little or nothing about the objects it stores,
107 other than the fact that they have been created by ao2_alloc().
108 All knowledge of the (user-defined) internals of the objects
109 is left to the (user-supplied) functions passed as arguments
110 to ao2_container_alloc().
112 If we want to insert an object in a container, we should
113 initialize its fields -- especially, those used by my_hash_fn() --
114 to compute the bucket to use.
115 Once done, we can link an object to a container with
119 The function returns NULL in case of errors (and the object
120 is not inserted in the container). Other values mean success
121 (we are not supposed to use the value as a pointer to anything).
122 Linking an object to a container increases its refcount by 1
125 \note While an object o is in a container, we expect that
126 my_hash_fn(o) will always return the same value. The function
127 does not lock the object to be computed, so modifications of
128 those fields that affect the computation of the hash should
129 be done by extracting the object from the container, and
130 re-inserting it after the change (this is not terribly expensive).
132 \note A container with a single buckets is effectively a linked
133 list. However there is no ordering among elements.
135 - \ref AstObj2_Containers
136 - \ref astobj2.h All documentation for functions and data structures
141 \note DEBUGGING REF COUNTS BIBLE:
142 An interface to help debug refcounting is provided
143 in this package. It is dependent on the REF_DEBUG macro being
144 defined via menuselect and in using variants of the normal ao2_xxxx
145 function that are named ao2_t_xxxx instead, with an extra argument,
146 a string that will be printed out into the refs log file when the
147 refcount for an object is changed.
149 these ao2_t_xxx variants are provided:
151 ao2_t_alloc(arg1, arg2, arg3)
152 ao2_t_ref(arg1,arg2,arg3)
153 ao2_t_container_alloc(arg1,arg2,arg3,arg4)
154 ao2_t_link(arg1, arg2, arg3)
155 ao2_t_unlink(arg1, arg2, arg3)
156 ao2_t_callback(arg1,arg2,arg3,arg4,arg5)
157 ao2_t_find(arg1,arg2,arg3,arg4)
158 ao2_t_iterator_next(arg1, arg2)
160 If you study each argument list, you will see that these functions all have
161 one extra argument than their ao2_xxx counterpart. The last argument in
162 each case is supposed to be a string pointer, a "tag", that should contain
163 enough of an explanation, that you can pair operations that increment the
164 ref count, with operations that are meant to decrement the refcount.
166 Each of these calls will generate at least one line of output in in the refs
167 log files. These lines look like this:
169 0x8756f00,+1,1234,chan_sip.c,22240,load_module,**constructor**,allocate users
170 0x86e3408,+1,1234,chan_sip.c,22241,load_module,**constructor**,allocate peers
171 0x86dd380,+1,1234,chan_sip.c,22242,load_module,**constructor**,allocate peers_by_ip
172 0x822d020,+1,1234,chan_sip.c,22243,load_module,**constructor**,allocate dialogs
173 0x8930fd8,+1,1234,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct
174 0x8930fd8,+1,1234,chan_sip.c,21467,reload_config,1,link peer into peer table
175 0x8930fd8,-1,1234,chan_sip.c,2370,unref_peer,2,unref_peer: from reload_config
176 0x89318b0,1,5678,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct
177 0x89318b0,+1,5678,chan_sip.c,21467,reload_config,1,link peer into peer table
178 0x89318b0,-1,1234,chan_sip.c,2370,unref_peer,2,unref_peer: from reload_config
179 0x8930218,+1,1234,chan_sip.c,20025,build_peer,**constructor**,allocate a peer struct
180 0x8930218,+1,1234,chan_sip.c,21539,reload_config,1,link peer into peers table
181 0x868c040,-1,1234,chan_sip.c,2424,dialog_unlink_all,2,unset the relatedpeer->call field in tandem with relatedpeer field itself
182 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
183 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
184 0x8cc07e8,-1,1234,chan_sip.c,2370,unref_peer,3,unsetting a dialog relatedpeer field in sip_destroy
185 0x8cc07e8,+1,1234,chan_sip.c,3876,find_peer,2,ao2_find in peers table
186 0x8cc07e8,-1,1234,chan_sip.c,2370,unref_peer,3,unref_peer, from sip_devicestate, release ref from find_peer
189 This uses a comma delineated format. The columns in the format are as
191 - The first column is the object address.
192 - The second column reflects how the operation affected the ref count
193 for that object. A change in the ref count is reflected either as
194 an increment (+) or decrement (-), as well as the amount it changed
196 - The third column is the ID of the thread that modified the reference
198 - The fourth column is the source file that the change in reference was
200 - The fifth column is the line number of the source file that the ref
201 change was issued from.
202 - The sixth column is the name of the function that the ref change was
204 - The seventh column indicates either (a) construction of the object via
205 the special tag **constructor**; (b) destruction of the object via
206 the special tag **destructor**; (c) the previous reference count
207 prior to this reference change.
208 - The eighth column is a special tag added by the developer to provide
209 context for the ref change. Note that any subsequent columns are
210 considered to be part of this tag.
212 Sometimes you have some helper functions to do object ref/unref
213 operations. Using these normally hides the place where these
214 functions were called. To get the location where these functions
215 were called to appear in /refs, you can do this sort of thing:
218 #define dialog_ref(arg1,arg2) dialog_ref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
219 #define dialog_unref(arg1,arg2) dialog_unref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
220 static struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, const char *tag, const char *file, int line, const char *func)
223 ao2_ref_debug(p, 1, tag, file, line, func);
225 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
230 static struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, const char *tag, const char *file, int line, const char *func)
233 ao2_ref_debug(p, -1, tag, file, line, func);
238 static struct sip_pvt *dialog_ref(struct sip_pvt *p, const char *tag)
243 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
248 static struct sip_pvt *dialog_unref(struct sip_pvt *p, const char *tag)
257 In the above code, note that the "normal" helper funcs call ao2_ref() as
258 normal, and the "helper" functions call ao2_ref_debug directly with the
259 file, function, and line number info provided. You might find this
260 well worth the effort to help track these function calls in the code.
262 To find out why objects are not destroyed (a common bug), you can
263 edit the source file to use the ao2_t_* variants, enable REF_DEBUG
264 in menuselect, and add a descriptive tag to each call. Recompile,
265 and run Asterisk, exit asterisk with "core stop gracefully", which should
266 result in every object being destroyed.
268 Then, you can "sort -k 1 {AST_LOG_DIR}/refs > x1" to get a sorted list of
269 all the objects, or you can use "contrib/script/refcounter.py" to scan
270 the file for you and output any problems it finds.
272 The above may seem astronomically more work than it is worth to debug
273 reference counts, which may be true in "simple" situations, but for
274 more complex situations, it is easily worth 100 times this effort to
277 To debug, pair all calls so that each call that increments the
278 refcount is paired with a corresponding call that decrements the
279 count for the same reason. Hopefully, you will be left with one
280 or more unpaired calls. This is where you start your search!
282 For instance, here is an example of this for a dialog object in
283 chan_sip, that was not getting destroyed, after I moved the lines around
286 0x83787a0,+1,1234,chan_sip.c,5733,sip_alloc,**constructor**,(allocate a dialog(pvt) struct)
287 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)
289 0x83787a0,+1,1234,chan_sip.c,5854,sip_alloc,1,(link pvt into dialogs table)
290 0x83787a0,-1,1234,chan_sip.c,19150,sip_poke_peer,3,(About to change the callid -- remove the old name)
291 0x83787a0,+1,1234,chan_sip.c,19152,sip_poke_peer,2,(Linking in under new name)
292 0x83787a0,-1,1234,chan_sip.c,2399,dialog_unlink_all,5,(unlinking dialog via ao2_unlink)
294 0x83787a0,+1,1234,chan_sip.c,19130,sip_poke_peer,2,(copy sip alloc from p to peer->call)
297 0x83787a0,+1,1234,chan_sip.c,2996,__sip_reliable_xmit,3,(__sip_reliable_xmit: setting pkt->owner)
298 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)
300 0x83787a0,+1,1234,chan_sip.c,22356,unload_module,4,(iterate thru dialogs)
301 0x83787a0,-1,1234,chan_sip.c,22359,unload_module,5,(toss dialog ptr from iterator_next)
304 0x83787a0,+1,1234,chan_sip.c,22373,unload_module,3,(iterate thru dialogs)
305 0x83787a0,-1,1234,chan_sip.c,22375,unload_module,2,(throw away iterator result)
307 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)
308 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)
310 As you can see, only one unbalanced operation is in the list, a ref count increment when
311 the peer->call was set, but no corresponding decrement was made...
313 Hopefully this helps you narrow your search and find those bugs.
315 THE ART OF REFERENCE COUNTING
317 SOME TIPS for complicated code, and ref counting:
319 1. Theoretically, passing a refcounted object pointer into a function
320 call is an act of copying the reference, and could be refcounted.
321 But, upon examination, this sort of refcounting will explode the amount
322 of code you have to enter, and for no tangible benefit, beyond
323 creating more possible failure points/bugs. It will even
324 complicate your code and make debugging harder, slow down your program
325 doing useless increments and decrements of the ref counts.
327 2. It is better to track places where a ref counted pointer
328 is copied into a structure or stored. Make sure to decrement the refcount
329 of any previous pointer that might have been there, if setting
330 this field might erase a previous pointer. ao2_find and iterate_next
331 internally increment the ref count when they return a pointer, so
332 you need to decrement the count before the pointer goes out of scope.
334 3. Any time you decrement a ref count, it may be possible that the
335 object will be destroyed (freed) immediately by that call. If you
336 are destroying a series of fields in a refcounted object, and
337 any of the unref calls might possibly result in immediate destruction,
338 you can first increment the count to prevent such behavior, then
339 after the last test, decrement the pointer to allow the object
340 to be destroyed, if the refcount would be zero.
344 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
346 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
348 *//* Unlink us from the owner (channel) if we have one *//*
351 ast_channel_lock(dialog->owner);
353 ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
354 dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
356 ast_channel_unlock(dialog->owner);
359 if (dialog->registry) {
360 if (dialog->registry->call == dialog) {
361 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
363 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
366 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
368 In the above code, the ao2_t_unlink could end up destroying the dialog
369 object; if this happens, then the subsequent usages of the dialog
370 pointer could result in a core dump. So, we 'bump' the
371 count upwards before beginning, and then decrementing the count when
372 we are finished. This is analogous to 'locking' or 'protecting' operations
375 4. One of the most insidious problems I've run into when converting
376 code to do ref counted automatic destruction, is in the destruction
377 routines. Where a "destroy" routine had previously been called to
378 get rid of an object in non-refcounted code, the new regime demands
379 that you tear that "destroy" routine into two pieces, one that will
380 tear down the links and 'unref' them, and the other to actually free
381 and reset fields. A destroy routine that does any reference deletion
382 for its own object, will never be called. Another insidious problem
383 occurs in mutually referenced structures. As an example, a dialog contains
384 a pointer to a peer, and a peer contains a pointer to a dialog. Watch
385 out that the destruction of one doesn't depend on the destruction of the
386 other, as in this case a dependency loop will result in neither being
389 Given the above, you should be ready to do a good job!
398 * \brief Typedef for an object destructor.
400 * \param vdoomed Object to destroy.
403 * This is called just before freeing the memory for the object.
404 * It is passed a pointer to the user-defined data of the
409 typedef void (*ao2_destructor_fn)(void *vdoomed);
411 /*! \brief Options available when allocating an ao2 object. */
412 enum ao2_alloc_opts {
413 /*! The ao2 object has a recursive mutex lock associated with it. */
414 AO2_ALLOC_OPT_LOCK_MUTEX = (0 << 0),
415 /*! The ao2 object has a non-recursive read/write lock associated with it. */
416 AO2_ALLOC_OPT_LOCK_RWLOCK = (1 << 0),
417 /*! The ao2 object has no lock associated with it. */
418 AO2_ALLOC_OPT_LOCK_NOLOCK = (2 << 0),
419 /*! The ao2 object locking option field mask. */
420 AO2_ALLOC_OPT_LOCK_MASK = (3 << 0),
424 * \brief Allocate and initialize an object.
426 * \param data_size The sizeof() of the user-defined structure.
427 * \param destructor_fn The destructor function (can be NULL)
428 * \param options The ao2 object options (See enum ao2_alloc_opts)
429 * \param debug_msg An ao2 object debug tracing message.
430 * \return A pointer to user-data.
433 * Allocates a struct astobj2 with sufficient space for the
434 * user-defined structure.
436 * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
437 * - the refcount of the object just created is 1
438 * - the returned pointer cannot be free()'d or realloc()'ed;
439 * rather, we just call ao2_ref(o, -1);
444 #if defined(REF_DEBUG)
446 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
447 __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
448 #define ao2_alloc_options(data_size, destructor_fn, options) \
449 __ao2_alloc_debug((data_size), (destructor_fn), (options), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
451 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
452 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
453 #define ao2_alloc(data_size, destructor_fn) \
454 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
456 #elif defined(__AST_DEBUG_MALLOC)
458 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
459 __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
460 #define ao2_alloc_options(data_size, destructor_fn, options) \
461 __ao2_alloc_debug((data_size), (destructor_fn), (options), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
463 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
464 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
465 #define ao2_alloc(data_size, destructor_fn) \
466 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
470 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
471 __ao2_alloc((data_size), (destructor_fn), (options))
472 #define ao2_alloc_options(data_size, destructor_fn, options) \
473 __ao2_alloc((data_size), (destructor_fn), (options))
475 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
476 __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX)
477 #define ao2_alloc(data_size, destructor_fn) \
478 __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX)
482 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag,
483 const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
484 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options) attribute_warn_unused_result;
489 * Reference/unreference an object and return the old refcount.
491 * \param o A pointer to the object
492 * \param delta Value to add to the reference counter.
493 * \param tag used for debugging
494 * \return The value of the reference counter before the operation.
496 * Increase/decrease the reference counter according
497 * the value of delta.
499 * If the refcount goes to zero, the object is destroyed.
501 * \note The object must not be locked by the caller of this function, as
502 * it is invalid to try to unlock it after releasing the reference.
504 * \note if we know the pointer to an object, it is because we
505 * have a reference count to it, so the only case when the object
506 * can go away is when we release our reference, and it is
507 * the last one in existence.
514 #define ao2_t_ref(o,delta,tag) __ao2_ref_debug((o), (delta), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
515 #define ao2_ref(o,delta) __ao2_ref_debug((o), (delta), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
519 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta))
520 #define ao2_ref(o,delta) __ao2_ref((o), (delta))
525 * \brief Retrieve the ao2 options used to create the object.
526 * \param obj pointer to the (user-defined part) of an object.
527 * \return options from enum ao2_alloc_opts.
529 unsigned int ao2_options_get(void *obj);
533 * \brief Bump refcount on an AO2 object by one, returning the object.
535 * This is useful for inlining a ref bump, and you don't care about the ref
536 * count. Also \c NULL safe, for even more convenience.
538 * \param obj AO2 object to bump the refcount on.
539 * \retval The given \a obj pointer.
541 #define ao2_bump(obj) \
543 typeof(obj) __obj_ ## __LINE__ = (obj); \
544 if (__obj_ ## __LINE__) { \
545 ao2_ref(__obj_ ## __LINE__, +1); \
547 __obj_ ## __LINE__; \
550 int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *func);
551 int __ao2_ref(void *o, int delta);
555 * \brief Replace one object reference with another cleaning up the original.
557 * \param dst Pointer to the object that will be cleaned up.
558 * \param src Pointer to the object replacing it.
560 #define ao2_replace(dst, src) \
562 typeof(dst) *__dst_ ## __LINE__ = &dst; \
563 typeof(src) __src_ ## __LINE__ = src; \
564 if (__src_ ## __LINE__) {\
565 ao2_ref(__src_ ## __LINE__, +1); \
567 if (*__dst_ ## __LINE__) {\
568 ao2_ref(*__dst_ ## __LINE__, -1); \
570 *__dst_ ## __LINE__ = __src_ ## __LINE__; \
575 /*! \brief Which lock to request. */
577 /*! Request the mutex lock be acquired. */
579 /*! Request the read lock be acquired. */
581 /*! Request the write lock be acquired. */
588 * \param a A pointer to the object we want to lock.
589 * \param lock_how, file, func, line, var
590 * \return 0 on success, other values on error.
592 int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
593 #define ao2_lock(a) __ao2_lock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
594 #define ao2_rdlock(a) __ao2_lock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
595 #define ao2_wrlock(a) __ao2_lock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
600 * \param a A pointer to the object we want unlock.
601 * \param file, func, line, var
602 * \return 0 on success, other values on error.
604 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
605 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
608 * Try locking-- (don't block if fail)
610 * \param a A pointer to the object we want to lock.
611 * \param lock_how, file, func, line, var
612 * \return 0 on success, other values on error.
614 int __ao2_trylock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
615 #define ao2_trylock(a) __ao2_trylock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
616 #define ao2_tryrdlock(a) __ao2_trylock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
617 #define ao2_trywrlock(a) __ao2_trylock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
620 * \brief Return the mutex lock address of an object
622 * \param[in] obj A pointer to the object we want.
623 * \return the address of the mutex lock, else NULL.
625 * This function comes in handy mainly for debugging locking
626 * situations, where the locking trace code reports the
627 * lock address, this allows you to correlate against
628 * object address, to match objects to reported locks.
632 void *ao2_object_get_lockaddr(void *obj);
635 /*! Global ao2 object holder structure. */
636 struct ao2_global_obj {
637 /*! Access lock to the held ao2 object. */
639 /*! Global ao2 object. */
644 * \brief Define a global object holder to be used to hold an ao2 object, statically initialized.
647 * \param name This will be the name of the object holder.
650 * This macro creates a global object holder that can be used to
651 * hold an ao2 object accessible using the API. The structure is
652 * allocated and initialized to be empty.
656 * static AO2_GLOBAL_OBJ_STATIC(global_cfg);
659 * This defines global_cfg, intended to hold an ao2 object
660 * accessible using an API.
662 #ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
663 #define AO2_GLOBAL_OBJ_STATIC(name) \
664 struct ao2_global_obj name; \
665 static void __attribute__((constructor)) __init_##name(void) \
667 ast_rwlock_init(&name.lock); \
670 static void __attribute__((destructor)) __fini_##name(void) \
673 ao2_ref(name.obj, -1); \
676 ast_rwlock_destroy(&name.lock); \
678 struct __dummy_##name
680 #define AO2_GLOBAL_OBJ_STATIC(name) \
681 struct ao2_global_obj name = { \
682 .lock = AST_RWLOCK_INIT_VALUE, \
687 * \brief Release the ao2 object held in the global holder.
690 * \param holder Global ao2 object holder.
691 * \param tag used for debugging
696 #define ao2_t_global_obj_release(holder, tag) \
697 __ao2_global_obj_release(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
698 #define ao2_global_obj_release(holder) \
699 __ao2_global_obj_release(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
703 #define ao2_t_global_obj_release(holder, tag) \
704 __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
705 #define ao2_global_obj_release(holder) \
706 __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
709 void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
712 * \brief Replace an ao2 object in the global holder.
715 * \param holder Global ao2 object holder.
716 * \param obj Object to put into the holder. Can be NULL.
717 * \param tag used for debugging
719 * \note This function automatically increases the reference
720 * count to account for the reference that the global holder now
721 * holds to the object.
723 * \retval Reference to previous global ao2 object stored.
724 * \retval NULL if no object available.
727 #define ao2_t_global_obj_replace(holder, obj, tag) \
728 __ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
729 #define ao2_global_obj_replace(holder, obj) \
730 __ao2_global_obj_replace(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
734 #define ao2_t_global_obj_replace(holder, obj, tag) \
735 __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
736 #define ao2_global_obj_replace(holder, obj) \
737 __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
740 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;
743 * \brief Replace an ao2 object in the global holder, throwing away any old object.
746 * \param holder Global ao2 object holder.
747 * \param obj Object to put into the holder. Can be NULL.
748 * \param tag used for debugging
750 * \note This function automatically increases the reference
751 * count to account for the reference that the global holder now
752 * holds to the object. It also decreases the reference count
753 * of any object being replaced.
755 * \retval 0 The global object was previously empty
756 * \retval 1 The global object was not previously empty
759 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
760 __ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
761 #define ao2_global_obj_replace_unref(holder, obj) \
762 __ao2_global_obj_replace_unref(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
766 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
767 __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
768 #define ao2_global_obj_replace_unref(holder, obj) \
769 __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
772 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);
775 * \brief Get a reference to the object stored in the global holder.
778 * \param holder Global ao2 object holder.
779 * \param tag used for debugging
781 * \retval Reference to current ao2 object stored in the holder.
782 * \retval NULL if no object available.
785 #define ao2_t_global_obj_ref(holder, tag) \
786 __ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
787 #define ao2_global_obj_ref(holder) \
788 __ao2_global_obj_ref(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
792 #define ao2_t_global_obj_ref(holder, tag) \
793 __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
794 #define ao2_global_obj_ref(holder) \
795 __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
798 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;
802 \page AstObj2_Containers AstObj2 Containers
804 Containers are data structures meant to store several objects,
805 and perform various operations on them.
806 Internally, objects are stored in lists, hash tables or other
807 data structures depending on the needs.
809 \note NOTA BENE: at the moment the only container we support is the
810 hash table and its degenerate form, the list.
812 Operations on container include:
814 - c = \b ao2_container_alloc(size, hash_fn, cmp_fn)
815 allocate a container with desired size and default compare
817 -The compare function returns an int, which
818 can be 0 for not found, CMP_STOP to stop end a traversal,
819 or CMP_MATCH if they are equal
820 -The hash function returns an int. The hash function
821 takes two argument, the object pointer and a flags field,
823 - \b ao2_find(c, arg, flags)
824 returns zero or more elements matching a given criteria
825 (specified as arg). 'c' is the container pointer. Flags
827 OBJ_UNLINK - to remove the object, once found, from the container.
828 OBJ_NODATA - don't return the object if found (no ref count change)
829 OBJ_MULTIPLE - don't stop at first match
830 OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
831 search will be done. If not, a traversal is done.
832 OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
833 Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
834 OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
835 Similar to OBJ_SEARCH_KEY and mutually exclusive.
837 - \b ao2_callback(c, flags, fn, arg)
838 apply fn(obj, arg) to all objects in the container.
839 Similar to find. fn() can tell when to stop, and
840 do anything with the object including unlinking it.
841 - c is the container;
843 OBJ_UNLINK - to remove the object, once found, from the container.
844 OBJ_NODATA - don't return the object if found (no ref count change)
845 OBJ_MULTIPLE - don't stop at first match
846 OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
847 search will be done. If not, a traversal is done through
848 all the hash table 'buckets'..
849 OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
850 Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
851 OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
852 Similar to OBJ_SEARCH_KEY and mutually exclusive.
853 - fn is a func that returns int, and takes 3 args:
854 (void *obj, void *arg, int flags);
856 arg is the same as arg passed into ao2_callback
857 flags is the same as flags passed into ao2_callback
859 0: no match, keep going
860 CMP_STOP: stop search, no match
861 CMP_MATCH: This object is matched.
863 Note that the entire operation is run with the container
864 locked, so nobody else can change its content while we work on it.
865 However, we pay this with the fact that doing
866 anything blocking in the callback keeps the container
868 The mechanism is very flexible because the callback function fn()
869 can do basically anything e.g. counting, deleting records, etc.
870 possibly using arg to store the results.
872 - \b iterate on a container
873 this is done with the following sequence
877 struct ao2_container *c = ... // our container
878 struct ao2_iterator i;
881 i = ao2_iterator_init(c, flags);
883 while ((o = ao2_iterator_next(&i))) {
884 ... do something on o ...
888 ao2_iterator_destroy(&i);
891 The difference with the callback is that the control
892 on how to iterate is left to us.
895 dropping a reference to a container destroys it, very simple!
897 Containers are ao2 objects themselves, and this is why their
898 implementation is simple too.
900 Before declaring containers, we need to declare the types of the
901 arguments passed to the constructor - in turn, this requires
902 to define callback and hash functions and their arguments.
909 * A callback function will return a combination of CMP_MATCH and CMP_STOP.
910 * The latter will terminate the search in a container.
913 CMP_MATCH = 0x1, /*!< the object matches the request */
914 CMP_STOP = 0x2, /*!< stop the search now */
918 * \brief Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
922 * Unlink the object for which the callback function returned
925 OBJ_UNLINK = (1 << 0),
927 * On match, don't return the object hence do not increase its
930 OBJ_NODATA = (1 << 1),
932 * Don't stop at the first match in ao2_callback() unless the
933 * result of of the callback function has the CMP_STOP bit set.
935 OBJ_MULTIPLE = (1 << 2),
937 * \brief Assume that the ao2_container is already locked.
939 * \note For ao2_containers that have mutexes, no locking will
942 * \note For ao2_containers that have RWLOCKs, the lock will be
943 * promoted to write mode as needed. The lock will be returned
944 * to the original locked state.
946 * \note Only use this flag if the ao2_container is manually
949 OBJ_NOLOCK = (1 << 4),
952 * \brief Search option field mask.
954 * \todo Eventually OBJ_SEARCH_MASK will shrink to a two bit
955 * field when the codebase is made to use the search field
956 * values as a field instead of independent bits.
958 OBJ_SEARCH_MASK = (0x07 << 5),
959 /*! \brief The arg parameter has no meaning to the astobj2 code. */
960 OBJ_SEARCH_NONE = (0 << 5),
962 * \brief The arg parameter is an object of the same type.
965 * The arg parameter is an object of the same type as the one
966 * being searched for, so use the object's ao2_hash_fn and/or
967 * ao2_sort_fn functions for optimized searching.
969 * \note The supplied ao2_callback_fn is called after the
970 * container nodes have been filtered by the ao2_hash_fn and/or
971 * ao2_sort_fn functions.
973 OBJ_SEARCH_OBJECT = (1 << 5),
975 * \brief The arg parameter is a search key, but is not an object.
978 * This can be used when you want to be able to pass custom data
979 * to the container's stored ao2_hash_fn, ao2_sort_fn, and
980 * ao2_find ao2_callback_fn functions that is not a full object,
981 * but perhaps just a string.
983 * \note The supplied ao2_callback_fn is called after the
984 * container nodes have been filtered by the ao2_hash_fn and/or
985 * ao2_sort_fn functions.
987 OBJ_SEARCH_KEY = (2 << 5),
989 * \brief The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
992 * The partial key can be used by the ao2_sort_fn to guide the
993 * search to find a contiguous subset of a sorted container.
994 * For example, a sorted container holds: "A", "B", "Bert",
995 * "Beth", "Earnie". Doing a partial key search with "B" will
996 * find the sorted subset of all held objects starting with "B".
998 * \note The supplied ao2_callback_fn is called after the
999 * container nodes have been filtered by the ao2_sort_fn
1002 OBJ_SEARCH_PARTIAL_KEY = (4 << 5),
1004 /*! \brief Traverse order option field mask. */
1005 OBJ_ORDER_MASK = (0x03 << 8),
1006 /*! \brief Traverse in ascending order (First to last container object) */
1007 OBJ_ORDER_ASCENDING = (0 << 8),
1008 /*! \brief Traverse in descending order (Last to first container object) */
1009 OBJ_ORDER_DESCENDING = (1 << 8),
1011 * \brief Traverse in pre-order (Node then children, for tree container)
1013 * \note For non-tree containers, it is up to the container type
1014 * to make the best interpretation of the order. For list and
1015 * hash containers, this also means ascending order because a
1016 * binary tree can degenerate into a list.
1018 OBJ_ORDER_PRE = (2 << 8),
1020 * \brief Traverse in post-order (Children then node, for tree container)
1022 * \note For non-tree containers, it is up to the container type
1023 * to make the best interpretation of the order. For list and
1024 * hash containers, this also means descending order because a
1025 * binary tree can degenerate into a list.
1027 OBJ_ORDER_POST = (3 << 8),
1031 * Deprecated backward compatible flag names.
1033 * Note: OBJ_POINTER, OBJ_KEY, and OBJ_PARTIAL_KEY are mutually
1036 #define OBJ_POINTER OBJ_SEARCH_OBJECT /*!< Deprecated name */
1037 #define OBJ_KEY OBJ_SEARCH_KEY /*!< Deprecated name */
1038 #define OBJ_PARTIAL_KEY OBJ_SEARCH_PARTIAL_KEY /*!< Deprecated name */
1041 * \brief Options available when allocating an ao2 container object.
1043 * \note Each option is open to some interpretation by the
1044 * container type as long as it makes sense with the option
1047 enum ao2_container_opts {
1049 * \brief Insert objects at the beginning of the container.
1050 * (Otherwise it is the opposite; insert at the end.)
1052 * \note If an ao2_sort_fn is provided, the object is inserted
1053 * before any objects with duplicate keys.
1055 * \note Hash containers insert the object in the computed hash
1056 * bucket in the indicated manner.
1058 AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN = (1 << 0),
1061 * \brief The ao2 container objects with duplicate keys option field mask.
1063 AO2_CONTAINER_ALLOC_OPT_DUPS_MASK = (3 << 1),
1065 * \brief Allow objects with duplicate keys in container.
1067 AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW = (0 << 1),
1069 * \brief Reject objects with duplicate keys in container.
1071 * \note The container must be sorted. i.e. have an
1074 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT = (1 << 1),
1076 * \brief Reject duplicate objects in container.
1078 * \details Don't link the same object into the container twice.
1079 * However, you can link a different object with the same key.
1081 * \note The container must be sorted. i.e. have an
1084 * \note It is assumed that the objects are located where the
1085 * search key says they should be located.
1087 AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT = (2 << 1),
1089 * \brief Replace objects with duplicate keys in container.
1091 * \details The existing duplicate object is removed and the new
1092 * object takes the old object's place.
1094 * \note The container must be sorted. i.e. have an
1097 AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE = (3 << 1),
1101 * \brief Type of a generic callback function
1102 * \param obj pointer to the (user-defined part) of an object.
1103 * \param arg callback argument from ao2_callback()
1104 * \param flags flags from ao2_callback()
1105 * OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
1106 * OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
1107 * OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
1109 * The return values are a combination of enum _cb_results.
1110 * Callback functions are used to search or manipulate objects in a container.
1112 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags);
1114 /*! \brief A common ao2_callback is one that matches by address. */
1115 int ao2_match_by_addr(void *obj, void *arg, int flags);
1118 * \brief Type of a generic callback function
1119 * \param obj pointer to the (user-defined part) of an object.
1120 * \param arg callback argument from ao2_callback()
1121 * \param data arbitrary data from ao2_callback()
1122 * \param flags flags from ao2_callback()
1123 * OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
1124 * OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
1125 * OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
1127 * The return values are a combination of enum _cb_results.
1128 * Callback functions are used to search or manipulate objects in a container.
1130 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags);
1133 * Type of a generic function to generate a hash value from an object.
1135 * \param obj pointer to the (user-defined part) of an object.
1136 * \param flags flags from ao2_callback()
1137 * OBJ_SEARCH_OBJECT - if set, 'obj', is an object.
1138 * OBJ_SEARCH_KEY - if set, 'obj', is a search key item that is not an object.
1140 * \note This function must be idempotent.
1142 * \return Computed hash value.
1144 typedef int (ao2_hash_fn)(const void *obj, int flags);
1147 * \brief Type of generic container sort function.
1149 * \param obj_left pointer to the (user-defined part) of an object.
1150 * \param obj_right pointer to the (user-defined part) of an object.
1151 * \param flags flags from ao2_callback()
1152 * OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
1153 * OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
1154 * OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
1156 * \note This function must be idempotent.
1158 * \retval <0 if obj_left < obj_right
1159 * \retval =0 if obj_left == obj_right
1160 * \retval >0 if obj_left > obj_right
1162 typedef int (ao2_sort_fn)(const void *obj_left, const void *obj_right, int flags);
1164 /*! \name Object Containers
1165 * Here start declarations of containers.
1168 struct ao2_container;
1171 * \brief Allocate and initialize a hash container with the desired number of buckets.
1174 * We allocate space for a struct astobj_container, struct container
1175 * and the buckets[] array.
1177 * \param options Container ao2 object options (See enum ao2_alloc_opts)
1178 * \param n_buckets Number of buckets for hash
1179 * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
1180 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1181 * \param tag used for debugging.
1183 * \return A pointer to a struct container.
1185 * \note Destructor is set implicitly.
1186 * \note This is legacy container creation that is mapped to the new method.
1189 #define ao2_t_container_alloc_options(options, n_buckets, hash_fn, cmp_fn, tag) \
1190 ao2_t_container_alloc_hash((options), 0, (n_buckets), (hash_fn), NULL, (cmp_fn), (tag))
1191 #define ao2_container_alloc_options(options, n_buckets, hash_fn, cmp_fn) \
1192 ao2_container_alloc_hash((options), 0, (n_buckets), (hash_fn), NULL, (cmp_fn))
1194 #define ao2_t_container_alloc(n_buckets, hash_fn, cmp_fn, tag) \
1195 ao2_t_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), (tag))
1196 #define ao2_container_alloc(n_buckets, hash_fn, cmp_fn) \
1197 ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn))
1200 * \brief Allocate and initialize a hash container with the desired number of buckets.
1203 * We allocate space for a struct astobj_container, struct container
1204 * and the buckets[] array.
1206 * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1207 * \param container_options Container behaviour options (See enum ao2_container_opts)
1208 * \param n_buckets Number of buckets for hash
1209 * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
1210 * \param sort_fn Pointer to a sort function. (NULL to not sort the buckets.)
1211 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1212 * \param tag used for debugging.
1214 * \return A pointer to a struct container.
1216 * \note Destructor is set implicitly.
1219 #if defined(REF_DEBUG)
1221 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
1222 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1223 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1224 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1226 #elif defined(__AST_DEBUG_MALLOC)
1228 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
1229 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1230 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1231 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1235 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
1236 __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn))
1237 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1238 __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn))
1242 struct ao2_container *__ao2_container_alloc_hash(unsigned int ao2_options,
1243 unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
1244 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
1245 struct ao2_container *__ao2_container_alloc_hash_debug(unsigned int ao2_options,
1246 unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
1247 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1248 const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1251 * \brief Allocate and initialize a list container.
1253 * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1254 * \param container_options Container behaviour options (See enum ao2_container_opts)
1255 * \param sort_fn Pointer to a sort function. (NULL if list not sorted.)
1256 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1257 * \param tag used for debugging.
1259 * \return A pointer to a struct container.
1261 * \note Destructor is set implicitly.
1262 * \note Implemented as a degenerate hash table.
1265 #if defined(REF_DEBUG)
1267 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1268 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1269 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1270 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1272 #elif defined(__AST_DEBUG_MALLOC)
1274 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1275 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1276 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1277 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1281 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1282 __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn))
1283 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1284 __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn))
1288 struct ao2_container *__ao2_container_alloc_list(unsigned int ao2_options,
1289 unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
1290 struct ao2_container *__ao2_container_alloc_list_debug(unsigned int ao2_options,
1291 unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1292 const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1295 * \brief Allocate and initialize a red-black tree container.
1297 * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1298 * \param container_options Container behaviour options (See enum ao2_container_opts)
1299 * \param sort_fn Pointer to a sort function.
1300 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1301 * \param tag used for debugging.
1303 * \return A pointer to a struct container.
1305 * \note Destructor is set implicitly.
1308 #if defined(REF_DEBUG)
1310 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1311 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1312 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1313 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1315 #elif defined(__AST_DEBUG_MALLOC)
1317 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1318 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1319 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1320 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1324 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1325 __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
1326 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1327 __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
1331 struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
1332 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
1333 struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
1334 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1335 const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1338 * Returns the number of elements in a container.
1340 int ao2_container_count(struct ao2_container *c);
1343 * \brief Copy all object references in the src container into the dest container.
1346 * \param dest Container to copy src object references into.
1347 * \param src Container to copy all object references from.
1348 * \param flags OBJ_NOLOCK if a lock is already held on both containers.
1349 * Otherwise, the src container is locked first.
1351 * \pre The dest container must be empty. If the duplication fails, the
1352 * dest container will be returned empty.
1354 * \note This can potentially be expensive because a malloc is
1355 * needed for every object in the src container.
1357 * \retval 0 on success.
1358 * \retval -1 on error.
1360 int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags);
1363 * \brief Create a clone/copy of the given container.
1366 * \param orig Container to copy all object references from.
1367 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1369 * \note This can potentially be expensive because a malloc is
1370 * needed for every object in the orig container.
1372 * \retval Clone container on success.
1373 * \retval NULL on error.
1375 struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags) attribute_warn_unused_result;
1376 struct ao2_container *__ao2_container_clone_debug(struct ao2_container *orig, enum search_flags flags, const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1377 #if defined(REF_DEBUG)
1379 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1380 #define ao2_container_clone(orig, flags) __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1382 #elif defined(__AST_DEBUG_MALLOC)
1384 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1385 #define ao2_container_clone(orig, flags) __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1389 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone(orig, flags)
1390 #define ao2_container_clone(orig, flags) __ao2_container_clone(orig, flags)
1395 * \brief Print output.
1398 * \param where User data pointer needed to determine where to put output.
1399 * \param fmt printf type format string.
1403 typedef void (ao2_prnt_fn)(void *where, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
1406 * \brief Print object key.
1409 * \param v_obj A pointer to the object we want the key printed.
1410 * \param where User data needed by prnt to determine where to put output.
1411 * \param prnt Print output callback function to use.
1415 typedef void (ao2_prnt_obj_fn)(void *v_obj, void *where, ao2_prnt_fn *prnt);
1418 * \brief Display contents of the specified container.
1421 * \param self Container to dump.
1422 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1423 * \param name Container name. (NULL if anonymous)
1424 * \param where User data needed by prnt to determine where to put output.
1425 * \param prnt Print output callback function to use.
1426 * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
1430 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);
1433 * \brief Display statistics of the specified container.
1436 * \param self Container to display statistics.
1437 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1438 * \param name Container name. (NULL if anonymous)
1439 * \param where User data needed by prnt to determine where to put output.
1440 * \param prnt Print output callback function to use.
1444 void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt);
1447 * \brief Perform an integrity check on the specified container.
1450 * \param self Container to check integrity.
1451 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1453 * \retval 0 on success.
1454 * \retval -1 on error.
1456 int ao2_container_check(struct ao2_container *self, enum search_flags flags);
1459 * \brief Register a container for CLI stats and integrity check.
1462 * \param name Name to register the container under.
1463 * \param self Container to register.
1464 * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
1466 * \retval 0 on success.
1467 * \retval -1 on error.
1469 int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj);
1472 * \brief Unregister a container for CLI stats and integrity check.
1475 * \param name Name the container is registered under.
1479 void ao2_container_unregister(const char *name);
1483 /*! \name Object Management
1484 * Here we have functions to manage objects.
1486 * We can use the functions below on any kind of
1487 * object defined by the user.
1492 * \brief Add an object to a container.
1494 * \param container The container to operate on.
1495 * \param obj The object to be added.
1496 * \param tag used for debugging.
1498 * \retval 0 on errors.
1499 * \retval 1 on success.
1501 * This function inserts an object in a container according its key.
1503 * \note Remember to set the key before calling this function.
1505 * \note This function automatically increases the reference count to account
1506 * for the reference that the container now holds to the object.
1510 #define ao2_t_link(container, obj, tag) __ao2_link_debug((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1511 #define ao2_link(container, obj) __ao2_link_debug((container), (obj), 0, "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1514 * \brief Add an object to a container.
1516 * \param container The container to operate on.
1517 * \param obj The object to be added.
1518 * \param flags search_flags to control linking the object. (OBJ_NOLOCK)
1519 * \param tag used for debugging.
1521 * \retval 0 on errors.
1522 * \retval 1 on success.
1524 * This function inserts an object in a container according its key.
1526 * \note Remember to set the key before calling this function.
1528 * \note This function automatically increases the reference count to account
1529 * for the reference that the container now holds to the object.
1531 #define ao2_t_link_flags(container, obj, flags, tag) __ao2_link_debug((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1532 #define ao2_link_flags(container, obj, flags) __ao2_link_debug((container), (obj), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1536 #define ao2_t_link(container, obj, tag) __ao2_link((container), (obj), 0)
1537 #define ao2_link(container, obj) __ao2_link((container), (obj), 0)
1539 #define ao2_t_link_flags(container, obj, flags, tag) __ao2_link((container), (obj), (flags))
1540 #define ao2_link_flags(container, obj, flags) __ao2_link((container), (obj), (flags))
1544 int __ao2_link_debug(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func);
1545 int __ao2_link(struct ao2_container *c, void *obj_new, int flags);
1548 * \brief Remove an object from a container
1550 * \param container The container to operate on.
1551 * \param obj The object to unlink.
1552 * \param tag used for debugging.
1554 * \retval NULL, always
1556 * \note The object requested to be unlinked must be valid. However, if it turns
1557 * out that it is not in the container, this function is still safe to
1560 * \note If the object gets unlinked from the container, the container's
1561 * reference to the object will be automatically released. (The
1562 * refcount will be decremented).
1566 #define ao2_t_unlink(container, obj, tag) __ao2_unlink_debug((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1567 #define ao2_unlink(container, obj) __ao2_unlink_debug((container), (obj), 0, "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1570 * \brief Remove an object from a container
1572 * \param container The container to operate on.
1573 * \param obj The object to unlink.
1574 * \param flags search_flags to control unlinking the object. (OBJ_NOLOCK)
1575 * \param tag used for debugging.
1577 * \retval NULL, always
1579 * \note The object requested to be unlinked must be valid. However, if it turns
1580 * out that it is not in the container, this function is still safe to
1583 * \note If the object gets unlinked from the container, the container's
1584 * reference to the object will be automatically released. (The
1585 * refcount will be decremented).
1588 #define ao2_t_unlink_flags(container, obj, flags, tag) __ao2_unlink_debug((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1589 #define ao2_unlink_flags(container, obj, flags) __ao2_unlink_debug((container), (obj), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1593 #define ao2_t_unlink(container, obj, tag) __ao2_unlink((container), (obj), 0)
1594 #define ao2_unlink(container, obj) __ao2_unlink((container), (obj), 0)
1596 #define ao2_t_unlink_flags(container, obj, flags, tag) __ao2_unlink((container), (obj), (flags))
1597 #define ao2_unlink_flags(container, obj, flags) __ao2_unlink((container), (obj), (flags))
1601 void *__ao2_unlink_debug(struct ao2_container *c, void *obj, int flags, const char *tag, const char *file, int line, const char *func);
1602 void *__ao2_unlink(struct ao2_container *c, void *obj, int flags);
1608 * ao2_callback() is a generic function that applies cb_fn() to all objects
1609 * in a container, as described below.
1611 * \param c A pointer to the container to operate on.
1612 * \param flags A set of flags specifying the operation to perform,
1613 * partially used by the container code, but also passed to
1615 * - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts
1616 * of any of the traversed objects will be incremented.
1617 * On the converse, if it is NOT set (the default), the ref count
1618 * of the first matching object will be incremented and returned.
1619 * - If OBJ_MULTIPLE is set, the ref count of all matching objects will
1620 * be incremented in an iterator for a temporary container and returned.
1621 * - If OBJ_SEARCH_OBJECT is set, the traversed items will be restricted
1622 * to the objects in the bucket that the object key hashes to.
1623 * - If OBJ_SEARCH_KEY is set, the traversed items will be restricted
1624 * to the objects in the bucket that the object key hashes to.
1625 * \param cb_fn A function pointer, that will be called on all
1626 * objects, to see if they match. This function returns CMP_MATCH
1627 * if the object is matches the criteria; CMP_STOP if the traversal
1628 * should immediately stop, or both (via bitwise ORing), if you find a
1629 * match and want to end the traversal, and 0 if the object is not a match,
1630 * but the traversal should continue. This is the function that is applied
1631 * to each object traversed. Its arguments are:
1632 * (void *obj, void *arg, int flags), where:
1634 * arg is the same as arg passed into ao2_callback
1635 * flags is the same as flags passed into ao2_callback (flags are
1636 * also used by ao2_callback).
1637 * \param arg passed to the callback.
1638 * \param tag used for debugging.
1640 * \retval NULL on failure or no matching object found.
1642 * \retval object found if OBJ_MULTIPLE is not set in the flags
1645 * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the
1646 * flags parameter. The iterator must be destroyed with
1647 * ao2_iterator_destroy() when the caller no longer needs it.
1649 * If the function returns any objects, their refcount is incremented,
1650 * and the caller is in charge of decrementing them once done.
1652 * Typically, ao2_callback() is used for two purposes:
1653 * - to perform some action (including removal from the container) on one
1654 * or more objects; in this case, cb_fn() can modify the object itself,
1655 * and to perform deletion should set CMP_MATCH on the matching objects,
1656 * and have OBJ_UNLINK set in flags.
1657 * - to look for a specific object in a container; in this case, cb_fn()
1658 * should not modify the object, but just return a combination of
1659 * CMP_MATCH and CMP_STOP on the desired object.
1660 * Other usages are also possible, of course.
1662 * This function searches through a container and performs operations
1663 * on objects according on flags passed.
1664 * XXX describe better
1665 * The comparison is done calling the compare function set implicitly.
1666 * The arg pointer can be a pointer to an object or to a key,
1667 * we can say this looking at flags value.
1668 * If arg points to an object we will search for the object pointed
1669 * by this value, otherwise we search for a key value.
1670 * If the key is not unique we only find the first matching value.
1672 * The use of flags argument is the follow:
1674 * OBJ_UNLINK unlinks the object found
1675 * OBJ_NODATA on match, do return an object
1676 * Callbacks use OBJ_NODATA as a default
1677 * functions such as find() do
1678 * OBJ_MULTIPLE return multiple matches
1680 * OBJ_SEARCH_OBJECT the pointer is to an object
1681 * OBJ_SEARCH_KEY the pointer is to a search key
1682 * OBJ_SEARCH_PARTIAL_KEY the pointer is to a partial search key
1684 * \note When the returned object is no longer in use, ao2_ref() should
1685 * be used to free the additional reference possibly created by this function.
1691 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
1692 __ao2_callback_debug((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1693 #define ao2_callback(c, flags, cb_fn, arg) \
1694 __ao2_callback_debug((c), (flags), (cb_fn), (arg), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1698 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
1699 __ao2_callback((c), (flags), (cb_fn), (arg))
1700 #define ao2_callback(c, flags, cb_fn, arg) \
1701 __ao2_callback((c), (flags), (cb_fn), (arg))
1705 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
1706 ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line,
1708 void *__ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg);
1713 * ao2_callback_data() is a generic function that applies cb_fn() to all objects
1714 * in a container. It is functionally identical to ao2_callback() except that
1715 * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and
1716 * allows the caller to pass in arbitrary data.
1718 * This call would be used instead of ao2_callback() when the caller needs to pass
1719 * OBJ_SEARCH_OBJECT, OBJ_SEARCH_KEY, or OBJ_SEARCH_PARTIAL_KEY as part of the flags
1720 * argument (which in turn requires passing in a known pointer type for 'arg') and
1721 * also needs access to other non-global data to complete it's comparison or task.
1723 * See the documentation for ao2_callback() for argument descriptions.
1725 * \see ao2_callback()
1729 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
1730 __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1731 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
1732 __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1736 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
1737 __ao2_callback_data((container), (flags), (cb_fn), (arg), (data))
1738 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
1739 __ao2_callback_data((container), (flags), (cb_fn), (arg), (data))
1743 void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags,
1744 ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file,
1745 int line, const char *func);
1746 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
1747 ao2_callback_data_fn *cb_fn, void *arg, void *data);
1749 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg)
1750 * XXX possibly change order of arguments ?
1754 #define ao2_t_find(container, arg, flags, tag) \
1755 __ao2_find_debug((container), (arg), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1756 #define ao2_find(container, arg, flags) \
1757 __ao2_find_debug((container), (arg), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1761 #define ao2_t_find(container, arg, flags, tag) \
1762 __ao2_find((container), (arg), (flags))
1763 #define ao2_find(container, arg, flags) \
1764 __ao2_find((container), (arg), (flags))
1768 void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags,
1769 const char *tag, const char *file, int line, const char *func);
1770 void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags);
1775 * When we need to walk through a container, we use an
1776 * ao2_iterator to keep track of the current position.
1778 * Because the navigation is typically done without holding the
1779 * lock on the container across the loop, objects can be
1780 * inserted or deleted or moved while we work. As a
1781 * consequence, there is no guarantee that we manage to touch
1782 * all the elements in the container, and it is possible that we
1783 * touch the same object multiple times.
1785 * An iterator must be first initialized with
1786 * ao2_iterator_init(), then we can use o = ao2_iterator_next()
1787 * to move from one element to the next. Remember that the
1788 * object returned by ao2_iterator_next() has its refcount
1789 * incremented, and the reference must be explicitly released
1790 * when done with it.
1792 * In addition, ao2_iterator_init() will hold a reference to the
1793 * container being iterated and the last container node found.
1794 * Thes objects will be unreffed when ao2_iterator_destroy() is
1795 * called to free up the resources used by the iterator (if
1802 * struct ao2_container *c = ... // the container we want to iterate on
1803 * struct ao2_iterator i;
1806 * i = ao2_iterator_init(c, flags);
1808 * while ((o = ao2_iterator_next(&i))) {
1809 * ... do something on o ...
1813 * ao2_iterator_restart(&i);
1814 * while ((o = ao2_iterator_next(&i))) {
1815 * ... do something on o ...
1819 * ao2_iterator_destroy(&i);
1826 * \brief The astobj2 iterator
1828 * \note You are not supposed to know the internals of an iterator!
1829 * We would like the iterator to be opaque, unfortunately
1830 * its size needs to be known if we want to store it around
1831 * without too much trouble.
1833 * The iterator has a pointer to the container, and a flags
1834 * field specifying various things e.g. whether the container
1835 * should be locked or not while navigating on it.
1836 * The iterator "points" to the current container node.
1838 * Details are in the implementation of ao2_iterator_next()
1840 struct ao2_iterator {
1841 /*! The container (Has a reference) */
1842 struct ao2_container *c;
1843 /*! Last container node (Has a reference) */
1845 /*! Nonzero if the iteration has completed. */
1847 /*! operation flags (enum ao2_iterator_flags) */
1851 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
1854 enum ao2_iterator_flags {
1856 * \brief Assume that the ao2_container is already locked.
1858 * \note For ao2_containers that have mutexes, no locking will
1861 * \note For ao2_containers that have RWLOCKs, the lock will be
1862 * promoted to write mode as needed. The lock will be returned
1863 * to the original locked state.
1865 * \note Only use this flag if the ao2_container is manually
1866 * locked already. You should hold the lock until after
1867 * ao2_iterator_destroy(). If you must release the lock then
1868 * you must at least hold the lock whenever you call an
1869 * ao2_iterator_xxx function with this iterator.
1871 AO2_ITERATOR_DONTLOCK = (1 << 0),
1873 * Indicates that the iterator was dynamically allocated by
1874 * astobj2 API and should be freed by ao2_iterator_destroy().
1876 AO2_ITERATOR_MALLOCD = (1 << 1),
1878 * Indicates that before the iterator returns an object from
1879 * the container being iterated, the object should be unlinked
1880 * from the container.
1882 AO2_ITERATOR_UNLINK = (1 << 2),
1884 * Iterate in descending order (Last to first container object)
1885 * (Otherwise ascending order)
1887 * \note Other traversal orders such as pre-order and post-order
1888 * do not make sense because they require the container
1889 * structure to be static during the traversal. Iterators just
1890 * about guarantee that is not going to happen because the
1891 * container is allowed to change by other threads during the
1894 AO2_ITERATOR_DESCENDING = (1 << 3),
1898 * \brief Create an iterator for a container
1900 * \param c the container
1901 * \param flags one or more flags from ao2_iterator_flags.
1903 * \retval the constructed iterator
1905 * \note This function does \b not take a pointer to an iterator;
1906 * rather, it returns an iterator structure that should be
1907 * assigned to (overwriting) an existing iterator structure
1908 * allocated on the stack or on the heap.
1910 * This function will take a reference on the container being iterated.
1912 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result;
1915 * \brief Destroy a container iterator
1917 * \param iter the iterator to destroy
1921 * This function will release the container reference held by the iterator
1922 * and any other resources it may be holding.
1924 #if defined(TEST_FRAMEWORK)
1925 void ao2_iterator_destroy(struct ao2_iterator *iter) __attribute__((noinline));
1927 void ao2_iterator_destroy(struct ao2_iterator *iter);
1928 #endif /* defined(TEST_FRAMEWORK) */
1932 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next_debug((iter), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1933 #define ao2_iterator_next(iter) __ao2_iterator_next_debug((iter), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1937 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next((iter))
1938 #define ao2_iterator_next(iter) __ao2_iterator_next((iter))
1942 void *__ao2_iterator_next_debug(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1943 void *__ao2_iterator_next(struct ao2_iterator *iter) attribute_warn_unused_result;
1946 * \brief Restart an iteration.
1948 * \param iter the iterator to restart
1950 * \note A restart is not going to have any effect if the
1951 * iterator was created with the AO2_ITERATOR_UNLINK flag. Any
1952 * previous objects returned were removed from the container.
1956 void ao2_iterator_restart(struct ao2_iterator *iter);
1958 /* extra functions */
1959 void ao2_bt(void); /* backtrace */
1961 /*! gcc __attribute__(cleanup()) functions
1962 * \note they must be able to handle NULL parameters because most of the
1963 * allocation/find functions can fail and we don't want to try to tear
1965 void __ao2_cleanup(void *obj);
1966 void __ao2_cleanup_debug(void *obj, const char *file, int line, const char *function);
1968 #define ao2_cleanup(obj) __ao2_cleanup_debug((obj), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1970 #define ao2_cleanup(obj) __ao2_cleanup(obj)
1972 void ao2_iterator_cleanup(struct ao2_iterator *iter);
1975 * \brief Get a count of the iterated container objects.
1977 * \param iter the iterator to query
1979 * \retval The number of objects in the iterated container
1981 int ao2_iterator_count(struct ao2_iterator *iter);
1983 #endif /* _ASTERISK_ASTOBJ2_H */