[Asterisk-code-review] core: Use macros to generate ao2 container callbacks where p... (asterisk[master])
Jenkins2
asteriskteam at digium.com
Wed Jan 3 07:50:37 CST 2018
Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7779 )
Change subject: core: Use macros to generate ao2_container callbacks where possible.
......................................................................
core: Use macros to generate ao2_container callbacks where possible.
This uses AO2_STRING_FIELD_HASH_FN and AO2_STRING_FIELD_CMP_FN where
possible in the Asterisk core.
This removes CMP_STOP from the result of CMP_FN callbacks for the
following structure types:
* ast_bucket_metadata
* ast_bucket_scheme
* generic_monitor_instance_list (ccss.c)
* ast_bucket_file (media_cache.c)
* named_acl
Change-Id: Ide4c1449a894bce70dea1fef664dade9b57578f1
---
M main/bucket.c
M main/ccss.c
M main/cel.c
M main/codec.c
M main/endpoints.c
M main/format.c
M main/media_cache.c
M main/named_acl.c
M main/named_locks.c
M main/sorcery.c
10 files changed, 54 insertions(+), 668 deletions(-)
Approvals:
Joshua Colp: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Jenkins2: Approved for Submit
diff --git a/main/bucket.c b/main/bucket.c
index e002848..07cd016 100644
--- a/main/bucket.c
+++ b/main/bucket.c
@@ -617,33 +617,10 @@
}
/*! \brief Hashing function for file metadata */
-static int bucket_file_metadata_hash(const void *obj, const int flags)
-{
- const struct ast_bucket_metadata *object;
- const char *key;
-
- switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
- case OBJ_KEY:
- key = obj;
- return ast_str_hash(key);
- case OBJ_POINTER:
- object = obj;
- return ast_str_hash(object->name);
- default:
- /* Hash can only work on something with a full key */
- ast_assert(0);
- return 0;
- }
-}
+AO2_STRING_FIELD_HASH_FN(ast_bucket_metadata, name)
/*! \brief Comparison function for file metadata */
-static int bucket_file_metadata_cmp(void *obj, void *arg, int flags)
-{
- struct ast_bucket_metadata *metadata1 = obj, *metadata2 = arg;
- const char *name = arg;
-
- return !strcmp(metadata1->name, flags & OBJ_KEY ? name : metadata2->name) ? CMP_MATCH | CMP_STOP : 0;
-}
+AO2_STRING_FIELD_CMP_FN(ast_bucket_metadata, name)
/*! \brief Destructor for bucket files */
static void bucket_file_destroy(void *obj)
@@ -673,7 +650,7 @@
}
file->metadata = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, METADATA_BUCKETS,
- bucket_file_metadata_hash, bucket_file_metadata_cmp);
+ ast_bucket_metadata_hash_fn, ast_bucket_metadata_cmp_fn);
if (!file->metadata) {
return NULL;
}
@@ -941,33 +918,10 @@
}
/*! \brief Hashing function for scheme container */
-static int bucket_scheme_hash(const void *obj, const int flags)
-{
- const struct ast_bucket_scheme *object;
- const char *key;
-
- switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
- case OBJ_KEY:
- key = obj;
- return ast_str_hash(key);
- case OBJ_POINTER:
- object = obj;
- return ast_str_hash(object->name);
- default:
- /* Hash can only work on something with a full key */
- ast_assert(0);
- return 0;
- }
-}
+AO2_STRING_FIELD_HASH_FN(ast_bucket_scheme, name)
/*! \brief Comparison function for scheme container */
-static int bucket_scheme_cmp(void *obj, void *arg, int flags)
-{
- struct ast_bucket_scheme *scheme1 = obj, *scheme2 = arg;
- const char *name = arg;
-
- return !strcmp(scheme1->name, flags & OBJ_KEY ? name : scheme2->name) ? CMP_MATCH | CMP_STOP : 0;
-}
+AO2_STRING_FIELD_CMP_FN(ast_bucket_scheme, name)
/*! \brief Cleanup function for graceful shutdowns */
static void bucket_cleanup(void)
@@ -1000,8 +954,8 @@
{
ast_register_cleanup(&bucket_cleanup);
- schemes = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, SCHEME_BUCKETS, bucket_scheme_hash,
- bucket_scheme_cmp);
+ schemes = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, SCHEME_BUCKETS,
+ ast_bucket_scheme_hash_fn, ast_bucket_scheme_cmp_fn);
if (!schemes) {
ast_log(LOG_ERROR, "Failed to create container for Bucket schemes\n");
return -1;
diff --git a/main/ccss.c b/main/ccss.c
index 892a8a5..ed0bba7 100644
--- a/main/ccss.c
+++ b/main/ccss.c
@@ -1380,19 +1380,8 @@
int core_id;
};
-static int generic_monitor_hash_fn(const void *obj, const int flags)
-{
- const struct generic_monitor_instance_list *generic_list = obj;
- return ast_str_hash(generic_list->device_name);
-}
-
-static int generic_monitor_cmp_fn(void *obj, void *arg, int flags)
-{
- const struct generic_monitor_instance_list *generic_list1 = obj;
- const struct generic_monitor_instance_list *generic_list2 = arg;
-
- return !strcmp(generic_list1->device_name, generic_list2->device_name) ? CMP_MATCH | CMP_STOP : 0;
-}
+AO2_STRING_FIELD_HASH_FN(generic_monitor_instance_list, device_name)
+AO2_STRING_FIELD_CMP_FN(generic_monitor_instance_list, device_name)
static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name)
{
@@ -4665,8 +4654,8 @@
return -1;
}
if (!(generic_monitors = ao2_t_container_alloc(CC_CORE_INSTANCES_BUCKETS,
- generic_monitor_hash_fn, generic_monitor_cmp_fn,
- "Create generic monitor container"))) {
+ generic_monitor_instance_list_hash_fn, generic_monitor_instance_list_cmp_fn,
+ "Create generic monitor container"))) {
return -1;
}
if (!(cc_core_taskprocessor = ast_taskprocessor_get("CCSS_core", TPS_REF_DEFAULT))) {
diff --git a/main/cel.c b/main/cel.c
index 24a35be..e4fae6d 100644
--- a/main/cel.c
+++ b/main/cel.c
@@ -329,129 +329,16 @@
};
/*! \brief Hashing function for cel_backend */
-static int cel_backend_hash(const void *obj, int flags)
-{
- const struct cel_backend *backend;
- const char *name;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- backend = obj;
- name = backend->name;
- break;
- case OBJ_SEARCH_KEY:
- name = obj;
- break;
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
-
- return ast_str_hash(name);
-}
+AO2_STRING_FIELD_HASH_FN(cel_backend, name)
/*! \brief Comparator function for cel_backend */
-static int cel_backend_cmp(void *obj, void *arg, int flags)
-{
- const struct cel_backend *object_left = obj;
- const struct cel_backend *object_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->name;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->name, 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(object_left->name, 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;
-}
+AO2_STRING_FIELD_CMP_FN(cel_backend, name)
/*! \brief Hashing function for dialstatus container */
-static int dialstatus_hash(const void *obj, int flags)
-{
- const struct cel_dialstatus *dialstatus;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- break;
- case OBJ_SEARCH_OBJECT:
- dialstatus = obj;
- key = dialstatus->uniqueid;
- break;
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
+AO2_STRING_FIELD_HASH_FN(cel_dialstatus, uniqueid)
/*! \brief Comparator function for dialstatus container */
-static int dialstatus_cmp(void *obj, void *arg, int flags)
-{
- struct cel_dialstatus *object_left = obj;
- struct cel_dialstatus *object_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->uniqueid;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->uniqueid, 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(object_left->uniqueid, 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;
-}
+AO2_STRING_FIELD_CMP_FN(cel_dialstatus, uniqueid)
unsigned int ast_cel_check_enabled(void)
{
@@ -1665,71 +1552,14 @@
return ret;
}
-static int lid_hash(const void *obj, const int flags)
-{
- const struct cel_linkedid *lid;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- break;
- case OBJ_SEARCH_OBJECT:
- lid = obj;
- key = lid->id;
- break;
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
-
-static int lid_cmp(void *obj, void *arg, int flags)
-{
- const struct cel_linkedid *object_left = obj;
- const struct cel_linkedid *object_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->id;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->id, 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(object_left->id, 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;
-}
+AO2_STRING_FIELD_HASH_FN(cel_linkedid, id)
+AO2_STRING_FIELD_CMP_FN(cel_linkedid, id)
int ast_cel_engine_init(void)
{
struct ao2_container *container;
- container = ao2_container_alloc(NUM_APP_BUCKETS, lid_hash, lid_cmp);
+ container = ao2_container_alloc(NUM_APP_BUCKETS, cel_linkedid_hash_fn, cel_linkedid_cmp_fn);
ao2_global_obj_replace_unref(cel_linkedids, container);
ao2_cleanup(container);
if (!container) {
@@ -1738,7 +1568,7 @@
}
container = ao2_container_alloc(NUM_DIALSTATUS_BUCKETS,
- dialstatus_hash, dialstatus_cmp);
+ cel_dialstatus_hash_fn, cel_dialstatus_cmp_fn);
ao2_global_obj_replace_unref(cel_dialstatus_store, container);
ao2_cleanup(container);
if (!container) {
@@ -1756,7 +1586,7 @@
return -1;
}
- container = ao2_container_alloc(BACKEND_BUCKETS, cel_backend_hash, cel_backend_cmp);
+ container = ao2_container_alloc(BACKEND_BUCKETS, cel_backend_hash_fn, cel_backend_cmp_fn);
ao2_global_obj_replace_unref(cel_backends, container);
ao2_cleanup(container);
if (!container) {
diff --git a/main/codec.c b/main/codec.c
index 5b262f6..d6de072 100644
--- a/main/codec.c
+++ b/main/codec.c
@@ -73,24 +73,7 @@
int __ast_codec_register_with_format(struct ast_codec *codec, const char *format_name,
struct ast_module *mod);
-static int codec_hash(const void *obj, int flags)
-{
- const struct ast_codec *codec;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- return ast_str_hash(key);
- case OBJ_SEARCH_OBJECT:
- codec = obj;
- return ast_str_hash(codec->name);
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
-}
+AO2_STRING_FIELD_HASH_FN(ast_codec, name)
static int codec_cmp(void *obj, void *arg, int flags)
{
@@ -265,7 +248,8 @@
int ast_codec_init(void)
{
- codecs = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CODEC_BUCKETS, codec_hash, codec_cmp);
+ codecs = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CODEC_BUCKETS,
+ ast_codec_hash_fn, codec_cmp);
if (!codecs) {
return -1;
}
diff --git a/main/endpoints.c b/main/endpoints.c
index d314052..f1608f3 100644
--- a/main/endpoints.c
+++ b/main/endpoints.c
@@ -76,53 +76,8 @@
struct stasis_forward *tech_forward;
};
-static int endpoint_hash(const void *obj, int flags)
-{
- const struct ast_endpoint *endpoint;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- return ast_str_hash(key);
- case OBJ_SEARCH_OBJECT:
- endpoint = obj;
- return ast_str_hash(endpoint->id);
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
-}
-
-static int endpoint_cmp(void *obj, void *arg, int flags)
-{
- const struct ast_endpoint *left = obj;
- const struct ast_endpoint *right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = right->id;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(left->id, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(left->id, right_key, strlen(right_key));
- break;
- default:
- ast_assert(0);
- cmp = 0;
- break;
- }
- if (cmp) {
- return 0;
- }
-
- return CMP_MATCH;
-}
+AO2_STRING_FIELD_HASH_FN(ast_endpoint, id)
+AO2_STRING_FIELD_CMP_FN(ast_endpoint, id)
struct ast_endpoint *ast_endpoint_find_by_id(const char *id)
{
@@ -522,14 +477,14 @@
{
ast_register_cleanup(endpoint_cleanup);
- endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, endpoint_hash,
- endpoint_cmp);
+ endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, ast_endpoint_hash_fn,
+ ast_endpoint_cmp_fn);
if (!endpoints) {
return -1;
}
- tech_endpoints = ao2_container_alloc(TECH_ENDPOINT_BUCKETS, endpoint_hash,
- endpoint_cmp);
+ tech_endpoints = ao2_container_alloc(TECH_ENDPOINT_BUCKETS, ast_endpoint_hash_fn,
+ ast_endpoint_cmp_fn);
if (!tech_endpoints) {
return -1;
}
diff --git a/main/format.c b/main/format.c
index 09e736c..5ba9ffe 100644
--- a/main/format.c
+++ b/main/format.c
@@ -64,53 +64,8 @@
/*! \brief Container for registered format interfaces */
static struct ao2_container *interfaces;
-static int format_interface_hash(const void *obj, int flags)
-{
- const struct format_interface *format_interface;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- return ast_str_hash(key);
- case OBJ_SEARCH_OBJECT:
- format_interface = obj;
- return ast_str_hash(format_interface->codec);
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
-}
-
-static int format_interface_cmp(void *obj, void *arg, int flags)
-{
- const struct format_interface *left = obj;
- const struct format_interface *right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- cmp = strcmp(left->codec, right->codec);
- break;
- case OBJ_SEARCH_KEY:
- cmp = strcmp(left->codec, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(left->codec, right_key, strlen(right_key));
- break;
- default:
- ast_assert(0);
- cmp = 0;
- break;
- }
- if (cmp) {
- return 0;
- }
-
- return CMP_MATCH;
-}
+AO2_STRING_FIELD_HASH_FN(format_interface, codec)
+AO2_STRING_FIELD_CMP_FN(format_interface, codec)
/*! \brief Function called when the process is shutting down */
static void format_shutdown(void)
@@ -121,8 +76,8 @@
int ast_format_init(void)
{
- interfaces = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, FORMAT_INTERFACE_BUCKETS, format_interface_hash,
- format_interface_cmp);
+ interfaces = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, FORMAT_INTERFACE_BUCKETS,
+ format_interface_hash_fn, format_interface_cmp_fn);
if (!interfaces) {
return -1;
}
diff --git a/main/media_cache.c b/main/media_cache.c
index c58acdd..90057dc 100644
--- a/main/media_cache.c
+++ b/main/media_cache.c
@@ -49,62 +49,6 @@
/*! Our one and only container holding media items */
static struct ao2_container *media_cache;
-/*!
- * \internal
- * \brief Hashing function for file metadata
- */
-static int media_cache_hash(const void *obj, const int flags)
-{
- const struct ast_bucket_file *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);
- break;
- default:
- /* Hash can only work on something with a full key */
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
-
-/*!
- * \internal
- * \brief Comparison function for file metadata
- */
-static int media_cache_cmp(void *obj, void *arg, int flags)
-{
- struct ast_bucket_file *left = obj;
- struct ast_bucket_file *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(right);
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(ast_sorcery_object_get_id(left), right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(ast_sorcery_object_get_id(left), right_key, strlen(right_key));
- break;
- default:
- ast_assert(0);
- cmp = 0;
- break;
- }
-
- return cmp ? 0 : CMP_MATCH | CMP_STOP;
-}
-
-
int ast_media_cache_exists(const char *uri)
{
struct ast_bucket_file *bucket_file;
@@ -712,7 +656,7 @@
ast_register_atexit(media_cache_shutdown);
media_cache = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, AO2_BUCKETS,
- media_cache_hash, media_cache_cmp);
+ ast_sorcery_object_id_hash, ast_sorcery_object_id_compare);
if (!media_cache) {
return -1;
}
diff --git a/main/named_acl.c b/main/named_acl.c
index 0e0385b..3a4c454 100644
--- a/main/named_acl.c
+++ b/main/named_acl.c
@@ -105,19 +105,8 @@
char name[ACL_NAME_LENGTH]; /* Same max length as a configuration category */
};
-static int named_acl_hash_fn(const void *obj, const int flags)
-{
- const struct named_acl *entry = obj;
- return ast_str_hash(entry->name);
-}
-
-static int named_acl_cmp_fn(void *obj, void *arg, const int flags)
-{
- struct named_acl *entry1 = obj;
- struct named_acl *entry2 = arg;
-
- return (!strcmp(entry1->name, entry2->name)) ? (CMP_MATCH | CMP_STOP) : 0;
-}
+AO2_STRING_FIELD_HASH_FN(named_acl, name)
+AO2_STRING_FIELD_CMP_FN(named_acl, name)
/*! \brief destructor for named_acl_config */
static void named_acl_config_destructor(void *obj)
diff --git a/main/named_locks.c b/main/named_locks.c
index b38539c..2fb8f44 100644
--- a/main/named_locks.c
+++ b/main/named_locks.c
@@ -41,46 +41,8 @@
struct ast_named_lock {
};
-static int named_locks_hash(const void *obj, const int flags)
-{
- const struct named_lock_proxy *lock = obj;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- return ast_str_hash(obj);
- case OBJ_SEARCH_OBJECT:
- return ast_str_hash(lock->key);
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
-}
-
-static int named_locks_cmp(void *obj_left, void *obj_right, int flags)
-{
- const struct named_lock_proxy *object_left = obj_left;
- const struct named_lock_proxy *object_right = obj_right;
- const char *right_key = obj_right;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->key;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->key, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(object_left->key, right_key, strlen(right_key));
- break;
- default:
- cmp = 0;
- break;
- }
-
- return cmp ? 0 : CMP_MATCH;
-}
+AO2_STRING_FIELD_HASH_FN(named_lock_proxy, key)
+AO2_STRING_FIELD_CMP_FN(named_lock_proxy, key)
static void named_locks_shutdown(void)
{
@@ -90,7 +52,7 @@
int ast_named_locks_init(void)
{
named_locks = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
- NAMED_LOCKS_BUCKETS, named_locks_hash, NULL, named_locks_cmp);
+ NAMED_LOCKS_BUCKETS, named_lock_proxy_hash_fn, NULL, named_lock_proxy_cmp_fn);
if (!named_locks) {
return -1;
}
diff --git a/main/sorcery.c b/main/sorcery.c
index 51b55c5..a55d5c7 100644
--- a/main/sorcery.c
+++ b/main/sorcery.c
@@ -343,102 +343,12 @@
return NULL;
}
-/*! \brief Hashing function for sorcery wizards */
-static int sorcery_wizard_hash(const void *obj, const int flags)
-{
- const struct ast_sorcery_internal_wizard *object;
- const char *key;
+/*! \brief Hashing and comparison functions for sorcery wizards */
+AO2_STRING_FIELD_HASH_FN(ast_sorcery_internal_wizard, callbacks.name)
+AO2_STRING_FIELD_CMP_FN(ast_sorcery_internal_wizard, callbacks.name)
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- break;
- case OBJ_SEARCH_OBJECT:
- object = obj;
- key = object->callbacks.name;
- break;
- default:
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
-
-/*! \brief Comparator function for sorcery wizards */
-static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
-{
- const struct ast_sorcery_internal_wizard *object_left = obj;
- const struct ast_sorcery_internal_wizard *object_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->callbacks.name;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->callbacks.name, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(object_left->callbacks.name, right_key, strlen(right_key));
- break;
- default:
- cmp = 0;
- break;
- }
- if (cmp) {
- return 0;
- }
- return CMP_MATCH;
-}
-
-/*! \brief Hashing function for sorcery wizards */
-static int object_type_field_hash(const void *obj, const int flags)
-{
- const struct ast_sorcery_object_field *object_field;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- break;
- case OBJ_SEARCH_OBJECT:
- object_field = obj;
- key = object_field->name;
- break;
- default:
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
-
-static int object_type_field_cmp(void *obj, void *arg, int flags)
-{
- const struct ast_sorcery_object_field *field_left = obj;
- const struct ast_sorcery_object_field *field_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = field_right->name;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(field_left->name, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(field_left->name, right_key, strlen(right_key));
- break;
- default:
- cmp = 0;
- break;
- }
- if (cmp) {
- return 0;
- }
- return CMP_MATCH;
-}
+AO2_STRING_FIELD_HASH_FN(ast_sorcery_object_field, name)
+AO2_STRING_FIELD_CMP_FN(ast_sorcery_object_field, name)
/*! \brief Cleanup function for graceful shutdowns */
static void sorcery_cleanup(void)
@@ -454,53 +364,10 @@
}
/*! \brief Compare function for sorcery instances */
-static int sorcery_instance_cmp(void *obj, void *arg, int flags)
-{
- const struct sorcery_proxy *object_left = obj;
- const struct sorcery_proxy *object_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->module_name;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->module_name, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
- break;
- default:
- cmp = 0;
- break;
- }
- if (cmp) {
- return 0;
- }
- return CMP_MATCH;
-}
+AO2_STRING_FIELD_CMP_FN(sorcery_proxy, module_name)
/*! \brief Hashing function for sorcery instances */
-static int sorcery_instance_hash(const void *obj, const int flags)
-{
- const struct sorcery_proxy *object;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- break;
- case OBJ_SEARCH_OBJECT:
- object = obj;
- key = object->module_name;
- break;
- default:
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
+AO2_STRING_FIELD_HASH_FN(sorcery_proxy, module_name)
int ast_sorcery_init(void)
{
@@ -519,7 +386,7 @@
}
wizards = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, WIZARD_BUCKETS,
- sorcery_wizard_hash, NULL, sorcery_wizard_cmp);
+ ast_sorcery_internal_wizard_hash_fn, NULL, ast_sorcery_internal_wizard_cmp_fn);
if (!wizards) {
sorcery_cleanup();
return -1;
@@ -532,7 +399,7 @@
}
instances = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, INSTANCE_BUCKETS,
- sorcery_instance_hash, NULL, sorcery_instance_cmp);
+ sorcery_proxy_hash_fn, NULL, sorcery_proxy_cmp_fn);
if (!instances) {
sorcery_cleanup();
return -1;
@@ -711,53 +578,8 @@
}
/*! \brief Hashing function for sorcery types */
-static int sorcery_type_hash(const void *obj, const int flags)
-{
- const struct ast_sorcery_object_type *object;
- const char *key;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- key = obj;
- break;
- case OBJ_SEARCH_OBJECT:
- object = obj;
- key = object->name;
- break;
- default:
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
-}
-
-/*! \brief Comparator function for sorcery types */
-static int sorcery_type_cmp(void *obj, void *arg, int flags)
-{
- const struct ast_sorcery_object_type *object_left = obj;
- const struct ast_sorcery_object_type *object_right = arg;
- const char *right_key = arg;
- int cmp;
-
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- right_key = object_right->name;
- /* Fall through */
- case OBJ_SEARCH_KEY:
- cmp = strcmp(object_left->name, right_key);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(object_left->name, right_key, strlen(right_key));
- break;
- default:
- cmp = 0;
- break;
- }
- if (cmp) {
- return 0;
- }
- return CMP_MATCH;
-}
+AO2_STRING_FIELD_HASH_FN(ast_sorcery_object_type, name)
+AO2_STRING_FIELD_CMP_FN(ast_sorcery_object_type, name)
static void sorcery_proxy_cb(void *weakproxy, void *data)
{
@@ -802,7 +624,9 @@
goto failure_cleanup;
}
- if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
+ sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS,
+ ast_sorcery_object_type_hash_fn, ast_sorcery_object_type_cmp_fn);
+ if (!sorcery->types) {
goto failure_cleanup;
}
@@ -878,7 +702,7 @@
}
object_type->fields = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
- OBJECT_FIELD_BUCKETS, object_type_field_hash, NULL, object_type_field_cmp);
+ OBJECT_FIELD_BUCKETS, ast_sorcery_object_field_hash_fn, NULL, ast_sorcery_object_field_cmp_fn);
if (!object_type->fields) {
ao2_ref(object_type, -1);
return NULL;
--
To view, visit https://gerrit.asterisk.org/7779
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ide4c1449a894bce70dea1fef664dade9b57578f1
Gerrit-Change-Number: 7779
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180103/972a7237/attachment-0001.html>
More information about the asterisk-code-review
mailing list