diff options
author | Sean Bright <sean@malleable.com> | 2008-11-25 01:01:49 +0000 |
---|---|---|
committer | Sean Bright <sean@malleable.com> | 2008-11-25 01:01:49 +0000 |
commit | fd8caa1778e486b26badd4691ad50447c1f08631 (patch) | |
tree | 6af2cfc1b0ec46aa179c5019f00274f1eba84fce /main | |
parent | 69d85eaca9e8636c2af2dd9bd29e61d027d55c2c (diff) |
This is basically a complete rollback of r155401, as it was determined that
it would be best to maintain API compatibility. Instead, this commit introduces
ao2_callback_data() which is functionally identical to ao2_callback() except
that it allows you to pass arbitrary data to the callback.
Reviewed by Mark Michelson via ReviewBoard:
http://reviewboard.digium.com/r/64
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@158959 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main')
-rw-r--r-- | main/astobj2.c | 102 | ||||
-rw-r--r-- | main/config.c | 4 | ||||
-rw-r--r-- | main/features.c | 4 | ||||
-rw-r--r-- | main/manager.c | 4 | ||||
-rw-r--r-- | main/taskprocessor.c | 6 |
5 files changed, 85 insertions, 35 deletions
diff --git a/main/astobj2.c b/main/astobj2.c index df2f08942..02f643c81 100644 --- a/main/astobj2.c +++ b/main/astobj2.c @@ -120,6 +120,11 @@ static inline struct astobj2 *INTERNAL_OBJ(void *user_data) return p; } +enum ao2_callback_type { + DEFAULT, + WITH_DATA, +}; + /*! * \brief convert from a pointer _p to an astobj2 object * @@ -135,7 +140,7 @@ static struct ao2_container *__ao2_container_alloc(struct ao2_container *c, cons ao2_callback_fn *cmp_fn); static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data); static void *__ao2_callback(struct ao2_container *c, - const enum search_flags flags, ao2_callback_fn *cb_fn, void *arg, void *data, + const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type, char *tag, char *file, int line, const char *funcname); static void * __ao2_iterator_next(struct ao2_iterator *a, struct bucket_list **q); @@ -527,7 +532,7 @@ void *_ao2_link(struct ao2_container *c, void *user_data) /*! * \brief another convenience function is a callback that matches on address */ -int ao2_match_by_addr(void *user_data, void *arg, void *data, int flags) +int ao2_match_by_addr(void *user_data, void *arg, int flags) { return (user_data == arg) ? (CMP_MATCH | CMP_STOP) : 0; } @@ -542,7 +547,7 @@ void *_ao2_unlink_debug(struct ao2_container *c, void *user_data, char *tag, if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */ return NULL; - _ao2_callback_debug(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, NULL, tag, file, line, funcname); + _ao2_callback_debug(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, tag, file, line, funcname); return NULL; } @@ -552,7 +557,7 @@ void *_ao2_unlink(struct ao2_container *c, void *user_data) if (INTERNAL_OBJ(user_data) == NULL) /* safety check on the argument */ return NULL; - _ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data, NULL); + _ao2_callback(c, OBJ_UNLINK | OBJ_POINTER | OBJ_NODATA, ao2_match_by_addr, user_data); return NULL; } @@ -560,9 +565,18 @@ void *_ao2_unlink(struct ao2_container *c, void *user_data) /*! * \brief special callback that matches all */ -static int cb_true(void *user_data, void *arg, void *data, int flags) +static int cb_true(void *user_data, void *arg, int flags) +{ + ast_log(LOG_ERROR, "Using default callback (cb_true). If you see this, something is strange!\n"); + return CMP_MATCH; +} + +/*! + * \brief similar to cb_true, but is an ao2_callback_data_fn instead + */ +static int cb_true_data(void *user_data, void *arg, void *data, int flags) { - ast_log(LOG_ERROR,"If you see this, something is strange!\n"); + ast_log(LOG_ERROR, "Using default callback (cb_true_data). If you see this, something is strange!\n"); return CMP_MATCH; } @@ -575,11 +589,13 @@ static int cb_true(void *user_data, void *arg, void *data, int flags) * called as often as, say, the ao2_ref func is called. */ static void *__ao2_callback(struct ao2_container *c, - const enum search_flags flags, ao2_callback_fn *cb_fn, void *arg, void *data, + const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type, char *tag, char *file, int line, const char *funcname) { int i, last; /* search boundaries */ void *ret = NULL; + ao2_callback_fn *cb_default = NULL; + ao2_callback_data_fn *cb_withdata = NULL; if (INTERNAL_OBJ(c) == NULL) /* safety check on the argument */ return NULL; @@ -590,8 +606,22 @@ static void *__ao2_callback(struct ao2_container *c, } /* override the match function if necessary */ - if (cb_fn == NULL) /* if NULL, match everything */ - cb_fn = cb_true; + if (cb_fn == NULL) { /* if NULL, match everything */ + if (type == WITH_DATA) { + cb_withdata = cb_true_data; + } else { + cb_default = cb_true; + } + } else { + /* We do this here to avoid the per object casting penalty (even though + that is probably optimized away anyway. */ + if (type == WITH_DATA) { + cb_withdata = cb_fn; + } else { + cb_default = cb_fn; + } + } + /* * XXX this can be optimized. * If we have a hash function and lookup by pointer, @@ -618,7 +648,13 @@ static void *__ao2_callback(struct ao2_container *c, struct bucket_list *cur; AST_LIST_TRAVERSE_SAFE_BEGIN(&c->buckets[i], cur, entry) { - int match = cb_fn(EXTERNAL_OBJ(cur->astobj), arg, data, flags) & (CMP_MATCH | CMP_STOP); + int match = (CMP_MATCH | CMP_STOP); + + if (type == WITH_DATA) { + match &= cb_withdata(EXTERNAL_OBJ(cur->astobj), arg, data, flags); + } else { + match &= cb_default(EXTERNAL_OBJ(cur->astobj), arg, flags); + } /* we found the object, performing operations according flags */ if (match == 0) { /* no match, no stop, continue */ @@ -674,29 +710,43 @@ static void *__ao2_callback(struct ao2_container *c, void *_ao2_callback_debug(struct ao2_container *c, const enum search_flags flags, - ao2_callback_fn *cb_fn, void *arg, void *data, + ao2_callback_fn *cb_fn, void *arg, + char *tag, char *file, int line, const char *funcname) +{ + return __ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, tag, file, line, funcname); +} + +void *_ao2_callback(struct ao2_container *c, const enum search_flags flags, + ao2_callback_fn *cb_fn, void *arg) +{ + return __ao2_callback(c,flags, cb_fn, arg, NULL, DEFAULT, NULL, NULL, 0, NULL); +} + +void *_ao2_callback_data_debug(struct ao2_container *c, + const enum search_flags flags, + ao2_callback_data_fn *cb_fn, void *arg, void *data, char *tag, char *file, int line, const char *funcname) { - return __ao2_callback(c,flags, cb_fn, arg, data, tag, file, line, funcname); + return __ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, tag, file, line, funcname); } -void *_ao2_callback(struct ao2_container *c,const enum search_flags flags, - ao2_callback_fn *cb_fn, void *arg, void *data) +void *_ao2_callback_data(struct ao2_container *c, const enum search_flags flags, + ao2_callback_data_fn *cb_fn, void *arg, void *data) { - return __ao2_callback(c,flags, cb_fn, arg, data, NULL, NULL, 0, NULL); + return __ao2_callback(c, flags, cb_fn, arg, data, WITH_DATA, NULL, NULL, 0, NULL); } /*! * the find function just invokes the default callback with some reasonable flags. */ -void *_ao2_find_debug(struct ao2_container *c, void *arg, void *data, enum search_flags flags, char *tag, char *file, int line, const char *funcname) +void *_ao2_find_debug(struct ao2_container *c, 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, data, tag, file, line, funcname); + return _ao2_callback_debug(c, flags, c->cmp_fn, arg, tag, file, line, funcname); } -void *_ao2_find(struct ao2_container *c, void *arg, void *data, enum search_flags flags) +void *_ao2_find(struct ao2_container *c, void *arg, enum search_flags flags) { - return _ao2_callback(c, flags, c->cmp_fn, arg, data); + return _ao2_callback(c, flags, c->cmp_fn, arg); } /*! @@ -809,13 +859,13 @@ void * _ao2_iterator_next(struct ao2_iterator *a) /* callback for destroying container. * we can make it simple as we know what it does */ -static int cd_cb(void *obj, void *arg, void *data, int flag) +static int cd_cb(void *obj, void *arg, int flag) { _ao2_ref(obj, -1); return 0; } -static int cd_cb_debug(void *obj, void *arg, void *data, int flag) +static int cd_cb_debug(void *obj, void *arg, int flag) { _ao2_ref_debug(obj, -1, "deref object via container destroy", __FILE__, __LINE__, __PRETTY_FUNCTION__); return 0; @@ -826,7 +876,7 @@ static void container_destruct(void *_c) struct ao2_container *c = _c; int i; - _ao2_callback(c, OBJ_UNLINK, cd_cb, NULL, NULL); + _ao2_callback(c, OBJ_UNLINK, cd_cb, NULL); for (i = 0; i < c->n_buckets; i++) { struct bucket_list *current; @@ -846,7 +896,7 @@ static void container_destruct_debug(void *_c) struct ao2_container *c = _c; int i; - _ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__); + _ao2_callback_debug(c, OBJ_UNLINK, cd_cb_debug, NULL, "container_destruct_debug called", __FILE__, __LINE__, __PRETTY_FUNCTION__); for (i = 0; i < c->n_buckets; i++) { struct bucket_list *current; @@ -862,7 +912,7 @@ static void container_destruct_debug(void *_c) } #ifdef AO2_DEBUG -static int print_cb(void *obj, void *arg, void *data, int flag) +static int print_cb(void *obj, void *arg, int flag) { int *fd = arg; char *s = (char *)obj; @@ -954,7 +1004,7 @@ static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cl ao2_t_ref(obj, -1, "test"); } ast_cli(a->fd, "testing callbacks\n"); - ao2_t_callback(c1, 0, print_cb, &a->fd, NULL, "test callback"); + ao2_t_callback(c1, 0, print_cb, &a->fd, "test callback"); ast_cli(a->fd, "testing iterators, remove every second object\n"); { struct ao2_iterator ai; @@ -975,7 +1025,7 @@ static char *handle_astobj2_test(struct ast_cli_entry *e, int cmd, struct ast_cl } } ast_cli(a->fd, "testing callbacks again\n"); - ao2_t_callback(c1, 0, print_cb, &a->fd, NULL, "test callback"); + ao2_t_callback(c1, 0, print_cb, &a->fd, "test callback"); ast_verbose("now you should see an error message:\n"); ao2_t_ref(&i, -1, ""); /* i is not a valid object so we print an error here */ diff --git a/main/config.c b/main/config.c index 216958065..4cef91a55 100644 --- a/main/config.c +++ b/main/config.c @@ -149,7 +149,7 @@ static int hash_string(const void *obj, const int flags) return total; } -static int hashtab_compare_strings(void *a, void *b, void *data, int flags) +static int hashtab_compare_strings(void *a, void *b, int flags) { const struct inclfile *ae = a, *be = b; return !strcmp(ae->fname, be->fname) ? CMP_MATCH | CMP_STOP : 0; @@ -1517,7 +1517,7 @@ static void set_fn(char *fn, int fn_size, const char *file, const char *configfi else snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file); lookup.fname = fn; - *fi = ao2_find(fileset, &lookup, NULL, OBJ_POINTER); + *fi = ao2_find(fileset, &lookup, OBJ_POINTER); if (!(*fi)) { /* set up a file scratch pad */ struct inclfile *fx = ao2_alloc(sizeof(struct inclfile), inclfile_destroy); diff --git a/main/features.c b/main/features.c index 7fd76a62f..561b2a57e 100644 --- a/main/features.c +++ b/main/features.c @@ -296,7 +296,7 @@ static int parkinglot_hash_cb(const void *obj, const int flags) return ast_str_case_hash(parkinglot->name); } -static int parkinglot_cmp_cb(void *obj, void *arg, void *data, int flags) +static int parkinglot_cmp_cb(void *obj, void *arg, int flags) { struct ast_parkinglot *parkinglot = obj, *parkinglot2 = arg; @@ -2855,7 +2855,7 @@ struct ast_parkinglot *find_parkinglot(const char *name) ast_copy_string(tmp_parkinglot.name, name, sizeof(tmp_parkinglot.name)); - parkinglot = ao2_find(parkinglots, &tmp_parkinglot, NULL, OBJ_POINTER); + parkinglot = ao2_find(parkinglots, &tmp_parkinglot, OBJ_POINTER); if (parkinglot && option_debug) ast_log(LOG_DEBUG, "Found Parkinglot: %s\n", parkinglot->name); diff --git a/main/manager.c b/main/manager.c index 4af2b79e1..d2a76bd02 100644 --- a/main/manager.c +++ b/main/manager.c @@ -3568,7 +3568,7 @@ static int variable_count_hash_fn(const void *vvc, const int flags) return res; } -static int variable_count_cmp_fn(void *obj, void *vstr, void *data, int flags) +static int variable_count_cmp_fn(void *obj, void *vstr, int flags) { /* Due to the simplicity of struct variable_count, it makes no difference * if you pass in objects or strings, the same operation applies. This is @@ -3677,7 +3677,7 @@ static void xml_translate(struct ast_str **out, char *in, struct ast_variable *v if (!in_data) { /* build appropriate line start */ ast_str_append(out, 0, xml ? " " : "<tr><td>"); - if ((vc = ao2_find(vco, var, NULL, 0))) + if ((vc = ao2_find(vco, var, 0))) vc->count++; else { /* Create a new entry for this one */ diff --git a/main/taskprocessor.c b/main/taskprocessor.c index 6fad190f2..954830410 100644 --- a/main/taskprocessor.c +++ b/main/taskprocessor.c @@ -93,7 +93,7 @@ AST_MUTEX_DEFINE_STATIC(cli_ping_cond_lock); /*! \brief The astobj2 hash callback for taskprocessors */ static int tps_hash_cb(const void *obj, const int flags); /*! \brief The astobj2 compare callback for taskprocessors */ -static int tps_cmp_cb(void *obj, void *arg, void *data, int flags); +static int tps_cmp_cb(void *obj, void *arg, int flags); /*! \brief The task processing function executed by a taskprocessor */ static void *tps_processing_function(void *data); @@ -335,7 +335,7 @@ static int tps_hash_cb(const void *obj, const int flags) } /* compare callback for astobj2 */ -static int tps_cmp_cb(void *obj, void *arg, void *data, int flags) +static int tps_cmp_cb(void *obj, void *arg, int flags) { struct ast_taskprocessor *lhs = obj, *rhs = arg; @@ -415,7 +415,7 @@ struct ast_taskprocessor *ast_taskprocessor_get(char *name, enum ast_tps_options return NULL; } ao2_lock(tps_singletons); - p = ao2_find(tps_singletons, &tmp_tps, NULL, OBJ_POINTER); + p = ao2_find(tps_singletons, &tmp_tps, OBJ_POINTER); if (p) { ao2_unlock(tps_singletons); return p; |