Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


astobj2.h
Go to the documentation of this file.
1 /*
2  * astobj2 - replacement containers for asterisk data structures.
3  *
4  * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 #ifndef _ASTERISK_ASTOBJ2_H
18 #define _ASTERISK_ASTOBJ2_H
19 
20 #include "asterisk/compat.h"
21 #include "asterisk/linkedlists.h"
22 
23 /*! \file
24  * \ref AstObj2
25  *
26  * \page AstObj2 Object Model implementing objects and containers.
27 
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.
31 
32 The internal implementation of objects and containers is opaque to the user,
33 so we can use different data structures as needs arise.
34 
35 \section AstObj2_UsageObjects USAGE - OBJECTS
36 
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
44 of freeing them.
45 As an additional feature, ao2 objects are associated to individual
46 locks.
47 
48 Creating an object requires the size of the object and
49 and a pointer to the destructor function:
50 
51  struct foo *o;
52 
53  o = ao2_alloc(sizeof(struct foo), my_destructor_fn);
54 
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.
58 
59 On return from ao2_alloc():
60 
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:
66 
67  ao2_ref(o, -1)
68 
69  causing the destructor to be called (and then memory freed) when
70  the refcount goes to 0.
71 
72 - ao2_ref(o, +1) can be used to modify the refcount on the
73  object in case we want to pass it around.
74 
75 - ao2_lock(obj), ao2_unlock(obj), ao2_trylock(obj) can be used
76  to manipulate the lock associated with the object.
77 
78 
79 \section AstObj2_UsageContainers USAGE - CONTAINERS
80 
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
84 itself.
85 
86 A container must first be allocated, specifying the initial
87 parameters. At the moment, this is done as follows:
88 
89  <b>Sample Usage:</b>
90  \code
91 
92  struct ao2_container *c;
93 
94  c = ao2_container_alloc(MAX_BUCKETS, my_hash_fn, my_cmp_fn);
95  \endcode
96 
97 where
98 
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,
105 
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().
111 
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
116 
117  ao2_link(c, o);
118 
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
123 automatically.
124 
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 reinserting it after the change (this is not terribly expensive).
131 
132 \note A container with a single buckets is effectively a linked
133 list. However there is no ordering among elements.
134 
135 - \ref AstObj2_Containers
136 - \ref astobj2.h All documentation for functions and data structures
137 
138  */
139 
140 /*
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.
148 
149  these ao2_t_xxxx variants are provided:
150 
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)
159 
160 If you study each argument list, you will see that these functions all have
161 one extra argument that 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.
165 
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:
168 ...
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
187 ...
188 
189 This uses a comma delineated format. The columns in the format are as
190 follows:
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
195  by.
196 - The third column is the ID of the thread that modified the reference
197  count.
198 - The fourth column is the source file that the change in reference was
199  issued from.
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
203  issued from.
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.
211 
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:
216 
217 #ifdef REF_DEBUG
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, char *tag, const char *file, int line, const char *func)
221 {
222  if (p)
223  ao2_ref_debug(p, 1, tag, file, line, func);
224  else
225  ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
226  return p;
227 }
228 
229 static struct sip_pvt *dialog_unref_debug(struct sip_pvt *p, char *tag, const char *file, int line, const char *func)
230 {
231  if (p)
232  ao2_ref_debug(p, -1, tag, file, line, func);
233  return NULL;
234 }
235 #else
236 static struct sip_pvt *dialog_ref(struct sip_pvt *p, char *tag)
237 {
238  if (p)
239  ao2_ref(p, 1);
240  else
241  ast_log(LOG_ERROR, "Attempt to Ref a null pointer\n");
242  return p;
243 }
244 
245 static struct sip_pvt *dialog_unref(struct sip_pvt *p, char *tag)
246 {
247  if (p)
248  ao2_ref(p, -1);
249  return NULL;
250 }
251 #endif
252 
253 In the above code, note that the "normal" helper funcs call ao2_ref() as
254 normal, and the "helper" functions call ao2_ref_debug directly with the
255 file, function, and line number info provided. You might find this
256 well worth the effort to help track these function calls in the code.
257 
258 To find out why objects are not destroyed (a common bug), you can
259 edit the source file to use the ao2_t_* variants, enable REF_DEBUG
260 in menuselect, and add a descriptive tag to each call. Recompile,
261 and run Asterisk, exit asterisk with "core stop gracefully", which should
262 result in every object being destroyed.
263 
264 Then, you can "sort -k 1 {AST_LOG_DIR}/refs > x1" to get a sorted list of
265 all the objects, or you can use "contrib/script/refcounter.py" to scan
266 the file for you and output any problems it finds.
267 
268 The above may seem astronomically more work than it is worth to debug
269 reference counts, which may be true in "simple" situations, but for
270 more complex situations, it is easily worth 100 times this effort to
271 help find problems.
272 
273 To debug, pair all calls so that each call that increments the
274 refcount is paired with a corresponding call that decrements the
275 count for the same reason. Hopefully, you will be left with one
276 or more unpaired calls. This is where you start your search!
277 
278 For instance, here is an example of this for a dialog object in
279 chan_sip, that was not getting destroyed, after I moved the lines around
280 to pair operations:
281 
282  0x83787a0,+1,1234,chan_sip.c,5733,sip_alloc,**constructor**,(allocate a dialog(pvt) struct)
283  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)
284 
285  0x83787a0,+1,1234,chan_sip.c,5854,sip_alloc,1,(link pvt into dialogs table)
286  0x83787a0,-1,1234,chan_sip.c,19150,sip_poke_peer,3,(About to change the callid -- remove the old name)
287  0x83787a0,+1,1234,chan_sip.c,19152,sip_poke_peer,2,(Linking in under new name)
288  0x83787a0,-1,1234,chan_sip.c,2399,dialog_unlink_all,5,(unlinking dialog via ao2_unlink)
289 
290  0x83787a0,+1,1234,chan_sip.c,19130,sip_poke_peer,2,(copy sip alloc from p to peer->call)
291 
292 
293  0x83787a0,+1,1234,chan_sip.c,2996,__sip_reliable_xmit,3,(__sip_reliable_xmit: setting pkt->owner)
294  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)
295 
296  0x83787a0,+1,1234,chan_sip.c,22356,unload_module,4,(iterate thru dialogs)
297  0x83787a0,-1,1234,chan_sip.c,22359,unload_module,5,(toss dialog ptr from iterator_next)
298 
299 
300  0x83787a0,+1,1234,chan_sip.c,22373,unload_module,3,(iterate thru dialogs)
301  0x83787a0,-1,1234,chan_sip.c,22375,unload_module,2,(throw away iterator result)
302 
303  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)
304  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)
305 
306 As you can see, only one unbalanced operation is in the list, a ref count increment when
307 the peer->call was set, but no corresponding decrement was made...
308 
309 Hopefully this helps you narrow your search and find those bugs.
310 
311 THE ART OF REFERENCE COUNTING
312 (by Steve Murphy)
313 SOME TIPS for complicated code, and ref counting:
314 
315 1. Theoretically, passing a refcounted object pointer into a function
316 call is an act of copying the reference, and could be refcounted.
317 But, upon examination, this sort of refcounting will explode the amount
318 of code you have to enter, and for no tangible benefit, beyond
319 creating more possible failure points/bugs. It will even
320 complicate your code and make debugging harder, slow down your program
321 doing useless increments and decrements of the ref counts.
322 
323 2. It is better to track places where a ref counted pointer
324 is copied into a structure or stored. Make sure to decrement the refcount
325 of any previous pointer that might have been there, if setting
326 this field might erase a previous pointer. ao2_find and iterate_next
327 internally increment the ref count when they return a pointer, so
328 you need to decrement the count before the pointer goes out of scope.
329 
330 3. Any time you decrement a ref count, it may be possible that the
331 object will be destroyed (freed) immediately by that call. If you
332 are destroying a series of fields in a refcounted object, and
333 any of the unref calls might possibly result in immediate destruction,
334 you can first increment the count to prevent such behavior, then
335 after the last test, decrement the pointer to allow the object
336 to be destroyed, if the refcount would be zero.
337 
338 Example:
339 
340  dialog_ref(dialog, "Let's bump the count in the unlink so it doesn't accidentally become dead before we are done");
341 
342  ao2_t_unlink(dialogs, dialog, "unlinking dialog via ao2_unlink");
343 
344  *//* Unlink us from the owner (channel) if we have one *//*
345  if (dialog->owner) {
346  if (lockowner)
347  ast_channel_lock(dialog->owner);
348  ast_debug(1, "Detaching from channel %s\n", dialog->owner->name);
349  dialog->owner->tech_pvt = dialog_unref(dialog->owner->tech_pvt, "resetting channel dialog ptr in unlink_all");
350  if (lockowner)
351  ast_channel_unlock(dialog->owner);
352  }
353  if (dialog->registry) {
354  if (dialog->registry->call == dialog)
355  dialog->registry->call = dialog_unref(dialog->registry->call, "nulling out the registry's call dialog field in unlink_all");
356  dialog->registry = registry_unref(dialog->registry, "delete dialog->registry");
357  }
358  ...
359  dialog_unref(dialog, "Let's unbump the count in the unlink so the poor pvt can disappear if it is time");
360 
361 In the above code, the ao2_t_unlink could end up destroying the dialog
362 object; if this happens, then the subsequent usages of the dialog
363 pointer could result in a core dump. So, we 'bump' the
364 count upwards before beginning, and then decrementing the count when
365 we are finished. This is analogous to 'locking' or 'protecting' operations
366 for a short while.
367 
368 4. One of the most insidious problems I've run into when converting
369 code to do ref counted automatic destruction, is in the destruction
370 routines. Where a "destroy" routine had previously been called to
371 get rid of an object in non-refcounted code, the new regime demands
372 that you tear that "destroy" routine into two pieces, one that will
373 tear down the links and 'unref' them, and the other to actually free
374 and reset fields. A destroy routine that does any reference deletion
375 for its own object, will never be called. Another insidious problem
376 occurs in mutually referenced structures. As an example, a dialog contains
377 a pointer to a peer, and a peer contains a pointer to a dialog. Watch
378 out that the destruction of one doesn't depend on the destruction of the
379 other, as in this case a dependency loop will result in neither being
380 destroyed!
381 
382 Given the above, you should be ready to do a good job!
383 
384 murf
385 
386 */
387 
388 
389 
390 /*! \brief
391  * Typedef for an object destructor. This is called just before freeing
392  * the memory for the object. It is passed a pointer to the user-defined
393  * data of the object.
394  */
395 typedef void (*ao2_destructor_fn)(void *);
396 
397 
398 /*! \brief
399  * Allocate and initialize an object.
400  *
401  * \param data_size The sizeof() of the user-defined structure.
402  * \param destructor_fn The destructor function (can be NULL)
403  * \param debug_msg
404  * \return A pointer to user-data.
405  *
406  * Allocates a struct astobj2 with sufficient space for the
407  * user-defined structure.
408  * \note
409  * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
410  * - the refcount of the object just created is 1
411  * - the returned pointer cannot be free()'d or realloc()'ed;
412  * rather, we just call ao2_ref(o, -1);
413  *
414  * @{
415  */
416 
417 #if defined(REF_DEBUG)
418 
419 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) __ao2_alloc_debug((data_size), (destructor_fn), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
420 #define ao2_alloc(data_size, destructor_fn) __ao2_alloc_debug((data_size), (destructor_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
421 
422 #elif defined(__AST_DEBUG_MALLOC)
423 
424 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) __ao2_alloc_debug((data_size), (destructor_fn), (debug_msg), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
425 #define ao2_alloc(data_size, destructor_fn) __ao2_alloc_debug((data_size), (destructor_fn), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
426 
427 #else
428 
429 #define ao2_t_alloc(data_size, destructor_fn, debug_msg) __ao2_alloc((data_size), (destructor_fn))
430 #define ao2_alloc(data_size, destructor_fn) __ao2_alloc((data_size), (destructor_fn))
431 
432 #endif
433 
434 void *__ao2_alloc_debug(const size_t data_size, ao2_destructor_fn destructor_fn, char *tag,
435  const char *file, int line, const char *funcname, int ref_debug);
436 void *__ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn);
437 
438 /*! @} */
439 
440 /*! \brief
441  * Reference/unreference an object and return the old refcount.
442  *
443  * \param o A pointer to the object
444  * \param delta Value to add to the reference counter.
445  * \param tag used for debugging
446  * \return The value of the reference counter before the operation.
447  *
448  * Increase/decrease the reference counter according
449  * the value of delta.
450  *
451  * If the refcount goes to zero, the object is destroyed.
452  *
453  * \note The object must not be locked by the caller of this function, as
454  * it is invalid to try to unlock it after releasing the reference.
455  *
456  * \note if we know the pointer to an object, it is because we
457  * have a reference count to it, so the only case when the object
458  * can go away is when we release our reference, and it is
459  * the last one in existence.
460  *
461  * @{
462  */
463 
464 #ifdef REF_DEBUG
465 
466 #define ao2_t_ref(o,delta,tag) __ao2_ref_debug((o), (delta), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
467 #define ao2_ref(o,delta) __ao2_ref_debug((o), (delta), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
468 
469 #else
470 
471 #define ao2_t_ref(o,delta,tag) __ao2_ref((o), (delta))
472 #define ao2_ref(o,delta) __ao2_ref((o), (delta))
473 
474 #endif
475 
476 int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *funcname);
477 int __ao2_ref(void *o, int delta);
478 
479 /*! @} */
480 
481 /*! \brief
482  * Lock an object.
483  *
484  * \param a A pointer to the object we want to lock.
485  * \return 0 on success, other values on error.
486  */
487 int __ao2_lock(void *a, const char *file, const char *func, int line, const char *var);
488 #define ao2_lock(a) __ao2_lock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
489 
490 /*! \brief
491  * Unlock an object.
492  *
493  * \param a A pointer to the object we want unlock.
494  * \return 0 on success, other values on error.
495  */
496 int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var);
497 #define ao2_unlock(a) __ao2_unlock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
498 
499 /*! \brief
500  * Try locking-- (don't block if fail)
501  *
502  * \param a A pointer to the object we want to lock.
503  * \return 0 on success, other values on error.
504  */
505 int __ao2_trylock(void *a, const char *file, const char *func, int line, const char *var);
506 #define ao2_trylock(a) __ao2_trylock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
507 
508 /*!
509  * \brief Return the lock address of an object
510  *
511  * \param[in] obj A pointer to the object we want.
512  * \return the address of the lock, else NULL.
513  *
514  * This function comes in handy mainly for debugging locking
515  * situations, where the locking trace code reports the
516  * lock address, this allows you to correlate against
517  * object address, to match objects to reported locks.
518  *
519  * \since 1.6.1
520  */
521 void *ao2_object_get_lockaddr(void *obj);
522 
523 /*!
524  \page AstObj2_Containers AstObj2 Containers
525 
526 Containers are data structures meant to store several objects,
527 and perform various operations on them.
528 Internally, objects are stored in lists, hash tables or other
529 data structures depending on the needs.
530 
531 \note NOTA BENE: at the moment the only container we support is the
532  hash table and its degenerate form, the list.
533 
534 Operations on container include:
535 
536  - c = \b ao2_container_alloc(size, hash_fn, cmp_fn)
537  allocate a container with desired size and default compare
538  and hash function
539  -The compare function returns an int, which
540  can be 0 for not found, CMP_STOP to stop end a traversal,
541  or CMP_MATCH if they are equal
542  -The hash function returns an int. The hash function
543  takes two argument, the object pointer and a flags field,
544 
545  - \b ao2_find(c, arg, flags)
546  returns zero or more element matching a given criteria
547  (specified as arg). 'c' is the container pointer. Flags
548  can be:
549  OBJ_UNLINK - to remove the object, once found, from the container.
550  OBJ_NODATA - don't return the object if found (no ref count change)
551  OBJ_MULTIPLE - don't stop at first match
552  OBJ_POINTER - if set, 'arg' is an object pointer, and a hashtable
553  search will be done. If not, a traversal is done.
554 
555  - \b ao2_callback(c, flags, fn, arg)
556  apply fn(obj, arg) to all objects in the container.
557  Similar to find. fn() can tell when to stop, and
558  do anything with the object including unlinking it.
559  - c is the container;
560  - flags can be
561  OBJ_UNLINK - to remove the object, once found, from the container.
562  OBJ_NODATA - don't return the object if found (no ref count change)
563  OBJ_MULTIPLE - don't stop at first match
564  OBJ_POINTER - if set, 'arg' is an object pointer, and a hashtable
565  search will be done. If not, a traversal is done through
566  all the hashtable 'buckets'..
567  - fn is a func that returns int, and takes 3 args:
568  (void *obj, void *arg, int flags);
569  obj is an object
570  arg is the same as arg passed into ao2_callback
571  flags is the same as flags passed into ao2_callback
572  fn returns:
573  0: no match, keep going
574  CMP_STOP: stop search, no match
575  CMP_MATCH: This object is matched.
576 
577  Note that the entire operation is run with the container
578  locked, so noone else can change its content while we work on it.
579  However, we pay this with the fact that doing
580  anything blocking in the callback keeps the container
581  blocked.
582  The mechanism is very flexible because the callback function fn()
583  can do basically anything e.g. counting, deleting records, etc.
584  possibly using arg to store the results.
585 
586  - \b iterate on a container
587  this is done with the following sequence
588 
589 \code
590 
591  struct ao2_container *c = ... // our container
592  struct ao2_iterator i;
593  void *o;
594 
595  i = ao2_iterator_init(c, flags);
596 
597  while ((o = ao2_iterator_next(&i))) {
598  ... do something on o ...
599  ao2_ref(o, -1);
600  }
601 
602  ao2_iterator_destroy(&i);
603 \endcode
604 
605  The difference with the callback is that the control
606  on how to iterate is left to us.
607 
608  - \b ao2_ref(c, -1)
609  dropping a reference to a container destroys it, very simple!
610 
611 Containers are ao2 objects themselves, and this is why their
612 implementation is simple too.
613 
614 Before declaring containers, we need to declare the types of the
615 arguments passed to the constructor - in turn, this requires
616 to define callback and hash functions and their arguments.
617 
618 - \ref AstObj2
619 - \ref astobj2.h
620  */
621 
622 /*! \brief
623  * Type of a generic callback function
624  * \param obj pointer to the (user-defined part) of an object.
625  * \param arg callback argument from ao2_callback()
626  * \param flags flags from ao2_callback()
627  *
628  * The return values are a combination of enum _cb_results.
629  * Callback functions are used to search or manipulate objects in a container.
630  */
631 typedef int (ao2_callback_fn)(void *obj, void *arg, int flags);
632 
633 /*! \brief
634  * Type of a generic callback function
635  * \param obj pointer to the (user-defined part) of an object.
636  * \param arg callback argument from ao2_callback()
637  * \param data arbitrary data from ao2_callback()
638  * \param flags flags from ao2_callback()
639  *
640  * The return values are a combination of enum _cb_results.
641  * Callback functions are used to search or manipulate objects in a container.
642  */
643 typedef int (ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags);
644 
645 /*! \brief a very common callback is one that matches by address. */
647 
648 /*! \brief
649  * A callback function will return a combination of CMP_MATCH and CMP_STOP.
650  * The latter will terminate the search in a container.
651  */
653  CMP_MATCH = 0x1, /*!< the object matches the request */
654  CMP_STOP = 0x2, /*!< stop the search now */
655 };
656 
657 /*! \brief
658  * Flags passed to ao2_callback() and ao2_hash_fn() to modify its behaviour.
659  */
661  /*! Unlink the object for which the callback function
662  * returned CMP_MATCH.
663  */
664  OBJ_UNLINK = (1 << 0),
665  /*! On match, don't return the object hence do not increase
666  * its refcount.
667  */
668  OBJ_NODATA = (1 << 1),
669  /*! Don't stop at the first match in ao2_callback() unless the result of
670  * of the callback function == (CMP_STOP | CMP_MATCH).
671  */
672  OBJ_MULTIPLE = (1 << 2),
673  /*! obj is an object of the same type as the one being searched for,
674  * so use the object's hash function for optimized searching.
675  * The search function is unaffected (i.e. use the one passed as
676  * argument, or match_by_addr if none specified).
677  */
678  OBJ_POINTER = (1 << 3),
679  /*!
680  * \brief Continue if a match is not found in the hashed out bucket
681  *
682  * This flag is to be used in combination with OBJ_POINTER. This tells
683  * the ao2_callback() core to keep searching through the rest of the
684  * buckets if a match is not found in the starting bucket defined by
685  * the hash value on the argument.
686  */
687  OBJ_CONTINUE = (1 << 4),
688 };
689 
690 /*!
691  * Type of a generic function to generate a hash value from an object.
692  * flags is ignored at the moment. Eventually, it will include the
693  * value of OBJ_POINTER passed to ao2_callback().
694  */
695 typedef int (ao2_hash_fn)(const void *obj, const int flags);
696 
697 /*! \name Object Containers
698  * Here start declarations of containers.
699  */
700 /*@{ */
701 struct ao2_container;
702 
703 /*! \brief
704  * Allocate and initialize a container
705  * with the desired number of buckets.
706  *
707  * We allocate space for a struct astobj_container, struct container
708  * and the buckets[] array.
709  *
710  * \param arg1 Number of buckets for hash
711  * \param arg2 Pointer to a function computing a hash value.
712  * \param arg3 Pointer to a function comparating key-value
713  * with a string. (can be NULL)
714  * \param arg4
715  *
716  * \return A pointer to a struct container.
717  *
718  * \note Destructor is set implicitly.
719  */
720 
721 #if defined(REF_DEBUG)
722 
723 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
724 #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 1)
725 
726 #elif defined(__AST_DEBUG_MALLOC)
727 
728 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
729 #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, 0)
730 
731 #else
732 
733 #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc((arg1), (arg2), (arg3))
734 #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc((arg1), (arg2), (arg3))
735 
736 #endif
737 
738 struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets,
740 struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets,
742  char *tag, char *file, int line, const char *funcname,
743  int ref_debug);
744 
745 /*! \brief
746  * Returns the number of elements in a container.
747  */
748 int ao2_container_count(struct ao2_container *c);
749 
750 /*@} */
751 
752 /*! \name Object Management
753  * Here we have functions to manage objects.
754  *
755  * We can use the functions below on any kind of
756  * object defined by the user.
757  */
758 /*@{ */
759 
760 /*!
761  * \brief Add an object to a container.
762  *
763  * \param arg1 the container to operate on.
764  * \param arg2 the object to be added.
765  * \param arg3 used for debuging.
766  *
767  * \retval NULL on errors.
768  * \retval newobj on success.
769  *
770  * This function inserts an object in a container according its key.
771  *
772  * \note Remember to set the key before calling this function.
773  *
774  * \note This function automatically increases the reference count to account
775  * for the reference that the container now holds to the object.
776  */
777 #ifdef REF_DEBUG
778 
779 #define ao2_t_link(arg1, arg2, arg3) __ao2_link_debug((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__)
780 #define ao2_link(arg1, arg2) __ao2_link_debug((arg1), (arg2), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
781 
782 #else
783 
784 #define ao2_t_link(arg1, arg2, arg3) __ao2_link((arg1), (arg2))
785 #define ao2_link(arg1, arg2) __ao2_link((arg1), (arg2))
786 
787 #endif
788 
789 void *__ao2_link_debug(struct ao2_container *c, void *new_obj, char *tag, char *file, int line, const char *funcname);
790 void *__ao2_link(struct ao2_container *c, void *newobj);
791 
792 /*!
793  * \brief Remove an object from a container
794  *
795  * \param arg1 the container
796  * \param arg2 the object to unlink
797  * \param arg3 tag for debugging
798  *
799  * \retval NULL, always
800  *
801  * \note The object requested to be unlinked must be valid. However, if it turns
802  * out that it is not in the container, this function is still safe to
803  * be called.
804  *
805  * \note If the object gets unlinked from the container, the container's
806  * reference to the object will be automatically released. (The
807  * refcount will be decremented).
808  */
809 #ifdef REF_DEBUG
810 
811 #define ao2_t_unlink(arg1, arg2, arg3) __ao2_unlink_debug((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__)
812 #define ao2_unlink(arg1, arg2) __ao2_unlink_debug((arg1), (arg2), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
813 
814 #else
815 
816 #define ao2_t_unlink(arg1, arg2, arg3) __ao2_unlink((arg1), (arg2))
817 #define ao2_unlink(arg1, arg2) __ao2_unlink((arg1), (arg2))
818 
819 #endif
820 
821 void *__ao2_unlink_debug(struct ao2_container *c, void *obj, char *tag, char *file, int line, const char *funcname);
822 void *__ao2_unlink(struct ao2_container *c, void *obj);
823 
824 
825 /*@} */
826 
827 /*! \brief
828  * ao2_callback() is a generic function that applies cb_fn() to all objects
829  * in a container, as described below.
830  *
831  * \param c A pointer to the container to operate on.
832  * \param flags A set of flags specifying the operation to perform,
833  partially used by the container code, but also passed to
834  the callback.
835  - If OBJ_NODATA is set, ao2_callback will return NULL. No refcounts
836  of any of the traversed objects will be incremented.
837  On the converse, if it is NOT set (the default), The ref count
838  of each object for which CMP_MATCH was set will be incremented,
839  and you will have no way of knowing which those are, until
840  the multiple-object-return functionality is implemented.
841  - If OBJ_POINTER is set, the traversed items will be restricted
842  to the objects in the bucket that the object key hashes to.
843  * \param cb_fn A function pointer, that will be called on all
844  objects, to see if they match. This function returns CMP_MATCH
845  if the object is matches the criteria; CMP_STOP if the traversal
846  should immediately stop, or both (via bitwise ORing), if you find a
847  match and want to end the traversal, and 0 if the object is not a match,
848  but the traversal should continue. This is the function that is applied
849  to each object traversed. Its arguments are:
850  (void *obj, void *arg, int flags), where:
851  obj is an object
852  arg is the same as arg passed into ao2_callback
853  flags is the same as flags passed into ao2_callback (flags are
854  also used by ao2_callback).
855  * \param arg passed to the callback.
856  * \param tag used for debuging.
857  * \return when OBJ_MULTIPLE is not included in the flags parameter,
858  * the return value will be either the object found or NULL if no
859  * no matching object was found. if OBJ_MULTIPLE is included,
860  * the return value will be a pointer to an ao2_iterator object,
861  * which must be destroyed with ao2_iterator_destroy() when the
862  * caller no longer needs it.
863  *
864  * If the function returns any objects, their refcount is incremented,
865  * and the caller is in charge of decrementing them once done.
866  *
867  * Typically, ao2_callback() is used for two purposes:
868  * - to perform some action (including removal from the container) on one
869  * or more objects; in this case, cb_fn() can modify the object itself,
870  * and to perform deletion should set CMP_MATCH on the matching objects,
871  * and have OBJ_UNLINK set in flags.
872  * - to look for a specific object in a container; in this case, cb_fn()
873  * should not modify the object, but just return a combination of
874  * CMP_MATCH and CMP_STOP on the desired object.
875  * Other usages are also possible, of course.
876 
877  * This function searches through a container and performs operations
878  * on objects according on flags passed.
879  * XXX describe better
880  * The comparison is done calling the compare function set implicitly.
881  * The p pointer can be a pointer to an object or to a key,
882  * we can say this looking at flags value.
883  * If p points to an object we will search for the object pointed
884  * by this value, otherwise we serch for a key value.
885  * If the key is not unique we only find the first matching valued.
886  *
887  * The use of flags argument is the follow:
888  *
889  * OBJ_UNLINK unlinks the object found
890  * OBJ_NODATA on match, do return an object
891  * Callbacks use OBJ_NODATA as a default
892  * functions such as find() do
893  * OBJ_MULTIPLE return multiple matches
894  * Default is no.
895  * OBJ_POINTER the pointer is an object pointer
896  *
897  * \note When the returned object is no longer in use, ao2_ref() should
898  * be used to free the additional reference possibly created by this function.
899  *
900  * @{
901  */
902 #ifdef REF_DEBUG
903 
904 #define ao2_t_callback(c,flags,cb_fn,arg,tag) __ao2_callback_debug((c), (flags), (cb_fn), (arg), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__)
905 #define ao2_callback(c,flags,cb_fn,arg) __ao2_callback_debug((c), (flags), (cb_fn), (arg), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
906 
907 #else
908 
909 #define ao2_t_callback(c,flags,cb_fn,arg,tag) __ao2_callback((c), (flags), (cb_fn), (arg))
910 #define ao2_callback(c,flags,cb_fn,arg) __ao2_callback((c), (flags), (cb_fn), (arg))
911 
912 #endif
913 
914 void *__ao2_callback_debug(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn,
915  void *arg, char *tag, char *file, int line, const char *funcname);
916 void *__ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg);
917 
918 /*! @} */
919 
920 /*! \brief
921  * ao2_callback_data() is a generic function that applies cb_fn() to all objects
922  * in a container. It is functionally identical to ao2_callback() except that
923  * instead of taking an ao2_callback_fn *, it takes an ao2_callback_data_fn *, and
924  * allows the caller to pass in arbitrary data.
925  *
926  * This call would be used instead of ao2_callback() when the caller needs to pass
927  * OBJ_POINTER as part of the flags argument (which in turn requires passing in a
928  * prototype ao2 object for 'arg') and also needs access to other non-global data
929  * to complete it's comparison or task.
930  *
931  * See the documentation for ao2_callback() for argument descriptions.
932  *
933  * \see ao2_callback()
934  */
935 #ifdef REF_DEBUG
936 
937 #define ao2_t_callback_data(arg1,arg2,arg3,arg4,arg5,arg6) __ao2_callback_data_debug((arg1), (arg2), (arg3), (arg4), (arg5), (arg6), __FILE__, __LINE__, __PRETTY_FUNCTION__)
938 #define ao2_callback_data(arg1,arg2,arg3,arg4,arg5) __ao2_callback_data_debug((arg1), (arg2), (arg3), (arg4), (arg5), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
939 
940 #else
941 
942 #define ao2_t_callback_data(arg1,arg2,arg3,arg4,arg5,arg6) __ao2_callback_data((arg1), (arg2), (arg3), (arg4), (arg5))
943 #define ao2_callback_data(arg1,arg2,arg3,arg4,arg5) __ao2_callback_data((arg1), (arg2), (arg3), (arg4), (arg5))
944 
945 #endif
946 
947 void *__ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags,
948  ao2_callback_data_fn *cb_fn, void *arg, void *data, char *tag,
949  char *file, int line, const char *funcname);
950 void *__ao2_callback_data(struct ao2_container *c, enum search_flags flags,
951  ao2_callback_data_fn *cb_fn, void *arg, void *data);
952 
953 /*! ao2_find() is a short hand for ao2_callback(c, flags, c->cmp_fn, arg)
954  * XXX possibly change order of arguments ?
955  */
956 #ifdef REF_DEBUG
957 
958 #define ao2_t_find(arg1,arg2,arg3,arg4) __ao2_find_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__)
959 #define ao2_find(arg1,arg2,arg3) __ao2_find_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
960 
961 #else
962 
963 #define ao2_t_find(arg1,arg2,arg3,arg4) __ao2_find((arg1), (arg2), (arg3))
964 #define ao2_find(arg1,arg2,arg3) __ao2_find((arg1), (arg2), (arg3))
965 
966 #endif
967 
968 void *__ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag,
969  char *file, int line, const char *funcname);
970 void *__ao2_find(struct ao2_container *c, void *arg, enum search_flags flags);
971 
972 /*! \brief
973  *
974  *
975  * When we need to walk through a container, we use an
976  * ao2_iterator to keep track of the current position.
977  *
978  * Because the navigation is typically done without holding the
979  * lock on the container across the loop, objects can be inserted or deleted
980  * or moved while we work. As a consequence, there is no guarantee that
981  * we manage to touch all the elements in the container, and it is possible
982  * that we touch the same object multiple times.
983  *
984  * However, within the current hash table container, the following is true:
985  * - It is not possible to miss an object in the container while iterating
986  * unless it gets added after the iteration begins and is added to a bucket
987  * that is before the one the current object is in. In this case, even if
988  * you locked the container around the entire iteration loop, you still would
989  * not see this object, because it would still be waiting on the container
990  * lock so that it can be added.
991  * - It would be extremely rare to see an object twice. The only way this can
992  * happen is if an object got unlinked from the container and added again
993  * during the same iteration. Furthermore, when the object gets added back,
994  * it has to be in the current or later bucket for it to be seen again.
995  *
996  * An iterator must be first initialized with ao2_iterator_init(),
997  * then we can use o = ao2_iterator_next() to move from one
998  * element to the next. Remember that the object returned by
999  * ao2_iterator_next() has its refcount incremented,
1000  * and the reference must be explicitly released when done with it.
1001  *
1002  * In addition, ao2_iterator_init() will hold a reference to the container
1003  * being iterated, which will be freed when ao2_iterator_destroy() is called
1004  * to free up the resources used by the iterator (if any).
1005  *
1006  * Example:
1007  *
1008  * \code
1009  *
1010  * struct ao2_container *c = ... // the container we want to iterate on
1011  * struct ao2_iterator i;
1012  * struct my_obj *o;
1013  *
1014  * i = ao2_iterator_init(c, flags);
1015  *
1016  * while ((o = ao2_iterator_next(&i))) {
1017  * ... do something on o ...
1018  * ao2_ref(o, -1);
1019  * }
1020  *
1021  * ao2_iterator_destroy(&i);
1022  *
1023  * \endcode
1024  *
1025  */
1026 
1027 /*! \brief
1028  * The astobj2 iterator
1029  *
1030  * \note You are not supposed to know the internals of an iterator!
1031  * We would like the iterator to be opaque, unfortunately
1032  * its size needs to be known if we want to store it around
1033  * without too much trouble.
1034  * Anyways...
1035  * The iterator has a pointer to the container, and a flags
1036  * field specifying various things e.g. whether the container
1037  * should be locked or not while navigating on it.
1038  * The iterator "points" to the current object, which is identified
1039  * by three values:
1040  *
1041  * - a bucket number;
1042  * - the object_id, which is also the container version number
1043  * when the object was inserted. This identifies the object
1044  * uniquely, however reaching the desired object requires
1045  * scanning a list.
1046  * - a pointer, and a container version when we saved the pointer.
1047  * If the container has not changed its version number, then we
1048  * can safely follow the pointer to reach the object in constant time.
1049  *
1050  * Details are in the implementation of ao2_iterator_next()
1051  * A freshly-initialized iterator has bucket=0, version=0.
1052  */
1054  /*! the container */
1055  struct ao2_container *c;
1056  /*! operation flags */
1057  int flags;
1058  /*! current bucket */
1059  int bucket;
1060  /*! container version */
1061  unsigned int c_version;
1062  /*! pointer to the current object */
1063  void *obj;
1064  /*! container version when the object was created */
1065  unsigned int version;
1066 };
1067 
1068 /*! Flags that can be passed to ao2_iterator_init() to modify the behavior
1069  * of the iterator.
1070  */
1072  /*! Prevents ao2_iterator_next() from locking the container
1073  * while retrieving the next object from it.
1074  */
1076  /*! Indicates that the iterator was dynamically allocated by
1077  * astobj2 API and should be freed by ao2_iterator_destroy().
1078  */
1080  /*! Indicates that before the iterator returns an object from
1081  * the container being iterated, the object should be unlinked
1082  * from the container.
1083  */
1085 };
1086 
1087 /*!
1088  * \brief Create an iterator for a container
1089  *
1090  * \param c the container
1091  * \param flags one or more flags from ao2_iterator_flags
1092  *
1093  * \retval the constructed iterator
1094  *
1095  * \note This function does \b not take a pointer to an iterator;
1096  * rather, it returns an iterator structure that should be
1097  * assigned to (overwriting) an existing iterator structure
1098  * allocated on the stack or on the heap.
1099  *
1100  * This function will take a reference on the container being iterated.
1101  *
1102  */
1103 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags);
1104 
1105 /*!
1106  * \brief Destroy a container iterator
1107  *
1108  * \param i the iterator to destroy
1109  *
1110  * \retval none
1111  *
1112  * This function will release the container reference held by the iterator
1113  * and any other resources it may be holding.
1114  *
1115  */
1116 void ao2_iterator_destroy(struct ao2_iterator *i);
1117 
1118 #ifdef REF_DEBUG
1119 
1120 #define ao2_t_iterator_next(arg1, arg2) __ao2_iterator_next_debug((arg1), (arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__)
1121 #define ao2_iterator_next(arg1) __ao2_iterator_next_debug((arg1), "", __FILE__, __LINE__, __PRETTY_FUNCTION__)
1122 
1123 #else
1124 
1125 #define ao2_t_iterator_next(arg1, arg2) __ao2_iterator_next((arg1))
1126 #define ao2_iterator_next(arg1) __ao2_iterator_next((arg1))
1127 
1128 #endif
1129 
1130 void *__ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname);
1131 void *__ao2_iterator_next(struct ao2_iterator *a);
1132 
1133 /* extra functions */
1134 void ao2_bt(void); /* backtrace */
1135 
1136 #endif /* _ASTERISK_ASTOBJ2_H */
unsigned int c_version
Definition: astobj2.h:1061
void ao2_bt(void)
Definition: astobj2.c:90
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Definition: astobj2.c:470
void * __ao2_callback_data(struct ao2_container *c, enum search_flags flags, ao2_callback_data_fn *cb_fn, void *arg, void *data)
Definition: astobj2.c:796
int n_buckets
Definition: astobj2.c:394
void * __ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
Definition: astobj2.c:810
#define var
Definition: ast_expr2f.c:606
int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var)
Unlock an object.
Definition: astobj2.c:175
Continue if a match is not found in the hashed out bucket.
Definition: astobj2.h:687
void * __ao2_callback(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg)
Definition: astobj2.c:782
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
search_flags
Flags passed to ao2_callback() and ao2_hash_fn() to modify its behaviour.
Definition: astobj2.h:660
void * __ao2_link_debug(struct ao2_container *c, void *new_obj, char *tag, char *file, int line, const char *funcname)
Definition: astobj2.c:520
struct ao2_container * c
Definition: astobj2.h:1055
int __ao2_trylock(void *a, const char *file, const char *func, int line, const char *var)
Try locking– (don&#39;t block if fail)
Definition: astobj2.c:189
void * ao2_object_get_lockaddr(void *obj)
Return the lock address of an object.
Definition: astobj2.c:205
void * __ao2_alloc_debug(const size_t data_size, ao2_destructor_fn destructor_fn, char *tag, const char *file, int line, const char *funcname, int ref_debug)
Definition: astobj2.c:335
int __ao2_ref(void *o, int delta)
Definition: astobj2.c:248
A set of macros to manage forward-linked lists.
void(* ao2_destructor_fn)(void *)
Typedef for an object destructor. This is called just before freeing the memory for the object...
Definition: astobj2.h:395
void * obj
Definition: astobj2.h:1063
int __ao2_lock(void *a, const char *file, const char *func, int line, const char *var)
Lock an object.
Definition: astobj2.c:161
_cb_results
A callback function will return a combination of CMP_MATCH and CMP_STOP. The latter will terminate th...
Definition: astobj2.h:652
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
struct ao2_container * __ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn)
Definition: astobj2.c:454
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
void * __ao2_callback_debug(struct ao2_container *c, enum search_flags flags, ao2_callback_fn *cb_fn, void *arg, char *tag, char *file, int line, const char *funcname)
Definition: astobj2.c:774
ao2_callback_fn ao2_match_by_addr
a very common callback is one that matches by address.
Definition: astobj2.h:646
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
struct ao2_container * __ao2_container_alloc_debug(const unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn, char *tag, char *file, int line, const char *funcname, int ref_debug)
Definition: astobj2.c:441
int( ao2_hash_fn)(const void *obj, const int flags)
Definition: astobj2.h:695
void * __ao2_unlink(struct ao2_container *c, void *obj)
Definition: astobj2.c:565
void * __ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
Definition: astobj2.c:805
int __ao2_ref_debug(void *o, int delta, const char *tag, const char *file, int line, const char *funcname)
Definition: astobj2.c:219
void * __ao2_callback_data_debug(struct ao2_container *c, enum search_flags flags, ao2_callback_data_fn *cb_fn, void *arg, void *data, char *tag, char *file, int line, const char *funcname)
Definition: astobj2.c:788
ao2_iterator_flags
Definition: astobj2.h:1071
void * __ao2_unlink_debug(struct ao2_container *c, void *obj, char *tag, char *file, int line, const char *funcname)
Definition: astobj2.c:554
void * __ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn)
Definition: astobj2.c:354
unsigned int version
Definition: astobj2.h:1065
void * __ao2_iterator_next_debug(struct ao2_iterator *a, char *tag, char *file, int line, const char *funcname)
Definition: astobj2.c:913
void * __ao2_link(struct ao2_container *c, void *newobj)
Definition: astobj2.c:531
ao2_hash_fn * hash_fn
Event type specific hash function.
Definition: event.c:165
void * __ao2_iterator_next(struct ao2_iterator *a)
Definition: astobj2.c:931
int( ao2_callback_data_fn)(void *obj, void *arg, void *data, int flags)
Type of a generic callback function.
Definition: astobj2.h:643
int( ao2_callback_fn)(void *obj, void *arg, int flags)
Type of a generic callback function.
Definition: astobj2.h:631
ao2_callback_fn * cmp_fn
Definition: astobj2.c:393