<p>George Joseph <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/11655">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Kevin Harwell: Looks good to me, but someone else must approve
Joshua Colp: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_musiconhold: Use a vector instead of custom array allocation<br><br>Change-Id: Ic476a56608b1820ca93dcf68d10cd76fc0b94141<br>---<br>M res/res_musiconhold.c<br>1 file changed, 55 insertions(+), 87 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c</span><br><span>index b598f08..32e77dd 100644</span><br><span>--- a/res/res_musiconhold.c</span><br><span>+++ b/res/res_musiconhold.c</span><br><span>@@ -171,12 +171,8 @@</span><br><span> char announcement[256];</span><br><span> char mode[80];</span><br><span> char digit;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! A dynamically sized array to hold the list of filenames in "files" mode */</span><br><span style="color: hsl(0, 100%, 40%);">- char **filearray;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The current size of the filearray */</span><br><span style="color: hsl(0, 100%, 40%);">- int allowed_files;</span><br><span style="color: hsl(0, 100%, 40%);">- /*! The current number of files loaded into the filearray */</span><br><span style="color: hsl(0, 100%, 40%);">- int total_files;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! A vector of filenames in "files" mode */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_vector_string files;</span><br><span> unsigned int flags;</span><br><span> /*! The format from the MOH source, not applicable to "files" mode */</span><br><span> struct ast_format *format;</span><br><span>@@ -318,6 +314,7 @@</span><br><span> {</span><br><span> struct moh_files_state *state = ast_channel_music_state(chan);</span><br><span> int tries;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t file_count;</span><br><span> </span><br><span> /* Discontinue a stream if it is running already */</span><br><span> if (ast_channel_stream(chan)) {</span><br><span>@@ -335,7 +332,8 @@</span><br><span> state->announcement = 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!state->class->total_files) {</span><br><span style="color: hsl(120, 100%, 40%);">+ file_count = AST_VECTOR_SIZE(&state->class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!file_count) {</span><br><span> ast_log(LOG_WARNING, "No files available for class '%s'\n", state->class->name);</span><br><span> return -1;</span><br><span> }</span><br><span>@@ -343,15 +341,15 @@</span><br><span> if (state->pos == 0 && ast_strlen_zero(state->save_pos_filename)) {</span><br><span> /* First time so lets play the file. */</span><br><span> state->save_pos = -1;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (state->save_pos >= 0 && state->save_pos < state->class->total_files && !strcmp(state->class->filearray[state->save_pos], state->save_pos_filename)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (state->save_pos >= 0 && state->save_pos < file_count && !strcmp(AST_VECTOR_GET(&state->class->files, state->save_pos), state->save_pos_filename)) {</span><br><span> /* If a specific file has been saved confirm it still exists and that it is still valid */</span><br><span> state->pos = state->save_pos;</span><br><span> state->save_pos = -1;</span><br><span> } else if (ast_test_flag(state->class, MOH_SORTMODE) == MOH_RANDOMIZE) {</span><br><span> /* Get a random file and ensure we can open it */</span><br><span> for (tries = 0; tries < 20; tries++) {</span><br><span style="color: hsl(0, 100%, 40%);">- state->pos = ast_random() % state->class->total_files;</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_fileexists(state->class->filearray[state->pos], NULL, NULL) > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ state->pos = ast_random() % file_count;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_fileexists(AST_VECTOR_GET(&state->class->files, state->pos), NULL, NULL) > 0) {</span><br><span> break;</span><br><span> }</span><br><span> }</span><br><span>@@ -360,29 +358,29 @@</span><br><span> } else {</span><br><span> /* This is easy, just increment our position and make sure we don't exceed the total file count */</span><br><span> state->pos++;</span><br><span style="color: hsl(0, 100%, 40%);">- state->pos %= state->class->total_files;</span><br><span style="color: hsl(120, 100%, 40%);">+ state->pos %= file_count;</span><br><span> state->save_pos = -1;</span><br><span> state->samples = 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- for (tries = 0; tries < state->class->total_files; ++tries) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_openstream_full(chan, state->class->filearray[state->pos], ast_channel_language(chan), 1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ for (tries = 0; tries < file_count; ++tries) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_openstream_full(chan, AST_VECTOR_GET(&state->class->files, state->pos), ast_channel_language(chan), 1)) {</span><br><span> break;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", state->class->filearray[state->pos], strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", AST_VECTOR_GET(&state->class->files, state->pos), strerror(errno));</span><br><span> state->pos++;</span><br><span style="color: hsl(0, 100%, 40%);">- state->pos %= state->class->total_files;</span><br><span style="color: hsl(120, 100%, 40%);">+ state->pos %= file_count;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (tries == state->class->total_files) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tries == file_count) {</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span> /* Record the pointer to the filename for position resuming later */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_copy_string(state->save_pos_filename, state->class->filearray[state->pos], sizeof(state->save_pos_filename));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(state->save_pos_filename, AST_VECTOR_GET(&state->class->files, state->pos), sizeof(state->save_pos_filename));</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "%s Opened file %d '%s'\n", ast_channel_name(chan), state->pos, state->class->filearray[state->pos]);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "%s Opened file %d '%s'\n", ast_channel_name(chan), state->pos, state->save_pos_filename);</span><br><span> </span><br><span> if (state->samples) {</span><br><span> size_t loc;</span><br><span>@@ -490,6 +488,7 @@</span><br><span> {</span><br><span> struct moh_files_state *state;</span><br><span> struct mohclass *class = params;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t file_count;</span><br><span> </span><br><span> state = ast_channel_music_state(chan);</span><br><span> if (!state && (state = ast_calloc(1, sizeof(*state)))) {</span><br><span>@@ -505,14 +504,16 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ file_count = AST_VECTOR_SIZE(&class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* Resume MOH from where we left off last time or start from scratch? */</span><br><span style="color: hsl(0, 100%, 40%);">- if (state->save_total != class->total_files || strcmp(state->name, class->name) != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (state->save_total != file_count || strcmp(state->name, class->name) != 0) {</span><br><span> /* Start MOH from scratch. */</span><br><span> ao2_cleanup(state->origwfmt);</span><br><span> ao2_cleanup(state->mohwfmt);</span><br><span> memset(state, 0, sizeof(*state));</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_test_flag(class, MOH_RANDOMIZE) && class->total_files) {</span><br><span style="color: hsl(0, 100%, 40%);">- state->pos = ast_random() % class->total_files;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(class, MOH_RANDOMIZE) && file_count) {</span><br><span style="color: hsl(120, 100%, 40%);">+ state->pos = ast_random() % file_count;</span><br><span> }</span><br><span> }</span><br><span> </span><br><span>@@ -522,7 +523,7 @@</span><br><span> ao2_replace(state->mohwfmt, ast_channel_writeformat(chan));</span><br><span> /* For comparison on restart of MOH (see above) */</span><br><span> ast_copy_string(state->name, class->name, sizeof(state->name));</span><br><span style="color: hsl(0, 100%, 40%);">- state->save_total = class->total_files;</span><br><span style="color: hsl(120, 100%, 40%);">+ state->save_total = file_count;</span><br><span> </span><br><span> moh_post_start(chan, class->name);</span><br><span> </span><br><span>@@ -1131,45 +1132,6 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static int moh_add_file(struct mohclass *class, const char *filepath)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- if (!class->allowed_files) {</span><br><span style="color: hsl(0, 100%, 40%);">- class->filearray = ast_calloc(1, INITIAL_NUM_FILES * sizeof(*class->filearray));</span><br><span style="color: hsl(0, 100%, 40%);">- if (!class->filearray) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- class->allowed_files = INITIAL_NUM_FILES;</span><br><span style="color: hsl(0, 100%, 40%);">- } else if (class->total_files == class->allowed_files) {</span><br><span style="color: hsl(0, 100%, 40%);">- char **new_array;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- new_array = ast_realloc(class->filearray, class->allowed_files * sizeof(*class->filearray) * 2);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!new_array) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- class->filearray = new_array;</span><br><span style="color: hsl(0, 100%, 40%);">- class->allowed_files *= 2;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- class->filearray[class->total_files] = ast_strdup(filepath);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!class->filearray[class->total_files]) {</span><br><span style="color: hsl(0, 100%, 40%);">- return -1;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- class->total_files++;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-static int moh_sort_compare(const void *i1, const void *i2)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- char *s1, *s2;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- s1 = ((char **)i1)[0];</span><br><span style="color: hsl(0, 100%, 40%);">- s2 = ((char **)i2)[0];</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- return strcasecmp(s1, s2);</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static int moh_scan_files(struct mohclass *class) {</span><br><span> </span><br><span> DIR *files_DIR;</span><br><span>@@ -1178,7 +1140,7 @@</span><br><span> char filepath[PATH_MAX];</span><br><span> char *ext;</span><br><span> struct stat statbuf;</span><br><span style="color: hsl(0, 100%, 40%);">- int i;</span><br><span style="color: hsl(120, 100%, 40%);">+ int res;</span><br><span> </span><br><span> if (class->dir[0] != '/') {</span><br><span> snprintf(dir_path, sizeof(dir_path), "%s/%s", ast_config_AST_DATA_DIR, class->dir);</span><br><span>@@ -1192,12 +1154,11 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < class->total_files; i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(class->filearray[i]);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- class->total_files = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_RESET(&class->files, ast_free);</span><br><span> </span><br><span> while ((files_dirent = readdir(files_DIR))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char *filepath_copy;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* The file name must be at least long enough to have the file type extension */</span><br><span> if ((strlen(files_dirent->d_name) < 4))</span><br><span> continue;</span><br><span>@@ -1222,20 +1183,32 @@</span><br><span> *ext = '\0';</span><br><span> </span><br><span> /* if the file is present in multiple formats, ensure we only put it into the list once */</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < class->total_files; i++)</span><br><span style="color: hsl(0, 100%, 40%);">- if (!strcmp(filepath, class->filearray[i]))</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (AST_VECTOR_GET_CMP(&class->files, &filepath[0], !strcmp)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (i == class->total_files) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (moh_add_file(class, filepath))</span><br><span style="color: hsl(0, 100%, 40%);">- break;</span><br><span style="color: hsl(120, 100%, 40%);">+ filepath_copy = ast_strdup(filepath);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!filepath_copy) {</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(class, MOH_SORTALPHA)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = AST_VECTOR_ADD_SORTED(&class->files, filepath_copy, strcasecmp);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ res = AST_VECTOR_APPEND(&class->files, filepath_copy);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (res) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(filepath_copy);</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span> }</span><br><span> }</span><br><span> </span><br><span> closedir(files_DIR);</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_test_flag(class, MOH_SORTALPHA))</span><br><span style="color: hsl(0, 100%, 40%);">- qsort(&class->filearray[0], class->total_files, sizeof(char *), moh_sort_compare);</span><br><span style="color: hsl(0, 100%, 40%);">- return class->total_files;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_COMPACT(&class->files);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_VECTOR_SIZE(&class->files);</span><br><span> }</span><br><span> </span><br><span> static int init_files_class(struct mohclass *class)</span><br><span>@@ -1420,6 +1393,7 @@</span><br><span> class->format = ao2_bump(ast_format_slin);</span><br><span> class->srcfd = -1;</span><br><span> class->kill_delay = 100000;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_INIT(&class->files, 0);</span><br><span> }</span><br><span> </span><br><span> return class;</span><br><span>@@ -1614,7 +1588,7 @@</span><br><span> }</span><br><span> </span><br><span> if (!state || !state->class || strcmp(mohclass->name, state->class->name)) {</span><br><span style="color: hsl(0, 100%, 40%);">- if (mohclass->total_files) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (AST_VECTOR_SIZE(&mohclass->files)) {</span><br><span> res = ast_activate_generator(chan, &moh_file_stream, mohclass);</span><br><span> } else {</span><br><span> res = ast_activate_generator(chan, &mohgen, mohclass);</span><br><span>@@ -1693,14 +1667,8 @@</span><br><span> class->srcfd = -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (class->filearray) {</span><br><span style="color: hsl(0, 100%, 40%);">- int i;</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < class->total_files; i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(class->filearray[i]);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free(class->filearray);</span><br><span style="color: hsl(0, 100%, 40%);">- class->filearray = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_RESET(&class->files, ast_free);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_VECTOR_FREE(&class->files);</span><br><span> </span><br><span> if (class->timer) {</span><br><span> ast_timer_close(class->timer);</span><br><span>@@ -1885,13 +1853,13 @@</span><br><span> for (; (class = ao2_t_iterator_next(&i, "Show files iterator")); mohclass_unref(class, "Unref iterator in moh show files")) {</span><br><span> int x;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (!class->total_files) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!AST_VECTOR_SIZE(&class->files)) {</span><br><span> continue;</span><br><span> }</span><br><span> </span><br><span> ast_cli(a->fd, "Class: %s\n", class->name);</span><br><span style="color: hsl(0, 100%, 40%);">- for (x = 0; x < class->total_files; x++) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "\tFile: %s\n", class->filearray[x]);</span><br><span style="color: hsl(120, 100%, 40%);">+ for (x = 0; x < AST_VECTOR_SIZE(&class->files); x++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\tFile: %s\n", AST_VECTOR_GET(&class->files, x));</span><br><span> }</span><br><span> }</span><br><span> ao2_iterator_destroy(&i);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/11655">change 11655</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/11655"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 17 </div>
<div style="display:none"> Gerrit-Change-Id: Ic476a56608b1820ca93dcf68d10cd76fc0b94141 </div>
<div style="display:none"> Gerrit-Change-Number: 11655 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean.bright@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>