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 in a source file, before the #include of astobj2.h,
145 and in using variants of the normal ao2_xxx functions
146 that are named ao2_t_xxx instead, with an extra argument, a string,
147 that will be printed out into /tmp/refs when the refcount for an
150 these ao2_t_xxx variants are provided:
152 ao2_t_alloc(arg1, arg2, arg3)
153 ao2_t_ref(arg1,arg2,arg3)
154 ao2_t_container_alloc(arg1,arg2,arg3,arg4)
155 ao2_t_link(arg1, arg2, arg3)
156 ao2_t_unlink(arg1, arg2, arg3)
157 ao2_t_callback(arg1,arg2,arg3,arg4,arg5)
158 ao2_t_find(arg1,arg2,arg3,arg4)
159 ao2_t_iterator_next(arg1, arg2)
161 If you study each argument list, you will see that these functions all have
162 one extra argument than their ao2_xxx counterpart. The last argument in
163 each case is supposed to be a string pointer, a "tag", that should contain
164 enough of an explanation, that you can pair operations that increment the
165 ref count, with operations that are meant to decrement the refcount.
167 Each of these calls will generate at least one line of output in /tmp/refs.
168 These lines look like this:
170 0x8756f00 =1 chan_sip.c:22240:load_module (allocate users)
171 0x86e3408 =1 chan_sip.c:22241:load_module (allocate peers)
172 0x86dd380 =1 chan_sip.c:22242:load_module (allocate peers_by_ip)
173 0x822d020 =1 chan_sip.c:22243:load_module (allocate dialogs)
174 0x8930fd8 =1 chan_sip.c:20025:build_peer (allocate a peer struct)
175 0x8930fd8 +1 chan_sip.c:21467:reload_config (link peer into peer table) [@1]
176 0x8930fd8 -1 chan_sip.c:2370:unref_peer (unref_peer: from reload_config) [@2]
177 0x89318b0 =1 chan_sip.c:20025:build_peer (allocate a peer struct)
178 0x89318b0 +1 chan_sip.c:21467:reload_config (link peer into peer table) [@1]
179 0x89318b0 -1 chan_sip.c:2370:unref_peer (unref_peer: from reload_config) [@2]
180 0x8930218 =1 chan_sip.c:20025:build_peer (allocate a peer struct)
181 0x8930218 +1 chan_sip.c:21539:reload_config (link peer into peers table) [@1]
182 0x868c040 -1 chan_sip.c:2424:dialog_unlink_all (unset the relatedpeer->call field in tandem with relatedpeer field itself) [@2]
183 0x868c040 -1 chan_sip.c:2443:dialog_unlink_all (Let's unbump the count in the unlink so the poor pvt can disappear if it is time) [@1]
184 0x868c040 **call destructor** chan_sip.c:2443:dialog_unlink_all (Let's unbump the count in the unlink so the poor pvt can disappear if it is time)
185 0x8cc07e8 -1 chan_sip.c:2370:unref_peer (unsetting a dialog relatedpeer field in sip_destroy) [@3]
186 0x8cc07e8 +1 chan_sip.c:3876:find_peer (ao2_find in peers table) [@2]
187 0x8cc07e8 -1 chan_sip.c:2370:unref_peer (unref_peer, from sip_devicestate, release ref from find_peer) [@3]
190 The first column is the object address.
191 The second column reflects how the operation affected the ref count
192 for that object. Creation sets the ref count to 1 (=1).
193 increment or decrement and amount are specified (-1/+1).
194 The remainder of the line specifies where in the file the call was made,
195 and the function name, and the tag supplied in the function call.
197 The **call destructor** is specified when the destroy routine is
198 run for an object. It does not affect the ref count, but is important
199 in debugging, because it is possible to have the astobj2 system run it
200 multiple times on the same object, commonly fatal to asterisk.
202 Sometimes you have some helper functions to do object ref/unref
203 operations. Using these normally hides the place where these
204 functions were called. To get the location where these functions
205 were called to appear in /tmp/refs, you can do this sort of thing:
208 #define dialog_ref(arg1,arg2) dialog_ref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
209 #define dialog_unref(arg1,arg2) dialog_unref_debug((arg1),(arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
210 static struct sip_pvt *dialog_ref_debug(struct sip_pvt *p, const char *tag, const char *file, int line, const char *func)
213 ao2_ref_debug(p, 1, tag, file, line, func);
215 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
220 static struct sip_pvt *dialog_unref_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);
228 static struct sip_pvt *dialog_ref(struct sip_pvt *p, const char *tag)
233 ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
238 static struct sip_pvt *dialog_unref(struct sip_pvt *p, const char *tag)
247 In the above code, note that the "normal" helper funcs call ao2_ref() as
248 normal, and the "helper" functions call ao2_ref_debug directly with the
249 file, function, and line number info provided. You might find this
250 well worth the effort to help track these function calls in the code.
252 To find out why objects are not destroyed (a common bug), you can
253 edit the source file to use the ao2_t_* variants, add the #define REF_DEBUG 1
254 before the #include "asterisk/astobj2.h" line, and add a descriptive
255 tag to each call. Recompile, and run Asterisk, exit asterisk with
256 "stop gracefully", which should result in every object being destroyed.
257 Then, you can "sort -k 1 /tmp/refs > x1" to get a sorted list of
258 all the objects, or you can use "util/refcounter" to scan the file
259 for you and output any problems it finds.
261 The above may seem astronomically more work than it is worth to debug
262 reference counts, which may be true in "simple" situations, but for
263 more complex situations, it is easily worth 100 times this effort to
266 To debug, pair all calls so that each call that increments the
267 refcount is paired with a corresponding call that decrements the
268 count for the same reason. Hopefully, you will be left with one
269 or more unpaired calls. This is where you start your search!
271 For instance, here is an example of this for a dialog object in
272 chan_sip, that was not getting destroyed, after I moved the lines around
275 0x83787a0 =1 chan_sip.c:5733:sip_alloc (allocate a dialog(pvt) struct)
276 0x83787a0 -1 chan_sip.c:19173:sip_poke_peer (unref dialog at end of sip_poke_peer, obtained from sip_alloc, just before it goes out of scope) [@4]
278 0x83787a0 +1 chan_sip.c:5854:sip_alloc (link pvt into dialogs table) [@1]
279 0x83787a0 -1 chan_sip.c:19150:sip_poke_peer (About to change the callid -- remove the old name) [@3]
280 0x83787a0 +1 chan_sip.c:19152:sip_poke_peer (Linking in under new name) [@2]
281 0x83787a0 -1 chan_sip.c:2399:dialog_unlink_all (unlinking dialog via ao2_unlink) [@5]
283 0x83787a0 +1 chan_sip.c:19130:sip_poke_peer (copy sip alloc from p to peer->call) [@2]
286 0x83787a0 +1 chan_sip.c:2996:__sip_reliable_xmit (__sip_reliable_xmit: setting pkt->owner) [@3]
287 0x83787a0 -1 chan_sip.c:2425:dialog_unlink_all (remove all current packets in this dialog, and the pointer to the dialog too as part of __sip_destroy) [@4]
289 0x83787a0 +1 chan_sip.c:22356:unload_module (iterate thru dialogs) [@4]
290 0x83787a0 -1 chan_sip.c:22359:unload_module (toss dialog ptr from iterator_next) [@5]
293 0x83787a0 +1 chan_sip.c:22373:unload_module (iterate thru dialogs) [@3]
294 0x83787a0 -1 chan_sip.c:22375:unload_module (throw away iterator result) [@2]
296 0x83787a0 +1 chan_sip.c:2397:dialog_unlink_all (Let's bump the count in the unlink so it doesn't accidentally become dead before we are done) [@4]
297 0x83787a0 -1 chan_sip.c:2436:dialog_unlink_all (Let's unbump the count in the unlink so the poor pvt can disappear if it is time) [@3]
299 As you can see, only one unbalanced operation is in the list, a ref count increment when
300 the peer->call was set, but no corresponding decrement was made...
302 Hopefully this helps you narrow your search and find those bugs.
304 THE ART OF REFERENCE COUNTING
306 SOME TIPS for complicated code, and ref counting:
308 1. Theoretically, passing a refcounted object pointer into a function
309 call is an act of copying the reference, and could be refcounted.
310 But, upon examination, this sort of refcounting will explode the amount
311 of code you have to enter, and for no tangible benefit, beyond
312 creating more possible failure points/bugs. It will even
313 complicate your code and make debugging harder, slow down your program
314 doing useless increments and decrements of the ref counts.
316 2. It is better to track places where a ref counted pointer
317 is copied into a structure or stored. Make sure to decrement the refcount
318 of any previous pointer that might have been there, if setting
319 this field might erase a previous pointer. ao2_find and iterate_next
320 internally increment the ref count when they return a pointer, so
321 you need to decrement the count before the pointer goes out of scope.
323 3. Any time you decrement a ref count, it may be possible that the
324 object will be destroyed (freed) immediately by that call. If you
325 are destroying a series of fields in a refcounted object, and
326 any of the unref calls might possibly result in immediate destruction,
327 you can first increment the count to prevent such behavior, then
328 after the last test, decrement the pointer to allow the object
329 to be destroyed, if the refcount would be zero.
333 dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
335 ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
337 *//* Unlink us from the owner (channel) if we have one *//*
340 ast_channel_lock(dialog->owner);
342 ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
343 dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
345 ast_channel_unlock(dialog->owner);
348 if (dialog->registry) {
349 if (dialog->registry->call == dialog) {
350 dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
352 dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
355 dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
357 In the above code, the ao2_t_unlink could end up destroying the dialog
358 object; if this happens, then the subsequent usages of the dialog
359 pointer could result in a core dump. So, we 'bump' the
360 count upwards before beginning, and then decrementing the count when
361 we are finished. This is analogous to 'locking' or 'protecting' operations
364 4. One of the most insidious problems I've run into when converting
365 code to do ref counted automatic destruction, is in the destruction
366 routines. Where a "destroy" routine had previously been called to
367 get rid of an object in non-refcounted code, the new regime demands
368 that you tear that "destroy" routine into two pieces, one that will
369 tear down the links and 'unref' them, and the other to actually free
370 and reset fields. A destroy routine that does any reference deletion
371 for its own object, will never be called. Another insidious problem
372 occurs in mutually referenced structures. As an example, a dialog contains
373 a pointer to a peer, and a peer contains a pointer to a dialog. Watch
374 out that the destruction of one doesn't depend on the destruction of the
375 other, as in this case a dependency loop will result in neither being
378 Given the above, you should be ready to do a good job!
387 * \brief Typedef for an object destructor.
389 * \param vdoomed Object to destroy.
392 * This is called just before freeing the memory for the object.
393 * It is passed a pointer to the user-defined data of the
398 typedef void (*ao2_destructor_fn)(void *vdoomed);
400 /*! \brief Options available when allocating an ao2 object. */
401 enum ao2_alloc_opts {
402 /*! The ao2 object has a recursive mutex lock associated with it. */
403 AO2_ALLOC_OPT_LOCK_MUTEX = (0 << 0),
404 /*! The ao2 object has a non-recursive read/write lock associated with it. */
405 AO2_ALLOC_OPT_LOCK_RWLOCK = (1 << 0),
406 /*! The ao2 object has no lock associated with it. */
407 AO2_ALLOC_OPT_LOCK_NOLOCK = (2 << 0),
408 /*! The ao2 object locking option field mask. */
409 AO2_ALLOC_OPT_LOCK_MASK = (3 << 0),
413 * \brief Allocate and initialize an object.
415 * \param data_size The sizeof() of the user-defined structure.
416 * \param destructor_fn The destructor function (can be NULL)
417 * \param options The ao2 object options (See enum ao2_alloc_opts)
418 * \param debug_msg An ao2 object debug tracing message.
419 * \return A pointer to user-data.
422 * Allocates a struct astobj2 with sufficient space for the
423 * user-defined structure.
425 * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
426 * - the refcount of the object just created is 1
427 * - the returned pointer cannot be free()'d or realloc()'ed;
428 * rather, we just call ao2_ref(o, -1);
433 #if defined(REF_DEBUG)
435 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
436 __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
437 #define ao2_alloc_options(data_size, destructor_fn, options) \
438 __ao2_alloc_debug((data_size), (destructor_fn), (options), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
440 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
441 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
442 #define ao2_alloc(data_size, destructor_fn) \
443 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
445 #elif defined(__AST_DEBUG_MALLOC)
447 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
448 __ao2_alloc_debug((data_size), (destructor_fn), (options), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
449 #define ao2_alloc_options(data_size, destructor_fn, options) \
450 __ao2_alloc_debug((data_size), (destructor_fn), (options), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
452 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
453 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
454 #define ao2_alloc(data_size, destructor_fn) \
455 __ao2_alloc_debug((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
459 #define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg) \
460 __ao2_alloc((data_size), (destructor_fn), (options))
461 #define ao2_alloc_options(data_size, destructor_fn, options) \
462 __ao2_alloc((data_size), (destructor_fn), (options))
464 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) \
465 __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX)
466 #define ao2_alloc(data_size, destructor_fn) \
467 __ao2_alloc((data_size), (destructor_fn), AO2_ALLOC_OPT_LOCK_MUTEX)
471 void *__ao2_alloc_debug(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag,
472 const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
473 void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options) attribute_warn_unused_result;
478 * Reference/unreference an object and return the old refcount.
480 * \param o A pointer to the object
481 * \param delta Value to add to the reference counter.
482 * \param tag used for debugging
483 * \return The value of the reference counter before the operation.
485 * Increase/decrease the reference counter according
486 * the value of delta.
488 * If the refcount goes to zero, the object is destroyed.
490 * \note The object must not be locked by the caller of this function, as
491 * it is invalid to try to unlock it after releasing the reference.
493 * \note if we know the pointer to an object, it is because we
494 * have a reference count to it, so the only case when the object
495 * can go away is when we release our reference, and it is
496 * the last one in existence.
503 #define ao2_t_ref(o,delta,tag) __ao2_ref_debug((o), (delta), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
504 #define ao2_ref(o,delta) __ao2_ref_debug((o), (delta), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
508 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta))
509 #define ao2_ref(o,delta) __ao2_ref((o), (delta))
515 * \brief Bump refcount on an AO2 object by one, returning the object.
517 * This is useful for inlining a ref bump, and you don't care about the ref
518 * count. Also \c NULL safe, for even more convenience.
520 * \param obj AO2 object to bump the refcount on.
521 * \retval The given \a obj pointer.
523 #define ao2_bump(obj) \
525 typeof(obj) __obj_ ## __LINE__ = (obj); \
526 if (__obj_ ## __LINE__) { \
527 ao2_ref(__obj_ ## __LINE__, +1); \
529 __obj_ ## __LINE__; \
532 int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *func);
533 int __ao2_ref(void *o, int delta);
537 /*! \brief Which lock to request. */
539 /*! Request the mutex lock be acquired. */
541 /*! Request the read lock be acquired. */
543 /*! Request the write lock be acquired. */
550 * \param a A pointer to the object we want to lock.
551 * \param lock_how, file, func, line, var
552 * \return 0 on success, other values on error.
554 int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
555 #define ao2_lock(a) __ao2_lock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
556 #define ao2_rdlock(a) __ao2_lock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
557 #define ao2_wrlock(a) __ao2_lock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
562 * \param a A pointer to the object we want unlock.
563 * \param file, func, line, var
564 * \return 0 on success, other values on error.
566 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
567 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
570 * Try locking-- (don't block if fail)
572 * \param a A pointer to the object we want to lock.
573 * \param lock_how, file, func, line, var
574 * \return 0 on success, other values on error.
576 int __ao2_trylock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var);
577 #define ao2_trylock(a) __ao2_trylock(a, AO2_LOCK_REQ_MUTEX, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
578 #define ao2_tryrdlock(a) __ao2_trylock(a, AO2_LOCK_REQ_RDLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
579 #define ao2_trywrlock(a) __ao2_trylock(a, AO2_LOCK_REQ_WRLOCK, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
582 * \brief Return the mutex lock address of an object
584 * \param[in] obj A pointer to the object we want.
585 * \return the address of the mutex lock, else NULL.
587 * This function comes in handy mainly for debugging locking
588 * situations, where the locking trace code reports the
589 * lock address, this allows you to correlate against
590 * object address, to match objects to reported locks.
594 void *ao2_object_get_lockaddr(void *obj);
597 /*! Global ao2 object holder structure. */
598 struct ao2_global_obj {
599 /*! Access lock to the held ao2 object. */
601 /*! Global ao2 object. */
606 * \brief Define a global object holder to be used to hold an ao2 object, statically initialized.
609 * \param name This will be the name of the object holder.
612 * This macro creates a global object holder that can be used to
613 * hold an ao2 object accessible using the API. The structure is
614 * allocated and initialized to be empty.
618 * static AO2_GLOBAL_OBJ_STATIC(global_cfg);
621 * This defines global_cfg, intended to hold an ao2 object
622 * accessible using an API.
624 #ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
625 #define AO2_GLOBAL_OBJ_STATIC(name) \
626 struct ao2_global_obj name; \
627 static void __attribute__((constructor)) __init_##name(void) \
629 ast_rwlock_init(&name.lock); \
632 static void __attribute__((destructor)) __fini_##name(void) \
635 ao2_ref(name.obj, -1); \
638 ast_rwlock_destroy(&name.lock); \
640 struct __dummy_##name
642 #define AO2_GLOBAL_OBJ_STATIC(name) \
643 struct ao2_global_obj name = { \
644 .lock = AST_RWLOCK_INIT_VALUE, \
649 * \brief Release the ao2 object held in the global holder.
652 * \param holder Global ao2 object holder.
653 * \param tag used for debugging
658 #define ao2_t_global_obj_release(holder, tag) \
659 __ao2_global_obj_release(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
660 #define ao2_global_obj_release(holder) \
661 __ao2_global_obj_release(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
665 #define ao2_t_global_obj_release(holder, tag) \
666 __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
667 #define ao2_global_obj_release(holder) \
668 __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
671 void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
674 * \brief Replace an ao2 object in the global holder.
677 * \param holder Global ao2 object holder.
678 * \param obj Object to put into the holder. Can be NULL.
679 * \param tag used for debugging
681 * \note This function automatically increases the reference
682 * count to account for the reference that the global holder now
683 * holds to the object.
685 * \retval Reference to previous global ao2 object stored.
686 * \retval NULL if no object available.
689 #define ao2_t_global_obj_replace(holder, obj, tag) \
690 __ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
691 #define ao2_global_obj_replace(holder, obj) \
692 __ao2_global_obj_replace(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
696 #define ao2_t_global_obj_replace(holder, obj, tag) \
697 __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
698 #define ao2_global_obj_replace(holder, obj) \
699 __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
702 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;
705 * \brief Replace an ao2 object in the global holder, throwing away any old object.
708 * \param holder Global ao2 object holder.
709 * \param obj Object to put into the holder. Can be NULL.
710 * \param tag used for debugging
712 * \note This function automatically increases the reference
713 * count to account for the reference that the global holder now
714 * holds to the object. It also decreases the reference count
715 * of any object being replaced.
717 * \retval 0 The global object was previously empty
718 * \retval 1 The global object was not previously empty
721 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
722 __ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
723 #define ao2_global_obj_replace_unref(holder, obj) \
724 __ao2_global_obj_replace_unref(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
728 #define ao2_t_global_obj_replace_unref(holder, obj, tag) \
729 __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
730 #define ao2_global_obj_replace_unref(holder, obj) \
731 __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
734 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);
737 * \brief Get a reference to the object stored in the global holder.
740 * \param holder Global ao2 object holder.
741 * \param tag used for debugging
743 * \retval Reference to current ao2 object stored in the holder.
744 * \retval NULL if no object available.
747 #define ao2_t_global_obj_ref(holder, tag) \
748 __ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
749 #define ao2_global_obj_ref(holder) \
750 __ao2_global_obj_ref(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
754 #define ao2_t_global_obj_ref(holder, tag) \
755 __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
756 #define ao2_global_obj_ref(holder) \
757 __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
760 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;
764 \page AstObj2_Containers AstObj2 Containers
766 Containers are data structures meant to store several objects,
767 and perform various operations on them.
768 Internally, objects are stored in lists, hash tables or other
769 data structures depending on the needs.
771 \note NOTA BENE: at the moment the only container we support is the
772 hash table and its degenerate form, the list.
774 Operations on container include:
776 - c = \b ao2_container_alloc(size, hash_fn, cmp_fn)
777 allocate a container with desired size and default compare
779 -The compare function returns an int, which
780 can be 0 for not found, CMP_STOP to stop end a traversal,
781 or CMP_MATCH if they are equal
782 -The hash function returns an int. The hash function
783 takes two argument, the object pointer and a flags field,
785 - \b ao2_find(c, arg, flags)
786 returns zero or more elements matching a given criteria
787 (specified as arg). 'c' is the container pointer. Flags
789 OBJ_UNLINK - to remove the object, once found, from the container.
790 OBJ_NODATA - don't return the object if found (no ref count change)
791 OBJ_MULTIPLE - don't stop at first match
792 OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
793 search will be done. If not, a traversal is done.
794 OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
795 Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
796 OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
797 Similar to OBJ_SEARCH_KEY and mutually exclusive.
799 - \b ao2_callback(c, flags, fn, arg)
800 apply fn(obj, arg) to all objects in the container.
801 Similar to find. fn() can tell when to stop, and
802 do anything with the object including unlinking it.
803 - c is the container;
805 OBJ_UNLINK - to remove the object, once found, from the container.
806 OBJ_NODATA - don't return the object if found (no ref count change)
807 OBJ_MULTIPLE - don't stop at first match
808 OBJ_SEARCH_OBJECT - if set, 'arg' is an object pointer, and a hash table
809 search will be done. If not, a traversal is done through
810 all the hash table 'buckets'..
811 OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
812 Similar to OBJ_SEARCH_OBJECT and mutually exclusive.
813 OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
814 Similar to OBJ_SEARCH_KEY and mutually exclusive.
815 - fn is a func that returns int, and takes 3 args:
816 (void *obj, void *arg, int flags);
818 arg is the same as arg passed into ao2_callback
819 flags is the same as flags passed into ao2_callback
821 0: no match, keep going
822 CMP_STOP: stop search, no match
823 CMP_MATCH: This object is matched.
825 Note that the entire operation is run with the container
826 locked, so nobody else can change its content while we work on it.
827 However, we pay this with the fact that doing
828 anything blocking in the callback keeps the container
830 The mechanism is very flexible because the callback function fn()
831 can do basically anything e.g. counting, deleting records, etc.
832 possibly using arg to store the results.
834 - \b iterate on a container
835 this is done with the following sequence
839 struct ao2_container *c = ... // our container
840 struct ao2_iterator i;
843 i = ao2_iterator_init(c, flags);
845 while ((o = ao2_iterator_next(&i))) {
846 ... do something on o ...
850 ao2_iterator_destroy(&i);
853 The difference with the callback is that the control
854 on how to iterate is left to us.
857 dropping a reference to a container destroys it, very simple!
859 Containers are ao2 objects themselves, and this is why their
860 implementation is simple too.
862 Before declaring containers, we need to declare the types of the
863 arguments passed to the constructor - in turn, this requires
864 to define callback and hash functions and their arguments.
871 * A callback function will return a combination of CMP_MATCH and CMP_STOP.
872 * The latter will terminate the search in a container.
875 CMP_MATCH = 0x1, /*!< the object matches the request */
876 CMP_STOP = 0x2, /*!< stop the search now */
880 * \brief Flags passed to ao2_callback_fn(), ao2_hash_fn(), and ao2_sort_fn() to modify behaviour.
884 * Unlink the object for which the callback function returned
887 OBJ_UNLINK = (1 << 0),
889 * On match, don't return the object hence do not increase its
892 OBJ_NODATA = (1 << 1),
894 * Don't stop at the first match in ao2_callback() unless the
895 * result of of the callback function has the CMP_STOP bit set.
897 OBJ_MULTIPLE = (1 << 2),
899 * \brief Continue if a match is not found.
902 * This flag forces a whole container search. The
903 * OBJ_SEARCH_OBJECT, OBJ_SEARCH_KEY, and OBJ_SEARCH_PARTIAL_KEY
904 * flags just specify where to start the search in the
905 * container. If the search is not stopped early then the
906 * search _continues_ until the search wraps around to the
909 * Normal searches start where the search key specifies to start
910 * and end when the search key indicates that the object is not
913 * For hash containers, this tells the ao2_callback() core to
914 * keep searching through the rest of the buckets if a match is
915 * not found in the starting bucket defined by the hash value on
918 * For rbtree containers, if the search key is not in the
919 * container, the search will start either at the first item
920 * before the search key or the first item after the search key.
922 * \note The supplied ao2_callback_fn is called for every node
923 * in the container from the starting point.
925 OBJ_CONTINUE = (1 << 3),
927 * \brief Assume that the ao2_container is already locked.
929 * \note For ao2_containers that have mutexes, no locking will
932 * \note For ao2_containers that have RWLOCKs, the lock will be
933 * promoted to write mode as needed. The lock will be returned
934 * to the original locked state.
936 * \note Only use this flag if the ao2_container is manually
939 OBJ_NOLOCK = (1 << 4),
942 * \brief Search option field mask.
944 * \todo Eventually OBJ_SEARCH_MASK will shrink to a two bit
945 * field when the codebase is made to use the search field
946 * values as a field instead of independent bits.
948 OBJ_SEARCH_MASK = (0x07 << 5),
949 /*! \brief The arg parameter has no meaning to the astobj2 code. */
950 OBJ_SEARCH_NONE = (0 << 5),
952 * \brief The arg parameter is an object of the same type.
955 * The arg parameter is an object of the same type as the one
956 * being searched for, so use the object's ao2_hash_fn and/or
957 * ao2_sort_fn functions for optimized searching.
959 * \note The supplied ao2_callback_fn is called after the
960 * container nodes have been filtered by the ao2_hash_fn and/or
961 * ao2_sort_fn functions.
963 OBJ_SEARCH_OBJECT = (1 << 5),
965 * \brief The arg parameter is a search key, but is not an object.
968 * This can be used when you want to be able to pass custom data
969 * to the container's stored ao2_hash_fn, ao2_sort_fn, and
970 * ao2_find ao2_callback_fn functions that is not a full object,
971 * but perhaps just a string.
973 * \note The supplied ao2_callback_fn is called after the
974 * container nodes have been filtered by the ao2_hash_fn and/or
975 * ao2_sort_fn functions.
977 OBJ_SEARCH_KEY = (2 << 5),
979 * \brief The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
982 * The partial key can be used by the ao2_sort_fn to guide the
983 * search to find a contiguous subset of a sorted container.
984 * For example, a sorted container holds: "A", "B", "Bert",
985 * "Beth", "Earnie". Doing a partial key search with "B" will
986 * find the sorted subset of all held objects starting with "B".
988 * \note The supplied ao2_callback_fn is called after the
989 * container nodes have been filtered by the ao2_sort_fn
992 OBJ_SEARCH_PARTIAL_KEY = (4 << 5),
994 /*! \brief Traverse order option field mask. */
995 OBJ_ORDER_MASK = (0x03 << 8),
996 /*! \brief Traverse in ascending order (First to last container object) */
997 OBJ_ORDER_ASCENDING = (0 << 8),
998 /*! \brief Traverse in descending order (Last to first container object) */
999 OBJ_ORDER_DESCENDING = (1 << 8),
1001 * \brief Traverse in pre-order (Node then children, for tree container)
1003 * \note For non-tree containers, it is up to the container type
1004 * to make the best interpretation of the order. For list and
1005 * hash containers, this also means ascending order because a
1006 * binary tree can degenerate into a list.
1008 OBJ_ORDER_PRE = (2 << 8),
1010 * \brief Traverse in post-order (Children then node, for tree container)
1012 * \note For non-tree containers, it is up to the container type
1013 * to make the best interpretation of the order. For list and
1014 * hash containers, this also means descending order because a
1015 * binary tree can degenerate into a list.
1017 OBJ_ORDER_POST = (3 << 8),
1021 * Deprecated backward compatible flag names.
1023 * Note: OBJ_POINTER, OBJ_KEY, and OBJ_PARTIAL_KEY are mutually
1026 #define OBJ_POINTER OBJ_SEARCH_OBJECT /*!< Deprecated name */
1027 #define OBJ_KEY OBJ_SEARCH_KEY /*!< Deprecated name */
1028 #define OBJ_PARTIAL_KEY OBJ_SEARCH_PARTIAL_KEY /*!< Deprecated name */
1031 * \brief Options available when allocating an ao2 container object.
1033 * \note Each option is open to some interpretation by the
1034 * container type as long as it makes sense with the option
1037 enum ao2_container_opts {
1039 * \brief Insert objects at the beginning of the container.
1040 * (Otherwise it is the opposite; insert at the end.)
1042 * \note If an ao2_sort_fn is provided, the object is inserted
1043 * before any objects with duplicate keys.
1045 * \note Hash containers insert the object in the computed hash
1046 * bucket in the indicated manner.
1048 AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN = (1 << 0),
1051 * \brief The ao2 container objects with duplicate keys option field mask.
1053 AO2_CONTAINER_ALLOC_OPT_DUPS_MASK = (3 << 1),
1055 * \brief Allow objects with duplicate keys in container.
1057 AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW = (0 << 1),
1059 * \brief Reject objects with duplicate keys in container.
1061 * \note The container must be sorted. i.e. have an
1064 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT = (1 << 1),
1066 * \brief Reject duplicate objects in container.
1068 * \details Don't link the same object into the container twice.
1069 * However, you can link a different object with the same key.
1071 * \note The container must be sorted. i.e. have an
1074 * \note It is assumed that the objects are located where the
1075 * search key says they should be located.
1077 AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT = (2 << 1),
1079 * \brief Replace objects with duplicate keys in container.
1081 * \details The existing duplicate object is removed and the new
1082 * object takes the old object's place.
1084 * \note The container must be sorted. i.e. have an
1087 AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE = (3 << 1),
1091 * \brief Type of a generic callback function
1092 * \param obj pointer to the (user-defined part) of an object.
1093 * \param arg callback argument from ao2_callback()
1094 * \param flags flags from ao2_callback()
1095 * OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
1096 * OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
1097 * OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
1099 * The return values are a combination of enum _cb_results.
1100 * Callback functions are used to search or manipulate objects in a container.
1102 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags);
1104 /*! \brief A common ao2_callback is one that matches by address. */
1105 int ao2_match_by_addr(void *obj, void *arg, int flags);
1108 * \brief Type of a generic callback function
1109 * \param obj pointer to the (user-defined part) of an object.
1110 * \param arg callback argument from ao2_callback()
1111 * \param data arbitrary data from ao2_callback()
1112 * \param flags flags from ao2_callback()
1113 * OBJ_SEARCH_OBJECT - if set, 'arg', is an object.
1114 * OBJ_SEARCH_KEY - if set, 'arg', is a search key item that is not an object.
1115 * OBJ_SEARCH_PARTIAL_KEY - if set, 'arg', is a partial search key item that is not an object.
1117 * The return values are a combination of enum _cb_results.
1118 * Callback functions are used to search or manipulate objects in a container.
1120 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags);
1123 * Type of a generic function to generate a hash value from an object.
1125 * \param obj pointer to the (user-defined part) of an object.
1126 * \param flags flags from ao2_callback()
1127 * OBJ_SEARCH_OBJECT - if set, 'obj', is an object.
1128 * OBJ_SEARCH_KEY - if set, 'obj', is a search key item that is not an object.
1130 * \return Computed hash value.
1132 typedef int (ao2_hash_fn)(const void *obj, int flags);
1135 * \brief Type of generic container sort function.
1137 * \param obj_left pointer to the (user-defined part) of an object.
1138 * \param obj_right pointer to the (user-defined part) of an object.
1139 * \param flags flags from ao2_callback()
1140 * OBJ_SEARCH_OBJECT - if set, 'obj_right', is an object.
1141 * OBJ_SEARCH_KEY - if set, 'obj_right', is a search key item that is not an object.
1142 * OBJ_SEARCH_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
1144 * \retval <0 if obj_left < obj_right
1145 * \retval =0 if obj_left == obj_right
1146 * \retval >0 if obj_left > obj_right
1148 typedef int (ao2_sort_fn)(const void *obj_left, const void *obj_right, int flags);
1150 /*! \name Object Containers
1151 * Here start declarations of containers.
1154 struct ao2_container;
1157 * \brief Allocate and initialize a hash container with the desired number of buckets.
1160 * We allocate space for a struct astobj_container, struct container
1161 * and the buckets[] array.
1163 * \param options Container ao2 object options (See enum ao2_alloc_opts)
1164 * \param n_buckets Number of buckets for hash
1165 * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
1166 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1167 * \param tag used for debugging.
1169 * \return A pointer to a struct container.
1171 * \note Destructor is set implicitly.
1172 * \note This is legacy container creation that is mapped to the new method.
1175 #define ao2_t_container_alloc_options(options, n_buckets, hash_fn, cmp_fn, tag) \
1176 ao2_t_container_alloc_hash((options), 0, (n_buckets), (hash_fn), NULL, (cmp_fn), (tag))
1177 #define ao2_container_alloc_options(options, n_buckets, hash_fn, cmp_fn) \
1178 ao2_container_alloc_hash((options), 0, (n_buckets), (hash_fn), NULL, (cmp_fn))
1180 #define ao2_t_container_alloc(n_buckets, hash_fn, cmp_fn, tag) \
1181 ao2_t_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn), (tag))
1182 #define ao2_container_alloc(n_buckets, hash_fn, cmp_fn) \
1183 ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, (n_buckets), (hash_fn), (cmp_fn))
1186 * \brief Allocate and initialize a hash container with the desired number of buckets.
1189 * We allocate space for a struct astobj_container, struct container
1190 * and the buckets[] array.
1192 * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1193 * \param container_options Container behaviour options (See enum ao2_container_opts)
1194 * \param n_buckets Number of buckets for hash
1195 * \param hash_fn Pointer to a function computing a hash value. (NULL if everyting goes in first bucket.)
1196 * \param sort_fn Pointer to a sort function. (NULL to not sort the buckets.)
1197 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1198 * \param tag used for debugging.
1200 * \return A pointer to a struct container.
1202 * \note Destructor is set implicitly.
1205 #if defined(REF_DEBUG)
1207 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
1208 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1209 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1210 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1212 #elif defined(__AST_DEBUG_MALLOC)
1214 #define ao2_t_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn, tag) \
1215 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1216 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1217 __ao2_container_alloc_hash_debug((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
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((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn))
1223 #define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn) \
1224 __ao2_container_alloc_hash((ao2_options), (container_options), (n_buckets), (hash_fn), (sort_fn), (cmp_fn))
1228 struct ao2_container *__ao2_container_alloc_hash(unsigned int ao2_options,
1229 unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
1230 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
1231 struct ao2_container *__ao2_container_alloc_hash_debug(unsigned int ao2_options,
1232 unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn,
1233 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1234 const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1237 * \brief Allocate and initialize a list container.
1239 * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1240 * \param container_options Container behaviour options (See enum ao2_container_opts)
1241 * \param sort_fn Pointer to a sort function. (NULL if list not sorted.)
1242 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1243 * \param tag used for debugging.
1245 * \return A pointer to a struct container.
1247 * \note Destructor is set implicitly.
1248 * \note Implemented as a degenerate hash table.
1251 #if defined(REF_DEBUG)
1253 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1254 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1255 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1256 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1258 #elif defined(__AST_DEBUG_MALLOC)
1260 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1261 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1262 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1263 __ao2_container_alloc_list_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1267 #define ao2_t_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1268 __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn))
1269 #define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn) \
1270 __ao2_container_alloc_list((ao2_options), (container_options), (sort_fn), (cmp_fn))
1274 struct ao2_container *__ao2_container_alloc_list(unsigned int ao2_options,
1275 unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
1276 struct ao2_container *__ao2_container_alloc_list_debug(unsigned int ao2_options,
1277 unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1278 const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1281 * \brief Allocate and initialize a red-black tree container.
1283 * \param ao2_options Container ao2 object options (See enum ao2_alloc_opts)
1284 * \param container_options Container behaviour options (See enum ao2_container_opts)
1285 * \param sort_fn Pointer to a sort function.
1286 * \param cmp_fn Pointer to a compare function used by ao2_find. (NULL to match everything)
1287 * \param tag used for debugging.
1289 * \return A pointer to a struct container.
1291 * \note Destructor is set implicitly.
1294 #if defined(REF_DEBUG)
1296 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1297 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1298 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1299 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1301 #elif defined(__AST_DEBUG_MALLOC)
1303 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1304 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1305 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1306 __ao2_container_alloc_rbtree_debug((ao2_options), (container_options), (sort_fn), (cmp_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1310 #define ao2_t_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn, tag) \
1311 __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
1312 #define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn) \
1313 __ao2_container_alloc_rbtree((ao2_options), (container_options), (sort_fn), (cmp_fn))
1317 struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options,
1318 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) attribute_warn_unused_result;
1319 struct ao2_container *__ao2_container_alloc_rbtree_debug(unsigned int ao2_options, unsigned int container_options,
1320 ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn,
1321 const char *tag, const char *file, int line, const char *func, int ref_debug) attribute_warn_unused_result;
1324 * Returns the number of elements in a container.
1326 int ao2_container_count(struct ao2_container *c);
1329 * \brief Copy all object references in the src container into the dest container.
1332 * \param dest Container to copy src object references into.
1333 * \param src Container to copy all object references from.
1334 * \param flags OBJ_NOLOCK if a lock is already held on both containers.
1335 * Otherwise, the src container is locked first.
1337 * \pre The dest container must be empty. If the duplication fails, the
1338 * dest container will be returned empty.
1340 * \note This can potentially be expensive because a malloc is
1341 * needed for every object in the src container.
1343 * \retval 0 on success.
1344 * \retval -1 on error.
1346 int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags);
1349 * \brief Create a clone/copy of the given container.
1352 * \param orig Container to copy all object references from.
1353 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1355 * \note This can potentially be expensive because a malloc is
1356 * needed for every object in the orig container.
1358 * \retval Clone container on success.
1359 * \retval NULL on error.
1361 struct ao2_container *__ao2_container_clone(struct ao2_container *orig, enum search_flags flags) attribute_warn_unused_result;
1362 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;
1363 #if defined(REF_DEBUG)
1365 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1366 #define ao2_container_clone(orig, flags) __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
1368 #elif defined(__AST_DEBUG_MALLOC)
1370 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone_debug(orig, flags, tag, __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1371 #define ao2_container_clone(orig, flags) __ao2_container_clone_debug(orig, flags, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
1375 #define ao2_t_container_clone(orig, flags, tag) __ao2_container_clone(orig, flags)
1376 #define ao2_container_clone(orig, flags) __ao2_container_clone(orig, flags)
1381 * \brief Print output.
1384 * \param where User data pointer needed to determine where to put output.
1385 * \param fmt printf type format string.
1389 typedef void (ao2_prnt_fn)(void *where, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
1392 * \brief Print object key.
1395 * \param v_obj A pointer to the object we want the key printed.
1396 * \param where User data needed by prnt to determine where to put output.
1397 * \param prnt Print output callback function to use.
1401 typedef void (ao2_prnt_obj_fn)(void *v_obj, void *where, ao2_prnt_fn *prnt);
1404 * \brief Display contents of the specified container.
1407 * \param self Container to dump.
1408 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1409 * \param name Container name. (NULL if anonymous)
1410 * \param where User data needed by prnt to determine where to put output.
1411 * \param prnt Print output callback function to use.
1412 * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
1416 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);
1419 * \brief Display statistics of the specified container.
1422 * \param self Container to display statistics.
1423 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1424 * \param name Container name. (NULL if anonymous)
1425 * \param where User data needed by prnt to determine where to put output.
1426 * \param prnt Print output callback function to use.
1430 void ao2_container_stats(struct ao2_container *self, enum search_flags flags, const char *name, void *where, ao2_prnt_fn *prnt);
1433 * \brief Perform an integrity check on the specified container.
1436 * \param self Container to check integrity.
1437 * \param flags OBJ_NOLOCK if a lock is already held on the container.
1439 * \retval 0 on success.
1440 * \retval -1 on error.
1442 int ao2_container_check(struct ao2_container *self, enum search_flags flags);
1445 * \brief Register a container for CLI stats and integrity check.
1448 * \param name Name to register the container under.
1449 * \param self Container to register.
1450 * \param prnt_obj Callback function to print the given object's key. (NULL if not available)
1452 * \retval 0 on success.
1453 * \retval -1 on error.
1455 int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj);
1458 * \brief Unregister a container for CLI stats and integrity check.
1461 * \param name Name the container is registered under.
1465 void ao2_container_unregister(const char *name);
1469 /*! \name Object Management
1470 * Here we have functions to manage objects.
1472 * We can use the functions below on any kind of
1473 * object defined by the user.
1478 * \brief Add an object to a container.
1480 * \param container The container to operate on.
1481 * \param obj The object to be added.
1482 * \param tag used for debugging.
1484 * \retval 0 on errors.
1485 * \retval 1 on success.
1487 * This function inserts an object in a container according its key.
1489 * \note Remember to set the key before calling this function.
1491 * \note This function automatically increases the reference count to account
1492 * for the reference that the container now holds to the object.
1496 #define ao2_t_link(container, obj, tag) __ao2_link_debug((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1497 #define ao2_link(container, obj) __ao2_link_debug((container), (obj), 0, "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1500 * \brief Add an object to a container.
1502 * \param container The container to operate on.
1503 * \param obj The object to be added.
1504 * \param flags search_flags to control linking the object. (OBJ_NOLOCK)
1505 * \param tag used for debugging.
1507 * \retval 0 on errors.
1508 * \retval 1 on success.
1510 * This function inserts an object in a container according its key.
1512 * \note Remember to set the key before calling this function.
1514 * \note This function automatically increases the reference count to account
1515 * for the reference that the container now holds to the object.
1517 #define ao2_t_link_flags(container, obj, flags, tag) __ao2_link_debug((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1518 #define ao2_link_flags(container, obj, flags) __ao2_link_debug((container), (obj), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1522 #define ao2_t_link(container, obj, tag) __ao2_link((container), (obj), 0)
1523 #define ao2_link(container, obj) __ao2_link((container), (obj), 0)
1525 #define ao2_t_link_flags(container, obj, flags, tag) __ao2_link((container), (obj), (flags))
1526 #define ao2_link_flags(container, obj, flags) __ao2_link((container), (obj), (flags))
1530 int __ao2_link_debug(struct ao2_container *c, void *obj_new, int flags, const char *tag, const char *file, int line, const char *func);
1531 int __ao2_link(struct ao2_container *c, void *obj_new, int flags);
1534 * \brief Remove an object from a container
1536 * \param container The container to operate on.
1537 * \param obj The object to unlink.
1538 * \param tag used for debugging.
1540 * \retval NULL, always
1542 * \note The object requested to be unlinked must be valid. However, if it turns
1543 * out that it is not in the container, this function is still safe to
1546 * \note If the object gets unlinked from the container, the container's
1547 * reference to the object will be automatically released. (The
1548 * refcount will be decremented).
1552 #define ao2_t_unlink(container, obj, tag) __ao2_unlink_debug((container), (obj), 0, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1553 #define ao2_unlink(container, obj) __ao2_unlink_debug((container), (obj), 0, "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1556 * \brief Remove an object from a container
1558 * \param container The container to operate on.
1559 * \param obj The object to unlink.
1560 * \param flags search_flags to control unlinking the object. (OBJ_NOLOCK)
1561 * \param tag used for debugging.
1563 * \retval NULL, always
1565 * \note The object requested to be unlinked must be valid. However, if it turns
1566 * out that it is not in the container, this function is still safe to
1569 * \note If the object gets unlinked from the container, the container's
1570 * reference to the object will be automatically released. (The
1571 * refcount will be decremented).
1574 #define ao2_t_unlink_flags(container, obj, flags, tag) __ao2_unlink_debug((container), (obj), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1575 #define ao2_unlink_flags(container, obj, flags) __ao2_unlink_debug((container), (obj), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1579 #define ao2_t_unlink(container, obj, tag) __ao2_unlink((container), (obj), 0)
1580 #define ao2_unlink(container, obj) __ao2_unlink((container), (obj), 0)
1582 #define ao2_t_unlink_flags(container, obj, flags, tag) __ao2_unlink((container), (obj), (flags))
1583 #define ao2_unlink_flags(container, obj, flags) __ao2_unlink((container), (obj), (flags))
1587 void *__ao2_unlink_debug(struct ao2_container *c, void *obj, int flags, const char *tag, const char *file, int line, const char *func);
1588 void *__ao2_unlink(struct ao2_container *c, void *obj, int flags);
1594 * ao2_callback() is a generic function that applies cb_fn() to all objects
1595 * in a container, as described below.
1597 * \param c A pointer to the container to operate on.
1598 * \param flags A set of flags specifying the operation to perform,
1599 * partially used by the container code, but also passed to
1601 * - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts
1602 * of any of the traversed objects will be incremented.
1603 * On the converse, if it is NOT set (the default), the ref count
1604 * of the first matching object will be incremented and returned.
1605 * - If OBJ_MULTIPLE is set, the ref count of all matching objects will
1606 * be incremented in an iterator for a temporary container and returned.
1607 * - If OBJ_SEARCH_OBJECT is set, the traversed items will be restricted
1608 * to the objects in the bucket that the object key hashes to.
1609 * - If OBJ_SEARCH_KEY is set, the traversed items will be restricted
1610 * to the objects in the bucket that the object key hashes to.
1611 * \param cb_fn A function pointer, that will be called on all
1612 * objects, to see if they match. This function returns CMP_MATCH
1613 * if the object is matches the criteria; CMP_STOP if the traversal
1614 * should immediately stop, or both (via bitwise ORing), if you find a
1615 * match and want to end the traversal, and 0 if the object is not a match,
1616 * but the traversal should continue. This is the function that is applied
1617 * to each object traversed. Its arguments are:
1618 * (void *obj, void *arg, int flags), where:
1620 * arg is the same as arg passed into ao2_callback
1621 * flags is the same as flags passed into ao2_callback (flags are
1622 * also used by ao2_callback).
1623 * \param arg passed to the callback.
1624 * \param tag used for debugging.
1626 * \retval NULL on failure or no matching object found.
1628 * \retval object found if OBJ_MULTIPLE is not set in the flags
1631 * \retval ao2_iterator pointer if OBJ_MULTIPLE is set in the
1632 * flags parameter. The iterator must be destroyed with
1633 * ao2_iterator_destroy() when the caller no longer needs it.
1635 * If the function returns any objects, their refcount is incremented,
1636 * and the caller is in charge of decrementing them once done.
1638 * Typically, ao2_callback() is used for two purposes:
1639 * - to perform some action (including removal from the container) on one
1640 * or more objects; in this case, cb_fn() can modify the object itself,
1641 * and to perform deletion should set CMP_MATCH on the matching objects,
1642 * and have OBJ_UNLINK set in flags.
1643 * - to look for a specific object in a container; in this case, cb_fn()
1644 * should not modify the object, but just return a combination of
1645 * CMP_MATCH and CMP_STOP on the desired object.
1646 * Other usages are also possible, of course.
1648 * This function searches through a container and performs operations
1649 * on objects according on flags passed.
1650 * XXX describe better
1651 * The comparison is done calling the compare function set implicitly.
1652 * The arg pointer can be a pointer to an object or to a key,
1653 * we can say this looking at flags value.
1654 * If arg points to an object we will search for the object pointed
1655 * by this value, otherwise we search for a key value.
1656 * If the key is not unique we only find the first matching value.
1658 * The use of flags argument is the follow:
1660 * OBJ_UNLINK unlinks the object found
1661 * OBJ_NODATA on match, do return an object
1662 * Callbacks use OBJ_NODATA as a default
1663 * functions such as find() do
1664 * OBJ_MULTIPLE return multiple matches
1666 * OBJ_SEARCH_OBJECT the pointer is to an object
1667 * OBJ_SEARCH_KEY the pointer is to a search key
1668 * OBJ_SEARCH_PARTIAL_KEY the pointer is to a partial search key
1670 * \note When the returned object is no longer in use, ao2_ref() should
1671 * be used to free the additional reference possibly created by this function.
1677 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
1678 __ao2_callback_debug((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1679 #define ao2_callback(c, flags, cb_fn, arg) \
1680 __ao2_callback_debug((c), (flags), (cb_fn), (arg), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1684 #define ao2_t_callback(c, flags, cb_fn, arg, tag) \
1685 __ao2_callback((c), (flags), (cb_fn), (arg))
1686 #define ao2_callback(c, flags, cb_fn, arg) \
1687 __ao2_callback((c), (flags), (cb_fn), (arg))
1691 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags,
1692 ao2_callback_fn *cb_fn, void *arg, const char *tag, const char *file, int line,
1694 void *__ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg);
1699 * ao2_callback_data() is a generic function that applies cb_fn() to all objects
1700 * in a container. It is functionally identical to ao2_callback() except that
1701 * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and
1702 * allows the caller to pass in arbitrary data.
1704 * This call would be used instead of ao2_callback() when the caller needs to pass
1705 * OBJ_SEARCH_OBJECT, OBJ_SEARCH_KEY, or OBJ_SEARCH_PARTIAL_KEY as part of the flags
1706 * argument (which in turn requires passing in a known pointer type for 'arg') and
1707 * also needs access to other non-global data to complete it's comparison or task.
1709 * See the documentation for ao2_callback() for argument descriptions.
1711 * \see ao2_callback()
1715 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
1716 __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1717 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
1718 __ao2_callback_data_debug((container), (flags), (cb_fn), (arg), (data), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1722 #define ao2_t_callback_data(container, flags, cb_fn, arg, data, tag) \
1723 __ao2_callback_data((container), (flags), (cb_fn), (arg), (data))
1724 #define ao2_callback_data(container, flags, cb_fn, arg, data) \
1725 __ao2_callback_data((container), (flags), (cb_fn), (arg), (data))
1729 void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags,
1730 ao2_callback_data_fn *cb_fn, void *arg, void *data, const char *tag, const char *file,
1731 int line, const char *func);
1732 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
1733 ao2_callback_data_fn *cb_fn, void *arg, void *data);
1735 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg)
1736 * XXX possibly change order of arguments ?
1740 #define ao2_t_find(container, arg, flags, tag) \
1741 __ao2_find_debug((container), (arg), (flags), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1742 #define ao2_find(container, arg, flags) \
1743 __ao2_find_debug((container), (arg), (flags), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1747 #define ao2_t_find(container, arg, flags, tag) \
1748 __ao2_find((container), (arg), (flags))
1749 #define ao2_find(container, arg, flags) \
1750 __ao2_find((container), (arg), (flags))
1754 void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags,
1755 const char *tag, const char *file, int line, const char *func);
1756 void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags);
1761 * When we need to walk through a container, we use an
1762 * ao2_iterator to keep track of the current position.
1764 * Because the navigation is typically done without holding the
1765 * lock on the container across the loop, objects can be
1766 * inserted or deleted or moved while we work. As a
1767 * consequence, there is no guarantee that we manage to touch
1768 * all the elements in the container, and it is possible that we
1769 * touch the same object multiple times.
1771 * An iterator must be first initialized with
1772 * ao2_iterator_init(), then we can use o = ao2_iterator_next()
1773 * to move from one element to the next. Remember that the
1774 * object returned by ao2_iterator_next() has its refcount
1775 * incremented, and the reference must be explicitly released
1776 * when done with it.
1778 * In addition, ao2_iterator_init() will hold a reference to the
1779 * container being iterated and the last container node found.
1780 * Thes objects will be unreffed when ao2_iterator_destroy() is
1781 * called to free up the resources used by the iterator (if
1788 * struct ao2_container *c = ... // the container we want to iterate on
1789 * struct ao2_iterator i;
1792 * i = ao2_iterator_init(c, flags);
1794 * while ((o = ao2_iterator_next(&i))) {
1795 * ... do something on o ...
1799 * ao2_iterator_restart(&i);
1800 * while ((o = ao2_iterator_next(&i))) {
1801 * ... do something on o ...
1805 * ao2_iterator_destroy(&i);
1812 * \brief The astobj2 iterator
1814 * \note You are not supposed to know the internals of an iterator!
1815 * We would like the iterator to be opaque, unfortunately
1816 * its size needs to be known if we want to store it around
1817 * without too much trouble.
1819 * The iterator has a pointer to the container, and a flags
1820 * field specifying various things e.g. whether the container
1821 * should be locked or not while navigating on it.
1822 * The iterator "points" to the current container node.
1824 * Details are in the implementation of ao2_iterator_next()
1826 struct ao2_iterator {
1827 /*! The container (Has a reference) */
1828 struct ao2_container *c;
1829 /*! Last container node (Has a reference) */
1831 /*! Nonzero if the iteration has completed. */
1833 /*! operation flags (enum ao2_iterator_flags) */
1837 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
1840 enum ao2_iterator_flags {
1842 * \brief Assume that the ao2_container is already locked.
1844 * \note For ao2_containers that have mutexes, no locking will
1847 * \note For ao2_containers that have RWLOCKs, the lock will be
1848 * promoted to write mode as needed. The lock will be returned
1849 * to the original locked state.
1851 * \note Only use this flag if the ao2_container is manually
1852 * locked already. You should hold the lock until after
1853 * ao2_iterator_destroy(). If you must release the lock then
1854 * you must at least hold the lock whenever you call an
1855 * ao2_iterator_xxx function with this iterator.
1857 AO2_ITERATOR_DONTLOCK = (1 << 0),
1859 * Indicates that the iterator was dynamically allocated by
1860 * astobj2 API and should be freed by ao2_iterator_destroy().
1862 AO2_ITERATOR_MALLOCD = (1 << 1),
1864 * Indicates that before the iterator returns an object from
1865 * the container being iterated, the object should be unlinked
1866 * from the container.
1868 AO2_ITERATOR_UNLINK = (1 << 2),
1870 * Iterate in descending order (Last to first container object)
1871 * (Otherwise ascending order)
1873 * \note Other traversal orders such as pre-order and post-order
1874 * do not make sense because they require the container
1875 * structure to be static during the traversal. Iterators just
1876 * about guarantee that is not going to happen because the
1877 * container is allowed to change by other threads during the
1880 AO2_ITERATOR_DESCENDING = (1 << 3),
1884 * \brief Create an iterator for a container
1886 * \param c the container
1887 * \param flags one or more flags from ao2_iterator_flags.
1889 * \retval the constructed iterator
1891 * \note This function does \b not take a pointer to an iterator;
1892 * rather, it returns an iterator structure that should be
1893 * assigned to (overwriting) an existing iterator structure
1894 * allocated on the stack or on the heap.
1896 * This function will take a reference on the container being iterated.
1898 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result;
1901 * \brief Destroy a container iterator
1903 * \param iter the iterator to destroy
1907 * This function will release the container reference held by the iterator
1908 * and any other resources it may be holding.
1910 #if defined(TEST_FRAMEWORK)
1911 void ao2_iterator_destroy(struct ao2_iterator *iter) __attribute__((noinline));
1913 void ao2_iterator_destroy(struct ao2_iterator *iter);
1914 #endif /* defined(TEST_FRAMEWORK) */
1918 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next_debug((iter), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1919 #define ao2_iterator_next(iter) __ao2_iterator_next_debug((iter), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1923 #define ao2_t_iterator_next(iter, tag) __ao2_iterator_next((iter))
1924 #define ao2_iterator_next(iter) __ao2_iterator_next((iter))
1928 void *__ao2_iterator_next_debug(struct ao2_iterator *iter, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result;
1929 void *__ao2_iterator_next(struct ao2_iterator *iter) attribute_warn_unused_result;
1932 * \brief Restart an iteration.
1934 * \param iter the iterator to restart
1936 * \note A restart is not going to have any effect if the
1937 * iterator was created with the AO2_ITERATOR_UNLINK flag. Any
1938 * previous objects returned were removed from the container.
1942 void ao2_iterator_restart(struct ao2_iterator *iter);
1944 /* extra functions */
1945 void ao2_bt(void); /* backtrace */
1947 /*! gcc __attribute__(cleanup()) functions
1948 * \note they must be able to handle NULL parameters because most of the
1949 * allocation/find functions can fail and we don't want to try to tear
1951 void __ao2_cleanup(void *obj);
1952 void __ao2_cleanup_debug(void *obj, const char *file, int line, const char *function);
1954 #define ao2_cleanup(obj) __ao2_cleanup_debug((obj), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1956 #define ao2_cleanup(obj) __ao2_cleanup(obj)
1958 void ao2_iterator_cleanup(struct ao2_iterator *iter);
1960 #endif /* _ASTERISK_ASTOBJ2_H */