Skip to content

Commit 270be81

Browse files
aspiersgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c04318e commit 270be81

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Documentation/technical/api-directory-listing.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ marked. If you to exclude files, make sure you have loaded index first.
8181

8282
* Use `dir.entries[]`.
8383

84+
* Call `free_directory()` when none of the contained elements are no longer in use.
85+
8486
(JC)

dir.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,3 +1557,33 @@ void free_pathspec(struct pathspec *pathspec)
15571557
free(pathspec->items);
15581558
pathspec->items = NULL;
15591559
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ extern void parse_exclude_pattern(const char **string, int *patternlen, int *fla
169169
extern void add_exclude(const char *string, const char *base,
170170
int baselen, struct exclude_list *el, int srcpos);
171171
extern void clear_exclude_list(struct exclude_list *el);
172+
extern void clear_directory(struct dir_struct *dir);
172173
extern int file_exists(const char *);
173174

174175
extern int is_inside_dir(const char *dir);

0 commit comments

Comments
 (0)