diff --git a/apps/playlist.c b/apps/playlist.c index 0b5662b47c..2bdc1f39cc 100644 --- a/apps/playlist.c +++ b/apps/playlist.c @@ -2887,11 +2887,8 @@ int playlist_create_ex(struct playlist_info* playlist, if (index_buffer) { - size_t unit_size = sizeof (*playlist->indices); -#ifdef HAVE_DIRCACHE - unit_size += sizeof (*playlist->dcfrefs); -#endif - int num_indices = index_buffer_size / unit_size; + int num_indices = index_buffer_size / + playlist_get_required_bufsz(playlist, false, 1); if (num_indices > global_settings.max_files_in_playlist) num_indices = global_settings.max_files_in_playlist; @@ -3525,6 +3522,26 @@ char *playlist_get_name(const struct playlist_info* playlist, char *buf, return buf; } +/* return size of buffer needed for playlist to initialize num_indices entries */ +size_t playlist_get_required_bufsz(struct playlist_info* playlist, + bool include_namebuf, + int num_indices) +{ + size_t namebuf = 0; + + if (!playlist) + playlist = ¤t_playlist; + + size_t unit_size = sizeof (*playlist->indices); + #ifdef HAVE_DIRCACHE + unit_size += sizeof (*playlist->dcfrefs); + #endif + if (include_namebuf) + namebuf = AVERAGE_FILENAME_LENGTH * global_settings.max_files_in_dir; + + return (num_indices * unit_size) + namebuf; +} + /* Fills info structure with information about track at specified index. Returns 0 on success and -1 on failure */ int playlist_get_track_info(struct playlist_info* playlist, int index, diff --git a/apps/playlist.h b/apps/playlist.h index 6048001ff7..220a577fb2 100644 --- a/apps/playlist.h +++ b/apps/playlist.h @@ -181,6 +181,8 @@ char *playlist_name(const struct playlist_info* playlist, char *buf, int buf_size); char *playlist_get_name(const struct playlist_info* playlist, char *buf, int buf_size); +size_t playlist_get_required_bufsz(struct playlist_info* playlist, + bool include_namebuf, int num_indices); int playlist_get_track_info(struct playlist_info* playlist, int index, struct playlist_track_info* info); int playlist_save(struct playlist_info* playlist, char *filename, diff --git a/apps/playlist_viewer.c b/apps/playlist_viewer.c index 54451992a7..751f1e21a7 100644 --- a/apps/playlist_viewer.c +++ b/apps/playlist_viewer.c @@ -362,9 +362,14 @@ static bool playlist_viewer_init(struct playlist_viewer * viewer, if (is_playing) { - /* Something is playing, use half the plugin buffer for playlist - indices */ - index_buffer_size = buffer_size / 2; + /* Something is playing, try to accommodate + * global_settings.max_files_in_playlist entries */ + index_buffer_size = playlist_get_required_bufsz(viewer->playlist, + false, global_settings.max_files_in_playlist); + + if ((unsigned)index_buffer_size >= buffer_size - MAX_PATH) + index_buffer_size = buffer_size - (MAX_PATH + 1); + index_buffer = buffer; }