astobj2: Avoid using temporary objects + ao2_find() with OBJ_POINTER.

There is a fairly common pattern making its way through the code base where we
put a temporary object on the stack so we can call ao2_find() with OBJ_POINTER.
The purpose is so that it can be passed into the object hash function.
However, this really seems like a hack and potentially error prone.  This patch
is a first stab at approach to avoid having to do that.

It adds a new flag, OBJ_KEY, which can be used instead of OBJ_POINTER in these
situations.  Then, the hash function can know whether it was given an object or
some custom data to hash.

The patch also changes some uses of ao2_find() for iax2_user and iax2_peer
objects to reflect how OBJ_KEY would be used.

So long, and thanks for all the fish.

Review: https://reviewboard.asterisk.org/r/1184/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@330273 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2011-07-29 19:34:36 +00:00
parent d2ed4ed5f7
commit 6a15e95a32
4 changed files with 76 additions and 54 deletions
+6 -6
View File
@@ -648,8 +648,8 @@ static void *internal_ao2_callback(struct ao2_container *c,
* run the hash function. Otherwise, scan the whole container
* (this only for the time being. We need to optimize this.)
*/
if ((flags & OBJ_POINTER)) /* we know hash can handle this case */
start = i = c->hash_fn(arg, flags & OBJ_POINTER) % c->n_buckets;
if ((flags & (OBJ_POINTER | OBJ_KEY))) /* we know hash can handle this case */
start = i = c->hash_fn(arg, flags & (OBJ_POINTER | OBJ_KEY)) % c->n_buckets;
else /* don't know, let's scan all buckets */
start = i = -1; /* XXX this must be fixed later. */
@@ -801,14 +801,14 @@ void *__ao2_callback_data(struct ao2_container *c, const enum search_flags flags
/*!
* the find function just invokes the default callback with some reasonable flags.
*/
void *__ao2_find_debug(struct ao2_container *c, void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
void *__ao2_find_debug(struct ao2_container *c, const void *arg, enum search_flags flags, char *tag, char *file, int line, const char *funcname)
{
return __ao2_callback_debug(c, flags, c->cmp_fn, arg, tag, file, line, funcname);
return __ao2_callback_debug(c, flags, c->cmp_fn, (void *) arg, tag, file, line, funcname);
}
void *__ao2_find(struct ao2_container *c, void *arg, enum search_flags flags)
void *__ao2_find(struct ao2_container *c, const void *arg, enum search_flags flags)
{
return __ao2_callback(c, flags, c->cmp_fn, arg);
return __ao2_callback(c, flags, c->cmp_fn, (void *) arg);
}
/*!