From 2d9cb673e58a0b3cc2a94884983ac689ed68ebee Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Thu, 12 Jan 2023 08:09:47 -0500 Subject: [PATCH] add chunk_alloc to playlist.c #2 dc_thread_playlist was asking for invalid indices since previously the name buffer would have been valid it just got whatever junk data was left over add dc_discard_playlist_pointers for HAVE_DIRCACHE targets this allows the dc_playlist_thread to stop its current lookup loop Change-Id: I6f25b97b8c4e314d27c5e1e6ff0925b5a3e93f26 --- apps/playlist.c | 84 ++++++++++++++++++++++++++++--------------------- apps/playlist.h | 9 +++--- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/apps/playlist.c b/apps/playlist.c index f3f084b702..25bd60dd2a 100644 --- a/apps/playlist.c +++ b/apps/playlist.c @@ -520,10 +520,8 @@ static void empty_playlist_unlocked(struct playlist_info* playlist, bool resume) playlist->filename[0] = '\0'; - if (playlist->buffer) - playlist->buffer[0] = 0; + chunk_alloc_free(&playlist->name_chunk_buffer); - playlist->buffer_end_pos = 0; playlist->seed = 0; playlist->num_cached = 0; @@ -1196,12 +1194,18 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see { max = dircache_get_fileref_path(&playlist->dcfrefs[index], tmp_buf, sizeof(tmp_buf)); + + NOTEF("%s [in DCache]: 0x%x %s", __func__, + playlist->dcfrefs[index], tmp_buf); } #endif /* HAVE_DIRCACHE */ if (playlist->in_ram && !control_file && max < 0) { - strmemccpy(tmp_buf, (char*)&playlist->buffer[seek], sizeof(tmp_buf)); + char *namebuf = chunk_get_data(&playlist->name_chunk_buffer, seek); + strmemccpy(tmp_buf, namebuf, sizeof(tmp_buf)); + chunk_put_data(&playlist->name_chunk_buffer, seek); + NOTEF("%s [in Ram]: 0x%x %s", __func__, seek, tmp_buf); } else if (max < 0) { @@ -1239,6 +1243,8 @@ static int get_track_filename(struct playlist_info* playlist, int index, int see if (!utf8) max = convert_m3u_name(tmp_buf, max, sizeof(tmp_buf), dir_buf); + + NOTEF("%s [in File]: 0x%x %s", __func__, seek, tmp_buf); } } } @@ -1989,6 +1995,25 @@ static void dc_thread_playlist(void) } #endif +/* + * Allocate name chunk buffer header for in-ram playlists + */ +static void alloc_namebuffer(void) +{ +#if MEMORYSIZE >= 16 +# define NAME_CHUNK_SZ (200 << 10) /*200K*/ +#elif MEMORYSIZE >= 8 +# define NAME_CHUNK_SZ (100 << 10) /*100K*/ +#else +# define NAME_CHUNK_SZ (50 << 10) /*50K*/ +#endif + struct playlist_info* playlist = ¤t_playlist; + size_t namebufsz = (AVERAGE_FILENAME_LENGTH * global_settings.max_files_in_dir); + size_t name_chunks = (namebufsz + NAME_CHUNK_SZ - 1) / NAME_CHUNK_SZ; + core_chunk_alloc_init(&playlist->name_chunk_buffer, NAME_CHUNK_SZ, name_chunks); +#undef NAME_CHUNK_SZ +} + /* * Allocate a temporary buffer for loading playlists */ @@ -2016,8 +2041,6 @@ static int move_callback(int handle, void* current, void* new) struct playlist_info* playlist = ¤t_playlist; if (current == playlist->indices) playlist->indices = new; - else if (current == playlist->buffer) - playlist->buffer = new; #ifdef HAVE_DIRCACHE else if (current == playlist->dcfrefs) playlist->dcfrefs = new; @@ -2056,13 +2079,6 @@ void playlist_init(void) playlist->max_playlist_size * sizeof(*playlist->indices), &ops); playlist->indices = core_get_data(handle); - playlist->buffer_size = - AVERAGE_FILENAME_LENGTH * global_settings.max_files_in_dir; - - handle = core_alloc_ex("playlist buf", - playlist->buffer_size, &ops); - playlist->buffer = core_get_data(handle); - playlist->buffer_handle = handle; initalize_new_playlist(playlist, true); @@ -2099,11 +2115,19 @@ void playlist_shutdown(void) */ int playlist_add(const char *filename) { + size_t indice = CHUNK_ALLOC_INVALID; struct playlist_info* playlist = ¤t_playlist; int len = strlen(filename); - if(len+1 > playlist->buffer_size - playlist->buffer_end_pos) + if (!chunk_alloc_is_initialized(&playlist->name_chunk_buffer)) + alloc_namebuffer(); + + if (chunk_alloc_is_initialized(&playlist->name_chunk_buffer)) + indice = chunk_alloc(&playlist->name_chunk_buffer, len + 1); + + if(indice == CHUNK_ALLOC_INVALID) { + notify_buffer_full(); return -2; } @@ -2116,17 +2140,21 @@ int playlist_add(const char *filename) playlist_mutex_lock(&(playlist->mutex)); - playlist->indices[playlist->amount] = playlist->buffer_end_pos; + char *namebuf = (char*)chunk_get_data(&playlist->name_chunk_buffer, indice); + strcpy(namebuf, filename); + + namebuf += len; + namebuf[0] = '\0'; + + chunk_put_data(&playlist->name_chunk_buffer, indice); + playlist->indices[playlist->amount] = indice; + #ifdef HAVE_DIRCACHE dc_copy_filerefs(&playlist->dcfrefs[playlist->amount], NULL, 1); #endif playlist->amount++; - strcpy((char*)&playlist->buffer[playlist->buffer_end_pos], filename); - playlist->buffer_end_pos += len; - playlist->buffer[playlist->buffer_end_pos++] = '\0'; - playlist_mutex_unlock(&(playlist->mutex)); return 0; @@ -2200,9 +2228,7 @@ int playlist_create_ex(struct playlist_info* playlist, #endif } - playlist->buffer_size = 0; - playlist->buffer_handle = -1; - playlist->buffer = NULL; + chunk_alloc_free(&playlist->name_chunk_buffer); } new_playlist_unlocked(playlist, dir, file); @@ -3701,10 +3727,6 @@ int playlist_save(struct playlist_info* playlist, char *filename, if (strlcat(path, "_temp", sizeof(path)) >= sizeof (path)) return -1; - /* can ignore volatile here, because core_get_data() is called later */ - char* old_buffer = (char*)playlist->buffer; - size_t old_buffer_size = playlist->buffer_size; - if (is_m3u8_name(path)) { fd = open_utf8(path, O_CREAT|O_WRONLY|O_TRUNC); @@ -3718,8 +3740,7 @@ int playlist_save(struct playlist_info* playlist, char *filename, if (fd < 0) { notify_access_error(); - result = -1; - goto reset_old_buffer; + return -1; } display_playlist_count(count, ID2P(LANG_PLAYLIST_SAVE_COUNT), false); @@ -3839,13 +3860,6 @@ int playlist_save(struct playlist_info* playlist, char *filename, cpu_boost(false); -reset_old_buffer: - if (playlist->buffer_handle > 0) - old_buffer = core_get_data(playlist->buffer_handle); - - playlist->buffer = old_buffer; - playlist->buffer_size = old_buffer_size; - return result; } diff --git a/apps/playlist.h b/apps/playlist.h index 1ab330e513..ef943f1bd9 100644 --- a/apps/playlist.h +++ b/apps/playlist.h @@ -28,6 +28,7 @@ #include "kernel.h" #include "metadata.h" #include "rbpaths.h" +#include "chunk_alloc.h" #define PLAYLIST_ATTR_QUEUED 0x01 #define PLAYLIST_ATTR_INSERTED 0x02 @@ -84,10 +85,10 @@ struct playlist_info global_settings.max_files_in_playlist */ int num_inserted_tracks; /* number of tracks inserted */ volatile unsigned long *indices; /* array of indices */ - int buffer_handle; /* handle to the below buffer (-1 if non-buflib) */ - volatile char *buffer;/* buffer for in-ram playlists */ - int buffer_size; /* size of buffer */ - int buffer_end_pos; /* last position where buffer was written */ + + struct chunk_alloc_header name_chunk_buffer; /* chunk buffer for + in-ram playlist */ + int index; /* index of current playing track */ int first_index; /* index of first song in playlist */ int amount; /* number of tracks in the index */