Adds DAHDI support alongside Zaptel. DAHDI usage favored, but all Zap stuff should...
[asterisk-bristuff.git] / include / asterisk / astobj2.h
blobbcb8adddae02e9b521857d6d4a796a9cbe533968
1 /*
2 * astobj2 - replacement containers for asterisk data structures.
4 * Copyright (C) 2006 Marta Carbone, Luigi Rizzo - Univ. di Pisa, Italy
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
17 #ifndef _ASTERISK_ASTOBJ2_H
18 #define _ASTERISK_ASTOBJ2_H
20 /*! \file
22 * \brief Object Model implementing objects and containers.
24 These functions implement an abstraction for objects (with
25 locks and reference counts) and containers for these user-defined objects,
26 supporting locking, reference counting and callbacks.
28 The internal implementation of the container is opaque to the user,
29 so we can use different data structures as needs arise.
31 At the moment, however, the only internal data structure is a hash
32 table. When other structures will be implemented, the initialization
33 function may change.
35 USAGE - OBJECTS
37 An object is a block of memory that must be allocated with the
38 function ao2_alloc(), and for which the system keeps track (with
39 abit of help from the programmer) of the number of references around.
40 When an object has no more references, it is destroyed, by first
41 invoking whatever 'destructor' function the programmer specifies
42 (it can be NULL), and then freeing the memory.
43 This way objects can be shared without worrying who is in charge
44 of freeing them.
46 Basically, creating an object requires the size of the object and
47 and a pointer to the destructor function:
49 struct foo *o;
51 o = ao2_alloc(sizeof(struct foo), my_destructor_fn);
53 The object returned has a refcount = 1.
54 Note that the memory for the object is allocated and zeroed.
55 - We cannot realloc() the object itself.
56 - We cannot call free(o) to dispose of the object; rather we
57 tell the system that we do not need the reference anymore:
59 ao2_ref(o, -1)
61 causing the destructor to be called (and then memory freed) when
62 the refcount goes to 0. This is also available as ao2_unref(o),
63 and returns NULL as a convenience, so you can do things like
64 o = ao2_unref(o);
65 and clean the original pointer to prevent errors.
67 - ao2_ref(o, +1) can be used to modify the refcount on the
68 object in case we want to pass it around.
71 - other calls on the object are ao2_lock(obj), ao2_unlock(),
72 ao2_trylock(), to manipulate the lock.
75 USAGE - CONTAINERS
77 A containers is an abstract data structure where we can store
78 objects, search them (hopefully in an efficient way), and iterate
79 or apply a callback function to them. A container is just an object
80 itself.
82 A container must first be allocated, specifying the initial
83 parameters. At the moment, this is done as follows:
85 <b>Sample Usage:</b>
86 \code
88 struct ao2_container *c;
90 c = ao2_container_alloc(MAX_BUCKETS, my_hash_fn, my_cmp_fn);
92 where
93 - MAX_BUCKETS is the number of buckets in the hash table,
94 - my_hash_fn() is the (user-supplied) function that returns a
95 hash key for the object (further reduced moduly MAX_BUCKETS
96 by the container's code);
97 - my_cmp_fn() is the default comparison function used when doing
98 searches on the container,
100 A container knows little or nothing about the object itself,
101 other than the fact that it has been created by ao2_alloc()
102 All knowledge of the (user-defined) internals of the object
103 is left to the (user-supplied) functions passed as arguments
104 to ao2_container_alloc().
106 If we want to insert the object in the container, we should
107 initialize its fields -- especially, those used by my_hash_fn() --
108 to compute the bucket to use.
109 Once done, we can link an object to a container with
111 ao2_link(c, o);
113 The function returns NULL in case of errors (and the object
114 is not inserted in the container). Other values mean success
115 (we are not supposed to use the value as a pointer to anything).
117 \note While an object o is in a container, we expect that
118 my_hash_fn(o) will always return the same value. The function
119 does not lock the object to be computed, so modifications of
120 those fields that affect the computation of the hash should
121 be done by extractiong the object from the container, and
122 reinserting it after the change (this is not terribly expensive).
124 \note A container with a single buckets is effectively a linked
125 list. However there is no ordering among elements.
127 Objects implement a reference counter keeping the count
128 of the number of references that reference an object.
130 When this number becomes zero the destructor will be
131 called and the object will be free'd.
135 * Invoked just before freeing the memory for the object.
136 * It is passed a pointer to user data.
138 typedef void (*ao2_destructor_fn)(void *);
140 void ao2_bt(void); /* backtrace */
142 * Allocate and initialize an object.
144 * \param data_size The sizeof() of user-defined structure.
145 * \param destructor_fn The function destructor (can be NULL)
146 * \return A pointer to user data.
148 * Allocates a struct astobj2 with sufficient space for the
149 * user-defined structure.
150 * \notes:
151 * - storage is zeroed; XXX maybe we want a flag to enable/disable this.
152 * - the refcount of the object just created is 1
153 * - the returned pointer cannot be free()'d or realloc()'ed;
154 * rather, we just call ao2_ref(o, -1);
156 void *ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn);
159 * Reference/unreference an object and return the old refcount.
161 * \param o A pointer to the object
162 * \param delta Value to add to the reference counter.
163 * \return The value of the reference counter before the operation.
165 * Increase/decrease the reference counter according
166 * the value of delta.
168 * If the refcount goes to zero, the object is destroyed.
170 * \note The object must not be locked by the caller of this function, as
171 * it is invalid to try to unlock it after releasing the reference.
173 * \note if we know the pointer to an object, it is because we
174 * have a reference count to it, so the only case when the object
175 * can go away is when we release our reference, and it is
176 * the last one in existence.
178 int ao2_ref(void *o, int delta);
181 * Lock an object.
183 * \param a A pointer to the object we want lock.
184 * \return 0 on success, other values on error.
186 int ao2_lock(void *a);
189 * Unlock an object.
191 * \param a A pointer to the object we want unlock.
192 * \return 0 on success, other values on error.
194 int ao2_unlock(void *a);
198 * Containers
200 containers are data structures meant to store several objects,
201 and perform various operations on them.
202 Internally, objects are stored in lists, hash tables or other
203 data structures depending on the needs.
205 NOTA BENE: at the moment the only container we support is the
206 hash table and its degenerate form, the list.
208 Operations on container include:
210 c = ao2_container_alloc(size, cmp_fn, hash_fn)
211 allocate a container with desired size and default compare
212 and hash function
214 ao2_find(c, arg, flags)
215 returns zero or more element matching a given criteria
216 (specified as arg). Flags indicate how many results we
217 want (only one or all matching entries), and whether we
218 should unlink the object from the container.
220 ao2_callback(c, flags, fn, arg)
221 apply fn(obj, arg) to all objects in the container.
222 Similar to find. fn() can tell when to stop, and
223 do anything with the object including unlinking it.
224 Note that the entire operation is run with the container
225 locked, so noone else can change its content while we work on it.
226 However, we pay this with the fact that doing
227 anything blocking in the callback keeps the container
228 blocked.
229 The mechanism is very flexible because the callback function fn()
230 can do basically anything e.g. counting, deleting records, etc.
231 possibly using arg to store the results.
233 iterate on a container
234 this is done with the following sequence
236 struct ao2_container *c = ... // our container
237 struct ao2_iterator i;
238 void *o;
240 i = ao2_iterator_init(c, flags);
242 while ( (o = ao2_iterator_next(&i)) ) {
243 ... do something on o ...
244 ao2_ref(o, -1);
247 The difference with the callback is that the control
248 on how to iterate is left to us.
250 ao2_ref(c, -1)
251 dropping a reference to a container destroys it, very simple!
253 Containers are astobj2 object themselves, and this is why their
254 implementation is simple too.
259 * We can perform different operation on an object. We do this
260 * according the following flags.
262 enum search_flags {
263 /*! unlink the object found */
264 OBJ_UNLINK = (1 << 0),
265 /*! on match, don't return the object or increase its reference count. */
266 OBJ_NODATA = (1 << 1),
267 /*! don't stop at the first match
268 * \note This is not fully implemented. */
269 OBJ_MULTIPLE = (1 << 2),
270 /*! obj is an object of the same type as the one being searched for.
271 * This implies that it can be passed to the object's hash function
272 * for optimized searching. */
273 OBJ_POINTER = (1 << 3),
277 * Type of a generic function to generate a hash value from an object.
280 typedef int (*ao2_hash_fn)(const void *obj, const int flags);
283 * valid callback results:
284 * We return a combination of
285 * CMP_MATCH when the object matches the request,
286 * and CMP_STOP when we should not continue the search further.
288 enum _cb_results {
289 CMP_MATCH = 0x1,
290 CMP_STOP = 0x2,
294 * generic function to compare objects.
295 * This, as other callbacks, should return a combination of
296 * _cb_results as described above.
298 * \param o object from container
299 * \param arg search parameters (directly from ao2_find)
300 * \param flags passed directly from ao2_find
301 * XXX explain.
305 * Type of a generic callback function
306 * \param obj pointer to the (user-defined part) of an object.
307 * \param arg callback argument from ao2_callback()
308 * \param flags flags from ao2_callback()
309 * The return values are the same as a compare function.
310 * In fact, they are the same thing.
312 typedef int (*ao2_callback_fn)(void *obj, void *arg, int flags);
315 * Here start declarations of containers.
317 struct ao2_container;
320 * Allocate and initialize a container
321 * with the desired number of buckets.
323 * We allocate space for a struct astobj_container, struct container
324 * and the buckets[] array.
326 * \param my_hash_fn Pointer to a function computing a hash value.
327 * \param my_cmp_fn Pointer to a function comparating key-value
328 * with a string. (can be NULL)
329 * \return A pointer to a struct container.
331 * destructor is set implicitly.
333 struct ao2_container *ao2_container_alloc(const uint n_buckets,
334 ao2_hash_fn hash_fn, ao2_callback_fn cmp_fn);
337 * Returns the number of elements in a container.
339 int ao2_container_count(struct ao2_container *c);
342 * Here we have functions to manage objects.
344 * We can use the functions below on any kind of
345 * object defined by the user.
349 * \brief Add an object to a container.
351 * \param c the container to operate on.
352 * \param newobj the object to be added.
354 * \return NULL on errors, other values on success.
356 * This function inserts an object in a container according its key.
358 * \note Remember to set the key before calling this function.
360 * \note This function automatically increases the reference count to
361 * account for the reference to the object that the container now holds.
363 * For Asterisk 1.4 only, there is a dirty hack here to ensure that chan_iax2
364 * can have objects linked in to the container at the head instead of tail
365 * when it is just a linked list. This is to maintain some existing behavior
366 * where the order must be maintained as it was before this conversion so that
367 * matching behavior doesn't change.
369 #define ao2_link(c, o) __ao2_link(c, o, 0)
370 void *__ao2_link(struct ao2_container *c, void *newobj, int iax2_hack);
373 * \brief Remove an object from the container
375 * \arg c the container
376 * \arg obj the object to unlink
378 * \retval NULL, always
380 * \note The object requested to be unlinked must be valid. However, if it turns
381 * out that it is not in the container, this function is still safe to
382 * be called.
384 * \note If the object gets unlinked from the container, the container's
385 * reference to the object will be automatically released.
387 void *ao2_unlink(struct ao2_container *c, void *obj);
389 /*! \struct Used as return value if the flag OBJ_MULTIPLE is set */
390 struct ao2_list {
391 struct ao2_list *next;
392 void *obj; /* pointer to the user portion of the object */
396 * ao2_callback() and astob2_find() are the same thing with only one difference:
397 * the latter uses as a callback the function passed as my_cmp_f() at
398 * the time of the creation of the container.
400 * \param c A pointer to the container to operate on.
401 * \param arg passed to the callback.
402 * \param flags A set of flags specifying the operation to perform,
403 partially used by the container code, but also passed to
404 the callback.
405 * \return A pointer to the object found/marked,
406 * a pointer to a list of objects matching comparison function,
407 * NULL if not found.
408 * If the function returns any objects, their refcount is incremented,
409 * and the caller is in charge of decrementing them once done.
410 * Also, in case of multiple values returned, the list used
411 * to store the objects must be freed by the caller.
413 * This function searches through a container and performs operations
414 * on objects according on flags passed.
415 * XXX describe better
416 * The comparison is done calling the compare function set implicitly.
417 * The p pointer can be a pointer to an object or to a key,
418 * we can say this looking at flags value.
419 * If p points to an object we will search for the object pointed
420 * by this value, otherwise we serch for a key value.
421 * If the key is not uniq we only find the first matching valued.
422 * If we use the OBJ_MARK flags, we mark all the objects matching
423 * the condition.
425 * The use of flags argument is the follow:
427 * OBJ_UNLINK unlinks the object found
428 * OBJ_NODATA on match, do return an object
429 * Callbacks use OBJ_NODATA as a default
430 * functions such as find() do
431 * OBJ_MULTIPLE return multiple matches
432 * Default for _find() is no.
433 * to a key (not yet supported)
434 * OBJ_POINTER the pointer is an object pointer
436 * In case we return a list, the callee must take care to destroy
437 * that list when no longer used.
439 * \note When the returned object is no longer in use, ao2_ref() should
440 * be used to free the additional reference possibly created by this function.
442 /* XXX order of arguments to find */
443 void *ao2_find(struct ao2_container *c, void *arg, enum search_flags flags);
444 void *ao2_callback(struct ao2_container *c,
445 enum search_flags flags,
446 ao2_callback_fn cb_fn, void *arg);
448 int ao2_match_by_addr(void *user_data, void *arg, int flags);
452 * When we need to walk through a container, we use
453 * ao2_iterator to keep track of the current position.
455 * Because the navigation is typically done without holding the
456 * lock on the container across the loop,
457 * objects can be inserted or deleted or moved
458 * while we work. As a consequence, there is no guarantee that
459 * the we manage to touch all the elements on the list, or it
460 * is possible that we touch the same object multiple times.
461 * However, within the current hash table container, the following is true:
462 * - It is not possible to miss an object in the container while iterating
463 * unless it gets added after the iteration begins and is added to a bucket
464 * that is before the one the current object is in. In this case, even if
465 * you locked the container around the entire iteration loop, you still would
466 * not see this object, because it would still be waiting on the container
467 * lock so that it can be added.
468 * - It would be extremely rare to see an object twice. The only way this can
469 * happen is if an object got unlinked from the container and added again
470 * during the same iteration. Furthermore, when the object gets added back,
471 * it has to be in the current or later bucket for it to be seen again.
473 * An iterator must be first initialized with ao2_iterator_init(),
474 * then we can use o = ao2_iterator_next() to move from one
475 * element to the next. Remember that the object returned by
476 * ao2_iterator_next() has its refcount incremented,
477 * and the reference must be explicitly released when done with it.
479 * Example:
481 * \code
483 * struct ao2_container *c = ... // the container we want to iterate on
484 * struct ao2_iterator i;
485 * struct my_obj *o;
487 * i = ao2_iterator_init(c, flags);
489 * while ( (o = ao2_iterator_next(&i)) ) {
490 * ... do something on o ...
491 * ao2_ref(o, -1);
494 * \endcode
499 * You are not supposed to know the internals of an iterator!
500 * We would like the iterator to be opaque, unfortunately
501 * its size needs to be known if we want to store it around
502 * without too much trouble.
503 * Anyways...
504 * The iterator has a pointer to the container, and a flags
505 * field specifying various things e.g. whether the container
506 * should be locked or not while navigating on it.
507 * The iterator "points" to the current object, which is identified
508 * by three values:
509 * - a bucket number;
510 * - the object_id, which is also the container version number
511 * when the object was inserted. This identifies the object
512 * univoquely, however reaching the desired object requires
513 * scanning a list.
514 * - a pointer, and a container version when we saved the pointer.
515 * If the container has not changed its version number, then we
516 * can safely follow the pointer to reach the object in constant time.
517 * Details are in the implementation of ao2_iterator_next()
518 * A freshly-initialized iterator has bucket=0, version = 0.
521 struct ao2_iterator {
522 /*! the container */
523 struct ao2_container *c;
524 /*! operation flags */
525 int flags;
526 #define F_AO2I_DONTLOCK 1 /*!< don't lock when iterating */
527 /*! current bucket */
528 int bucket;
529 /*! container version */
530 uint c_version;
531 /*! pointer to the current object */
532 void *obj;
533 /*! container version when the object was created */
534 uint version;
537 struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags);
539 void *ao2_iterator_next(struct ao2_iterator *a);
541 #endif /* _ASTERISK_ASTOBJ2_H */