astobj2.h 82 KB

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