Index: file.c =================================================================== --- file.c (revision 7720) +++ file.c (working copy) @@ -50,6 +50,7 @@ #include "asterisk/lock.h" #include "asterisk/app.h" #include "asterisk/pbx.h" +#include "asterisk/linkedlists.h" struct ast_format { /*! Name of format */ @@ -79,7 +80,7 @@ /*! Retrieve file comment */ char * (*getcomment)(struct ast_filestream *); /*! Link */ - struct ast_format *next; + AST_LIST_ENTRY(ast_format) list; }; struct ast_filestream { @@ -99,10 +100,8 @@ struct ast_channel *owner; }; -AST_MUTEX_DEFINE_STATIC(formatlock); +static AST_LIST_HEAD_STATIC(formats, ast_format); -static struct ast_format *formats = NULL; - int ast_format_register(const char *name, const char *exts, int format, struct ast_filestream * (*open)(FILE *f), struct ast_filestream * (*rewrite)(FILE *f, const char *comment), @@ -115,23 +114,21 @@ char * (*getcomment)(struct ast_filestream *)) { struct ast_format *tmp; - if (ast_mutex_lock(&formatlock)) { + if (AST_LIST_LOCK(&formats)) { ast_log(LOG_WARNING, "Unable to lock format list\n"); return -1; } - tmp = formats; - while(tmp) { + AST_LIST_TRAVERSE(&formats, tmp, list) { if (!strcasecmp(name, tmp->name)) { - ast_mutex_unlock(&formatlock); + AST_LIST_UNLOCK(&formats); ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", name); return -1; } - tmp = tmp->next; } tmp = malloc(sizeof(struct ast_format)); if (!tmp) { ast_log(LOG_WARNING, "Out of memory\n"); - ast_mutex_unlock(&formatlock); + AST_LIST_UNLOCK(&formats); return -1; } ast_copy_string(tmp->name, name, sizeof(tmp->name)); @@ -146,9 +143,8 @@ tmp->close = close; tmp->format = format; tmp->getcomment = getcomment; - tmp->next = formats; - formats = tmp; - ast_mutex_unlock(&formatlock); + AST_LIST_INSERT_HEAD(&formats, tmp, list); + AST_LIST_UNLOCK(&formats); if (option_verbose > 1) ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", name, exts); return 0; @@ -156,29 +152,30 @@ int ast_format_unregister(const char *name) { - struct ast_format *tmp, *tmpl = NULL; - if (ast_mutex_lock(&formatlock)) { + struct ast_format *tmp; + int res = -1; + + if (AST_LIST_LOCK(&formats)) { ast_log(LOG_WARNING, "Unable to lock format list\n"); return -1; } - tmp = formats; - while(tmp) { + AST_LIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) { if (!strcasecmp(name, tmp->name)) { - if (tmpl) - tmpl->next = tmp->next; - else - formats = tmp->next; + AST_LIST_REMOVE_CURRENT(&formats, list); free(tmp); - ast_mutex_unlock(&formatlock); - if (option_verbose > 1) - ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name); - return 0; + res = 0; } - tmpl = tmp; - tmp = tmp->next; } - ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name); - return -1; + AST_LIST_TRAVERSE_SAFE_END + AST_LIST_UNLOCK(&formats); + + if (tmp) { + if (option_verbose > 1) + ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name); + } else + ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name); + + return res; } int ast_stopstream(struct ast_channel *tmp) @@ -363,15 +360,14 @@ if (action == ACTION_OPEN) ret = -1; /* Check for a specific format */ - if (ast_mutex_lock(&formatlock)) { + if (AST_LIST_LOCK(&formats)) { ast_log(LOG_WARNING, "Unable to lock format list\n"); if (action == ACTION_EXISTS) return 0; else return -1; } - f = formats; - while(f) { + AST_LIST_TRAVERSE(&formats, f, list) { if (!fmt || exts_compare(f->exts, fmt)) { char *stringp=NULL; exts = ast_strdupa(f->exts); @@ -452,9 +448,8 @@ } while(ext); } - f = f->next; } - ast_mutex_unlock(&formatlock); + AST_LIST_UNLOCK(&formats); if ((action == ACTION_EXISTS) || (action == ACTION_OPEN)) res = ret ? ret : -1; return res; @@ -829,12 +824,15 @@ struct ast_filestream *fs = NULL; char *fn; - if (ast_mutex_lock(&formatlock)) { + if (AST_LIST_LOCK(&formats)) { ast_log(LOG_WARNING, "Unable to lock format list\n"); return NULL; } - for (f = formats; f && !fs; f = f->next) { + AST_LIST_TRAVERSE(&formats, f, list) { + if (fs) + break; + if (!exts_compare(f->exts, type)) continue; @@ -861,7 +859,7 @@ free(fn); } - ast_mutex_unlock(&formatlock); + AST_LIST_UNLOCK(&formats); if (!fs) ast_log(LOG_WARNING, "No such format '%s'\n", type); @@ -879,7 +877,7 @@ char *buf = NULL; size_t size = 0; - if (ast_mutex_lock(&formatlock)) { + if (AST_LIST_LOCK(&formats)) { ast_log(LOG_WARNING, "Unable to lock format list\n"); return NULL; } @@ -894,7 +892,10 @@ myflags |= O_WRONLY | O_CREAT; - for (f = formats; f && !fs; f = f->next) { + AST_LIST_TRAVERSE(&formats, f, list) { + if (fs) + break; + if (!exts_compare(f->exts, type)) continue; @@ -976,7 +977,7 @@ free(fn); } - ast_mutex_unlock(&formatlock); + AST_LIST_UNLOCK(&formats); if (!fs) ast_log(LOG_WARNING, "No such format '%s'\n", type); @@ -1252,18 +1253,16 @@ return RESULT_SHOWUSAGE; ast_cli(fd, FORMAT, "Format", "Name", "Extensions"); - if (ast_mutex_lock(&formatlock)) { + if (AST_LIST_LOCK(&formats)) { ast_log(LOG_WARNING, "Unable to lock format list\n"); return -1; } - f = formats; - while(f) { + AST_LIST_TRAVERSE(&formats, f, list) { ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts); - f = f->next; count_fmt++; - }; - ast_mutex_unlock(&formatlock); + } + AST_LIST_UNLOCK(&formats); ast_cli(fd, "%d file formats registered.\n", count_fmt); return RESULT_SUCCESS; #undef FORMAT