[Bf-blender-cvs] [f8f8282] asset-experiments: Refactor: do not filter each item twice! Mem usage here is not really an issue anyway.

Bastien Montagne noreply at git.blender.org
Thu Dec 4 18:49:49 CET 2014


Commit: f8f82821ef7826aba8c02eebd74d6d83c2144c77
Author: Bastien Montagne
Date:   Thu Dec 4 17:45:55 2014 +0100
Branches: asset-experiments
https://developer.blender.org/rBf8f82821ef7826aba8c02eebd74d6d83c2144c77

Refactor: do not filter each item twice! Mem usage here is not really an issue anyway.

===================================================================

M	source/blender/editors/space_file/filelist.c

===================================================================

diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index 989c261..f9dcbe0 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -498,32 +498,33 @@ static bool is_filtered_main(struct direntry *file, const char *UNUSED(dir), Fil
 void filelist_filter(FileList *filelist)
 {
 	int num_filtered = 0;
-	int i, j;
+	int *fidx_tmp;
+	int i;
 
-	if (!filelist->filelist)
+	if (!filelist->filelist) {
 		return;
+	}
+
+	fidx_tmp = MEM_mallocN(sizeof(*fidx_tmp) * (size_t)filelist->numfiles, __func__);
 
 	/* How many files are left after filter ? */
 	for (i = 0; i < filelist->numfiles; ++i) {
 		struct direntry *file = &filelist->filelist[i];
 		if (filelist->filterf(file, filelist->dir, &filelist->filter_data)) {
-			num_filtered++;
+			fidx_tmp[num_filtered++] = i;
 		}
 	}
-	
+
 	if (filelist->fidx) {
 		MEM_freeN(filelist->fidx);
 		filelist->fidx = NULL;
 	}
-	filelist->fidx = (int *)MEM_callocN(num_filtered * sizeof(int), "filteridx");
+	/* Note: maybe we could even accept filelist->fidx to be filelist->numfiles -len allocated? */
+	filelist->fidx = (int *)MEM_mallocN(sizeof(*filelist->fidx) * (size_t)num_filtered, __func__);
+	memcpy(filelist->fidx, fidx_tmp, sizeof(*filelist->fidx) * (size_t)num_filtered);
 	filelist->numfiltered = num_filtered;
 
-	for (i = 0, j = 0; i < filelist->numfiles; ++i) {
-		struct direntry *file = &filelist->filelist[i];
-		if (filelist->filterf(file, filelist->dir, &filelist->filter_data)) {
-			filelist->fidx[j++] = i;
-		}
-	}
+	MEM_freeN(fidx_tmp);
 }
 
 void filelist_init_icons(void)




More information about the Bf-blender-cvs mailing list