From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Pierre Habouzit <madcoder@debian.org>
Subject: [PATCH 1/2] Introduce leaky().
Date: Tue, 24 Jun 2008 22:14:33 +0200 [thread overview]
Message-ID: <1214338474-16822-2-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <1214338474-16822-1-git-send-email-madcoder@debian.org>
This can be used to mark allocated memory as a "leak". This can be used to
collect memory at the exit of the command, so that tools like valgrind can
be used to check for actual memory leak without noise.
COLLECT_LEAKS_AT_EXIT must be set to that purpose, else 'leaky' is the
transparent macro.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
Makefile | 5 +++++
alloc.c | 20 ++++++++++++++++++++
cache.h | 5 +++++
3 files changed, 30 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
index 9c16482..6be15c8 100644
--- a/Makefile
+++ b/Makefile
@@ -151,6 +151,8 @@ all::
# Define NO_EXTERNAL_GREP if you don't want "git grep" to ever call
# your external grep (e.g., if your system lacks grep, if its grep is
# broken, or spawning external process is slower than built-in grep git has).
+#
+# Define COLLECT_LEAKS_AT_EXIT if you want memory marked as leaky() at exit.
GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -947,6 +949,9 @@ endif
ifdef NO_EXTERNAL_GREP
BASIC_CFLAGS += -DNO_EXTERNAL_GREP
endif
+ifdef COLLECT_LEAKS_AT_EXIT
+ BASIC_CFLAGS += -DCOLLECT_LEAKS_AT_EXIT
+endif
ifeq ($(TCLTK_PATH),)
NO_TCLTK=NoThanks
diff --git a/alloc.c b/alloc.c
index 216c23a..1afe810 100644
--- a/alloc.c
+++ b/alloc.c
@@ -74,3 +74,23 @@ void alloc_report(void)
REPORT(commit);
REPORT(tag);
}
+
+#ifdef COLLECT_LEAKS_AT_EXIT
+static void **leaks;
+int leaknb, leaksz;
+
+static void release_leaks(void)
+{
+ while (leaknb-- > 0)
+ free(*leaks++);
+ free(leaks);
+}
+
+void *leaky(void *ptr)
+{
+ if (leaksz == 0)
+ atexit(&release_leaks);
+ ALLOC_GROW(leaks, leaknb + 1, leaksz);
+ return leaks[leaknb++] = ptr;
+}
+#endif
diff --git a/cache.h b/cache.h
index 101ead5..33603bb 100644
--- a/cache.h
+++ b/cache.h
@@ -777,6 +777,11 @@ int decode_85(char *dst, const char *line, int linelen);
void encode_85(char *buf, const unsigned char *data, int bytes);
/* alloc.c */
+#ifdef COLLECT_LEAKS_AT_EXIT
+extern void *leaky(void *);
+#else
+# define leaky(x) x
+#endif
extern void *alloc_blob_node(void);
extern void *alloc_tree_node(void);
extern void *alloc_commit_node(void);
--
1.5.6.120.g3adb8.dirty
next prev parent reply other threads:[~2008-06-24 20:15 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-24 20:14 [RFC] leaky() Pierre Habouzit
2008-06-24 20:14 ` Pierre Habouzit [this message]
2008-06-24 20:14 ` [PATCH 2/2] git-rev-parse: use leaky() Pierre Habouzit
2008-06-24 20:16 ` [PATCH 1/2] Introduce leaky() Pierre Habouzit
2008-06-24 21:28 ` Jakub Narebski
2008-06-24 22:10 ` Pierre Habouzit
2008-06-26 18:46 ` Junio C Hamano
2008-06-26 21:33 ` Pierre Habouzit
2008-07-22 18:09 ` Jan Hudec
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1214338474-16822-2-git-send-email-madcoder@debian.org \
--to=madcoder@debian.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).