git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 03/11] dir.c: provide clear_directory() for reclaiming dir_struct memory

From
Adam Spiers <git@adamspiers.org>
Date
Jan 6, 2013, 16:58 UTC
Message-ID
<1357491493-11619-4-git-send-email-git@adamspiers.org>
In-Reply-To
<1357491493-11619-1-git-send-email-git@adamspiers.org>

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>
---
 Documentation/technical/api-directory-listing.txt |  2 ++
 dir.c                                             | 30 +++++++++++++++++++++++
 dir.h                                             |  1 +
 3 files changed, 33 insertions(+)
diff --git a/Documentation/technical/api-directory-listing.txt b/Documentation/technical/api-directory-listing.txt
index fa9c8ae..fbceb62 100644
--- a/Documentation/technical/api-directory-listing.txt
+++ b/Documentation/technical/api-directory-listing.txt
@@ -81,4 +81,6 @@ marked. If you to exclude files, make sure you have loaded index first.
 
 * Use `dir.entries[]`.
 
+* Call `free_directory()` when none of the contained elements are no longer in use.
+
 (JC)
diff --git a/dir.c b/dir.c
index d3f462b..46f362e 100644
--- a/dir.c
+++ b/dir.c
@@ -1557,3 +1557,33 @@ void free_pathspec(struct pathspec *pathspec)
 	free(pathspec->items);
 	pathspec->items = NULL;
 }
+
+/*
+ * Frees memory within dir which was allocated for exclude lists and
+ * the exclude_stack.  Does not free dir itself.
+ */
+void clear_directory(struct dir_struct *dir)
+{
+	int i, j;
+	struct exclude_list_group *group;
+	struct exclude_list *el;
+	struct exclude_stack *stk;
+
+	for (i = EXC_CMDL; i <= EXC_FILE; i++) {
+		group = &dir->exclude_list_group[i];
+		for (j = 0; j < group->nr; j++) {
+			el = &group->el[j];
+			if (i == EXC_DIRS)
+				free((char *)el->src);
+			clear_exclude_list(el);
+		}
+		free(group->el);
+	}
+
+	stk = dir->exclude_stack;
+	while (stk) {
+		struct exclude_stack *prev = stk->prev;
+		free(stk);
+		stk = prev;
+	}
+}
diff --git a/dir.h b/dir.h
index 64c410e..dd42a3a 100644
--- a/dir.h
+++ b/dir.h
@@ -169,6 +169,7 @@ extern void parse_exclude_pattern(const char **string, int *patternlen, int *fla
 extern void add_exclude(const char *string, const char *base,
 			int baselen, struct exclude_list *el, int srcpos);
 extern void clear_exclude_list(struct exclude_list *el);
+extern void clear_directory(struct dir_struct *dir);
 extern int file_exists(const char *);
 
 extern int is_inside_dir(const char *dir);
-- 
1.7.11.7.33.gb8feba5
Previous: Adam SpiersNext: Adam Spiers
Message 8 of 16 in “What's cooking in git.git (Jan 2013, #02; Thu, 3)”
  1. Junio C HamanoJan 3, 2013
  2. Adam SpiersJan 4, 2013
  3. Junio C HamanoJan 4, 2013
  4. Adam SpiersJan 6, 2013
  5. 00/11 new git check-ignore sub-commandAdam Spiers, Jan 6, 2013
  6. 01/11 dir.c: use a single struct exclude_list per source of excludesAdam Spiers, Jan 6, 2013
  7. 02/11 dir.c: keep track of where patterns came fromAdam Spiers, Jan 6, 2013
  8. 03/11 dir.c: provide clear_directory() for reclaiming dir_struct memoryAdam Spiers, Jan 6, 2013
  9. 04/11 dir.c: improve docs for match_pathspec() and match_pathspec_depth()Adam Spiers, Jan 6, 2013
  10. 05/11 add.c: remove unused argument from validate_pathspec()Adam Spiers, Jan 6, 2013
  11. 06/11 add.c: move pathspec matchers into new pathspec.c for reuseAdam Spiers, Jan 6, 2013
  12. 07/11 pathspec.c: rename newly public functions for clarityAdam Spiers, Jan 6, 2013
  13. 08/11 add.c: extract check_path_for_gitlink() from treat_gitlinks() for reuseAdam Spiers, Jan 6, 2013
  14. 09/11 add.c: extract new die_if_path_beyond_symlink() for reuseAdam Spiers, Jan 6, 2013
  15. 10/11 setup.c: document get_pathspec()Adam Spiers, Jan 6, 2013
  16. 11/11 add git-check-ignore sub-commandAdam Spiers, Jan 6, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.