diff options
author | Richard Mudgett <rmudgett@digium.com> | 2015-03-27 21:06:22 +0000 |
---|---|---|
committer | Richard Mudgett <rmudgett@digium.com> | 2015-03-27 21:06:22 +0000 |
commit | 2659e48d9d1b422474a2f39ff06f67d0fd075dfb (patch) | |
tree | a71a22ad0f9b2b6c8c800a2bfd67e8f65e0b51bd /res/res_pjsip_registrar_expire.c | |
parent | 0b62e416541066c53088013fc99a0775f9b318e3 (diff) |
res_pjsip_registrar_expire.c: Made use ao2 container template routines and eliminated some RAII_VAR() usage.
* Converted the contact_autoexpire container to use the ao2 template hash
and cmp functions. Also made use the OBJ_SEARCH_xxx names instead of the
deprecated names.
* Eliminates several unnecessary uses of RAII_VAR().
Review: https://reviewboard.asterisk.org/r/4524/
........
Merged revisions 433622 from http://svn.asterisk.org/svn/asterisk/branches/13
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@433623 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_pjsip_registrar_expire.c')
-rw-r--r-- | res/res_pjsip_registrar_expire.c | 100 |
1 files changed, 78 insertions, 22 deletions
diff --git a/res/res_pjsip_registrar_expire.c b/res/res_pjsip_registrar_expire.c index c311c0025..5b2b350ea 100644 --- a/res/res_pjsip_registrar_expire.c +++ b/res/res_pjsip_registrar_expire.c @@ -57,32 +57,76 @@ static void contact_expiration_destroy(void *obj) /*! \brief Hashing function for contact auto-expiration */ static int contact_expiration_hash(const void *obj, const int flags) { - const struct contact_expiration *expiration = obj; - const char *id = obj; - - return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(expiration->contact)); + const struct contact_expiration *object; + const char *key; + + switch (flags & OBJ_SEARCH_MASK) { + case OBJ_SEARCH_KEY: + key = obj; + break; + case OBJ_SEARCH_OBJECT: + object = obj; + key = ast_sorcery_object_get_id(object->contact); + break; + default: + /* Hash can only work on something with a full key. */ + ast_assert(0); + return 0; + } + return ast_str_hash(key); } /*! \brief Comparison function for contact auto-expiration */ static int contact_expiration_cmp(void *obj, void *arg, int flags) { - struct contact_expiration *expiration1 = obj, *expiration2 = arg; - const char *id = arg; - - return !strcmp(ast_sorcery_object_get_id(expiration1->contact), flags & OBJ_KEY ? id : - ast_sorcery_object_get_id(expiration2->contact)) ? CMP_MATCH | CMP_STOP : 0; + const struct contact_expiration *object_left = obj; + const struct contact_expiration *object_right = arg; + const char *right_key = arg; + int cmp; + + switch (flags & OBJ_SEARCH_MASK) { + case OBJ_SEARCH_OBJECT: + right_key = ast_sorcery_object_get_id(object_right->contact); + /* Fall through */ + case OBJ_SEARCH_KEY: + cmp = strcmp(ast_sorcery_object_get_id(object_left->contact), right_key); + break; + case OBJ_SEARCH_PARTIAL_KEY: + /* + * We could also use a partial key struct containing a length + * so strlen() does not get called for every comparison instead. + */ + cmp = strncmp(ast_sorcery_object_get_id(object_left->contact), right_key, + strlen(right_key)); + break; + default: + /* + * What arg points to is specific to this traversal callback + * and has no special meaning to astobj2. + */ + cmp = 0; + break; + } + if (cmp) { + return 0; + } + /* + * At this point the traversal callback is identical to a sorted + * container. + */ + return CMP_MATCH; } /*! \brief Scheduler function which deletes a contact */ static int contact_expiration_expire(const void *data) { - RAII_VAR(struct contact_expiration *, expiration, (void*)data, ao2_cleanup); + struct contact_expiration *expiration = (void *) data; expiration->sched = -1; /* This will end up invoking the deleted observer callback, which will perform the unlinking and such */ ast_sorcery_delete(ast_sip_get_sorcery(), expiration->contact); - + ao2_ref(expiration, -1); return 0; } @@ -90,14 +134,16 @@ static int contact_expiration_expire(const void *data) static void contact_expiration_observer_created(const void *object) { const struct ast_sip_contact *contact = object; - RAII_VAR(struct contact_expiration *, expiration, NULL, ao2_cleanup); + struct contact_expiration *expiration; int expires = MAX(0, ast_tvdiff_ms(contact->expiration_time, ast_tvnow())); if (ast_tvzero(contact->expiration_time)) { return; } - if (!(expiration = ao2_alloc_options(sizeof(*expiration), contact_expiration_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK))) { + expiration = ao2_alloc_options(sizeof(*expiration), contact_expiration_destroy, + AO2_ALLOC_OPT_LOCK_NOLOCK); + if (!expiration) { return; } @@ -106,39 +152,46 @@ static void contact_expiration_observer_created(const void *object) ao2_ref(expiration, +1); if ((expiration->sched = ast_sched_add(sched, expires, contact_expiration_expire, expiration)) < 0) { - ao2_cleanup(expiration); + ao2_ref(expiration, -1); ast_log(LOG_ERROR, "Scheduled expiration for contact '%s' could not be performed, contact may persist past life\n", ast_sorcery_object_get_id(contact)); - return; + } else { + ao2_link(contact_autoexpire, expiration); } - - ao2_link(contact_autoexpire, expiration); + ao2_ref(expiration, -1); } /*! \brief Observer callback for when a contact is updated */ static void contact_expiration_observer_updated(const void *object) { const struct ast_sip_contact *contact = object; - RAII_VAR(struct contact_expiration *, expiration, ao2_find(contact_autoexpire, ast_sorcery_object_get_id(contact), OBJ_KEY), ao2_cleanup); + struct contact_expiration *expiration; int expires = MAX(0, ast_tvdiff_ms(contact->expiration_time, ast_tvnow())); + expiration = ao2_find(contact_autoexpire, ast_sorcery_object_get_id(contact), + OBJ_SEARCH_KEY); if (!expiration) { return; } - AST_SCHED_REPLACE_UNREF(expiration->sched, sched, expires, contact_expiration_expire, expiration, ao2_cleanup(expiration), ao2_cleanup(expiration), ao2_ref(expiration, +1)); + AST_SCHED_REPLACE_UNREF(expiration->sched, sched, expires, contact_expiration_expire, + expiration, ao2_cleanup(expiration), ao2_cleanup(expiration), ao2_ref(expiration, +1)); + ao2_ref(expiration, -1); } /*! \brief Observer callback for when a contact is deleted */ static void contact_expiration_observer_deleted(const void *object) { - RAII_VAR(struct contact_expiration *, expiration, ao2_find(contact_autoexpire, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup); + struct contact_expiration *expiration; + expiration = ao2_find(contact_autoexpire, ast_sorcery_object_get_id(object), + OBJ_SEARCH_KEY | OBJ_UNLINK); if (!expiration) { return; } AST_SCHED_DEL_UNREF(sched, expiration->sched, ao2_cleanup(expiration)); + ao2_ref(expiration, -1); } /*! \brief Observer callbacks for autoexpiring contacts */ @@ -166,13 +219,16 @@ static int contact_expiration_setup(void *obj, void *arg, int flags) /*! \brief Initialize auto-expiration of any existing contacts */ static void contact_expiration_initialize_existing(void) { - RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); + struct ao2_container *contacts; - if (!(contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) { + contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact", + AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + if (!contacts) { return; } ao2_callback(contacts, OBJ_NODATA, contact_expiration_setup, NULL); + ao2_ref(contacts, -1); } static int unload_observer_delete(void *obj, void *arg, int flags) |