From ac6d0e8408fc7a41820752013579796cf5c8e856 Mon Sep 17 00:00:00 2001 From: nova Date: Thu, 9 Oct 2025 00:13:25 +0200 Subject: [PATCH] avoiding useless memory read/writes & fixing race condition in btm_status --- threading.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/threading.c b/threading.c index 565dd78..1663394 100644 --- a/threading.c +++ b/threading.c @@ -48,6 +48,8 @@ volatile unsigned long selected_file_last = 0; extern unsigned int terminal_width; extern unsigned int status; +unsigned int btm_status; + void *thread_mid(){ @@ -93,6 +95,7 @@ void *thread_mid(){ pthread_mutex_unlock(&mutex_selection); } + btm_status = local_status; pthread_cond_signal(&cond_rgt); pthread_cond_signal(&cond_btm); @@ -233,7 +236,8 @@ void *thread_top(){ top_width = sizeof("cannot open directory"); top_buffer = "cannot open directory"; } else { - top_buffer = getcwd(NULL, 0); + top_buffer = malloc(strlen(path)+1); + memcpy(top_buffer, path, strlen(path)+1); top_width = strlen(top_buffer); } @@ -246,21 +250,21 @@ void *thread_btm(){ char *path = malloc(sizeof(char)); char *ui_btm_right_block = malloc(sizeof(char)); unsigned int ui_btm_right_block_size = 0; + unsigned int buffer_width = 0; while(!(status & STATUS_QUIT_PROGRAM)){ pthread_mutex_lock(&mutex_btm); pthread_cond_wait(&cond_btm, &mutex_btm); - unsigned int local_status = status; + unsigned int local_status = btm_status; - int buffer_width = terminal_width; if (local_status & (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY)) { /*{{{ parse storage info */ pthread_mutex_lock(&mutex_mid); unsigned long i; float total_dir_size = 0; for(i = 0; i < mid_file_count; i++) { - if (!(mid_content[i].file_type & (FILE_TYPE_DIR))) { + if ((mid_content[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) { total_dir_size += mid_content[i].file_size; } } @@ -290,6 +294,7 @@ void *thread_btm(){ } } else { size_index[0] = 0; + parsed_number[0] = 0; } if (disk_size_free > 1) { @@ -322,10 +327,17 @@ void *thread_btm(){ /*}}}*/ } - free(btm_buffer); - btm_buffer = malloc(buffer_width); - memset(btm_buffer, ' ', buffer_width); + if (buffer_width != terminal_width) { + buffer_width = terminal_width; + free(btm_buffer); + btm_buffer = malloc(buffer_width); + memset(btm_buffer, ' ', buffer_width/2); + } + memset(btm_buffer + (buffer_width/2), ' ', buffer_width/2); btm_buffer[buffer_width] = '\0'; + + memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size); + btm_buffer[0] = (S_ISDIR(mid_content[selected_file_current].permissions)) ? 'd' : '-'; btm_buffer[1] = (mid_content[selected_file_current].permissions & S_IRUSR) ? 'r' : '-'; btm_buffer[2] = (mid_content[selected_file_current].permissions & S_IWUSR) ? 'w' : '-'; @@ -338,7 +350,6 @@ void *thread_btm(){ btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-'; - memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size); pthread_mutex_unlock(&mutex_btm); }