From 7f1357495e2ec9c1432b0f09bb10eeb238664a0f Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 3 Mar 2024 22:09:16 +0100 Subject: replace malloc with memory_bucket, free prev result --- src/main_windows.cpp | 10 +++++----- src/memory_bucket.cpp | 4 ++-- src/search.cpp | 22 ++++++++++++++++++---- src/search.h | 2 ++ 4 files changed, 27 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main_windows.cpp b/src/main_windows.cpp index cbc7265..229a055 100644 --- a/src/main_windows.cpp +++ b/src/main_windows.cpp @@ -261,7 +261,7 @@ ts_file_content ts_platform_read_file(char *path, const char *mode) void ts_platform_list_files_block(ts_search_result* result, wchar_t* start_dir) { // Utf8 to wchar str - wchar_t* search_dir = (wchar_t*)malloc(MAX_INPUT_LENGTH); + wchar_t* search_dir = (wchar_t*)ts_memory_bucket_reserve(&result->memory, MAX_INPUT_LENGTH); if (start_dir == nullptr) { MultiByteToWideChar(CP_UTF8, 0, result->directory_to_search, -1, search_dir, MAX_INPUT_LENGTH); } @@ -270,7 +270,7 @@ void ts_platform_list_files_block(ts_search_result* result, wchar_t* start_dir) } // Append wildcard - wchar_t* search_dir_fix = (wchar_t*)malloc(MAX_INPUT_LENGTH); + wchar_t* search_dir_fix = (wchar_t*)ts_memory_bucket_reserve(&result->memory, MAX_INPUT_LENGTH); wcscpy_s(search_dir_fix, MAX_INPUT_LENGTH, search_dir); wcscat_s(search_dir_fix, MAX_INPUT_LENGTH, L"\\*"); @@ -299,7 +299,7 @@ void ts_platform_list_files_block(ts_search_result* result, wchar_t* start_dir) if ((wcscmp(name, L".") == 0) || (wcscmp(name, L"..") == 0)) continue; - wchar_t* subdir_buffer_path = (wchar_t*)malloc(MAX_INPUT_LENGTH); + wchar_t* subdir_buffer_path = (wchar_t*)ts_memory_bucket_reserve(&result->memory, MAX_INPUT_LENGTH); wcscpy(subdir_buffer_path, search_dir); wcscat(subdir_buffer_path, L"\\"); wcscat(subdir_buffer_path, name); @@ -325,8 +325,8 @@ void ts_platform_list_files_block(ts_search_result* result, wchar_t* start_dir) wcscat(complete_file_path, L"\\"); wcscat(complete_file_path, name); - ts_found_file* f = (ts_found_file*)malloc(MAX_INPUT_LENGTH); - f->path = (utf8_int8_t*)malloc(MAX_INPUT_LENGTH); + ts_found_file* f = (ts_found_file*)ts_memory_bucket_reserve(&result->memory, sizeof(ts_found_file)); + f->path = (utf8_int8_t*)ts_memory_bucket_reserve(&result->memory, MAX_INPUT_LENGTH); f->match_count = 0; WideCharToMultiByte(CP_UTF8,0,complete_file_path,-1,(LPSTR)f->path,MAX_INPUT_LENGTH, NULL, NULL); diff --git a/src/memory_bucket.cpp b/src/memory_bucket.cpp index f6f215a..1d8b638 100644 --- a/src/memory_bucket.cpp +++ b/src/memory_bucket.cpp @@ -42,7 +42,7 @@ void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, int reserve_length) return new_bucket.data; } -inline void ts_memory_bucket_reset(ts_memory_bucket *bucket) +void ts_memory_bucket_reset(ts_memory_bucket *bucket) { ts_mutex_lock(&bucket->bucket_mutex); for (int i = 0; i < bucket->buckets.length; i++) @@ -53,7 +53,7 @@ inline void ts_memory_bucket_reset(ts_memory_bucket *bucket) ts_mutex_unlock(&bucket->bucket_mutex); } -inline void ts_memory_bucket_destroy(ts_memory_bucket *bucket) +void ts_memory_bucket_destroy(ts_memory_bucket *bucket) { ts_mutex_lock(&bucket->bucket_mutex); for (int i = 0; i < bucket->buckets.length; i++) diff --git a/src/search.cpp b/src/search.cpp index e90e433..5be2b33 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -96,6 +96,8 @@ ts_search_result *ts_create_empty_search_result() new_result_buffer->file_count = 0; new_result_buffer->cancel_search = false; new_result_buffer->max_file_size = megabytes(1000); + new_result_buffer->memory = ts_memory_bucket_init(megabytes(1)); + new_result_buffer->prev_result = current_search_result; new_result_buffer->files = ts_array_create(sizeof(ts_found_file)); new_result_buffer->files.reserve_jump = FILE_RESERVE_COUNT; @@ -106,8 +108,8 @@ ts_search_result *ts_create_empty_search_result() ts_array_reserve(&new_result_buffer->matches, FILE_RESERVE_COUNT); // filter buffers - new_result_buffer->directory_to_search = (char *)malloc(MAX_INPUT_LENGTH); - new_result_buffer->search_text = (char *)malloc(MAX_INPUT_LENGTH); + new_result_buffer->directory_to_search = (char *)ts_memory_bucket_reserve(&new_result_buffer->memory, MAX_INPUT_LENGTH); + new_result_buffer->search_text = (char *)ts_memory_bucket_reserve(&new_result_buffer->memory, MAX_INPUT_LENGTH); return new_result_buffer; } @@ -260,7 +262,7 @@ static void _ts_search_file(ts_found_file *ref, ts_file_content content, ts_sear file_match.line_nr = m->line_nr; file_match.word_match_offset = m->word_offset; file_match.word_match_length = m->word_match_len; - file_match.line_info = (char *)malloc(MAX_INPUT_LENGTH); + file_match.line_info = (char *)ts_memory_bucket_reserve(&result->memory, MAX_INPUT_LENGTH); int text_pad_lr = 25; if (file_match.word_match_offset > text_pad_lr) @@ -335,12 +337,24 @@ static void *_ts_list_files_thread(void *args) ts_platform_list_files_block(info, nullptr); info->done_finding_files = true; - // Use this thread to cleanup. + // Use this thread to cleanup previous result. + if (info->prev_result) { + while (!info->prev_result->search_completed) { + ts_thread_sleep(10); + } + ts_memory_bucket_destroy(&info->prev_result->memory); + free(info->prev_result); + info->prev_result = nullptr; + } + + // Use this thread to sync. while (!info->search_completed) { if (info->completed_match_threads == info->max_ts_thread_count) { info->search_completed = true; // No memory is written after this point. } + ts_thread_sleep(10); } + return 0; } diff --git a/src/search.h b/src/search.h index f8ddc6c..e9464b5 100644 --- a/src/search.h +++ b/src/search.h @@ -24,6 +24,8 @@ typedef struct t_ts_search_result ts_array filters; int match_count; int file_count; + ts_memory_bucket memory; + struct t_ts_search_result* prev_result; // thread syncing ts_mutex mutex; -- cgit v1.2.3-70-g09d2