@@ -1213,37 +1213,51 @@ void btrfs_list_free_filter_set(struct btrfs_list_filter_set *filter_set)
free(filter_set);
}
+static int btrfs_list_is_filter_set(struct btrfs_list_filter_set *fset,
+ enum btrfs_list_filter_enum filter)
+{
+ int i;
+ for (i=0; i < fset->nfilters; i++) {
+ if (fset->filters[i].filter_func == all_filter_funcs[filter])
+ return i;
+ }
+ return -1;
+}
+
int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
enum btrfs_list_filter_enum filter, u64 data)
{
struct btrfs_list_filter_set *set = *filter_set;
int size;
+ int nfilter;
BUG_ON(!set);
BUG_ON(filter >= BTRFS_LIST_FILTER_MAX);
BUG_ON(set->nfilters > set->total);
- if (set->nfilters == set->total) {
- size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
- size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
- set = realloc(set, size);
- if (!set) {
- fprintf(stderr, "memory allocation failed\n");
- exit(1);
- }
+ nfilter = btrfs_list_is_filter_set(set, filter);
+ if (nfilter < 0) {
+ if (set->nfilters == set->total) {
+ size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
+ size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
+ set = realloc(set, size);
+ if (!set) {
+ fprintf(stderr, "memory allocation failed\n");
+ exit(1);
+ }
- memset(&set->filters[set->total], 0,
- BTRFS_LIST_NFILTERS_INCREASE *
- sizeof(struct btrfs_list_filter));
- set->total += BTRFS_LIST_NFILTERS_INCREASE;
- *filter_set = set;
+ memset(&set->filters[set->total], 0,
+ BTRFS_LIST_NFILTERS_INCREASE *
+ sizeof(struct btrfs_list_filter));
+ set->total += BTRFS_LIST_NFILTERS_INCREASE;
+ *filter_set = set;
+ }
+ nfilter = set->nfilters;
+ set->nfilters++;
}
- BUG_ON(set->filters[set->nfilters].filter_func);
-
- set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
- set->filters[set->nfilters].data = data;
- set->nfilters++;
+ set->filters[nfilter].filter_func = all_filter_funcs[filter];
+ set->filters[nfilter].data = data;
return 0;
}