dir.c: provide clear_directory() for reclaiming dir_struct memory
By the end of a directory traversal, a dir_struct instance will typically contains pointers to various data structures on the heap. clear_directory() provides a convenient way to reclaim that memory. Signed-off-by: Adam Spiers <git@adamspiers.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adam Spiers committed
Jan 6, 2013 at 16:58 UTC
270be8160493eb25ee43eb7db8dda2504343dc65
3 files changed
+33
Documentation/technical/api-directory-listing.txt
+2
@@ -81,4 +81,6 @@ marked. If you to exclude files, make sure you have loaded index first.
81
82
* Use `dir.entries[]`.
83
84
+* Call `free_directory()` when none of the contained elements are no longer in use.
85
+
86
(JC)
dir.c
+30
@@ -1557,3 +1557,33 @@ void free_pathspec(struct pathspec *pathspec)
1557
free(pathspec->items);
1558
pathspec->items = NULL;
1559
}
1560
+
1561
+/*
1562
+ * Frees memory within dir which was allocated for exclude lists and
1563
+ * the exclude_stack. Does not free dir itself.
1564
+ */
1565
+void clear_directory(struct dir_struct *dir)
1566
+{
1567
+ int i, j;
1568
+ struct exclude_list_group *group;
1569
+ struct exclude_list *el;
1570
+ struct exclude_stack *stk;
1571
+
1572
+ for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1573
+ group = &dir->exclude_list_group[i];
1574
+ for (j = 0; j < group->nr; j++) {
1575
+ el = &group->el[j];
1576
+ if (i == EXC_DIRS)
1577
+ free((char *)el->src);
1578
+ clear_exclude_list(el);
1579
+ }
1580
+ free(group->el);
1581
+ }
1582
+
1583
+ stk = dir->exclude_stack;
1584
+ while (stk) {
1585
+ struct exclude_stack *prev = stk->prev;
1586
+ free(stk);
1587
+ stk = prev;
1588
+ }
1589
+}
dir.h
+1
@@ -169,6 +169,7 @@ extern void parse_exclude_pattern(const char **string, int *patternlen, int *fla
169
extern void add_exclude(const char *string, const char *base,
170
int baselen, struct exclude_list *el, int srcpos);
171
extern void clear_exclude_list(struct exclude_list *el);
172
+extern void clear_directory(struct dir_struct *dir);
173
extern int file_exists(const char *);
174
175
extern int is_inside_dir(const char *dir);