diff options
authorCarlo Marcelo Arenas Belón <>2019-10-16 12:10:22 (GMT)
committerJunio C Hamano <>2019-10-18 01:33:16 (GMT)
commit57d4660468ef2674bb6afc2c5814d4b397a5eda9 (patch)
parent51cf315870bbb7254ddf06c84fe03b41bc48eebd (diff)
grep: make PCRE1 aware of custom allocator
63e7e9d8b6 ("git-grep: Learn PCRE", 2011-05-09) didn't include a way to override the system alocator, and so it is incompatible with USE_NED_ALLOCATOR as reported by Dscho[1] (in similar code from PCRE2) Note that nedmalloc, as well as other custom allocators like jemalloc and mi-malloc, can be configured at runtime (via `LD_PRELOAD`), therefore we cannot know at compile time whether a custom allocator is used or not. Make the minimum change possible to ensure this combination is supported by extending `grep_init()` to set the PCRE1 specific functions to Git's idea of `malloc()` and `free()` and therefore making sure all allocations are done inside PCRE1 with the same allocator than the rest of Git. This change has negligible performance impact: PCRE needs to allocate memory once per program run for the character table and for each pattern compilation. These are both rare events compared to matching patterns against lines. Actual measurements[2] show that the impact is lost in the noise. [1] [2] Signed-off-by: Carlo Marcelo Arenas Belón <> Signed-off-by: Johannes Schindelin <> Signed-off-by: Junio C Hamano <>
1 files changed, 8 insertions, 0 deletions
diff --git a/grep.c b/grep.c
index cd952ef..db6e0d8 100644
--- a/grep.c
+++ b/grep.c
@@ -150,12 +150,20 @@ int grep_config(const char *var, const char *value, void *cb)
* Initialize one instance of grep_opt and copy the
* default values from the template we read the configuration
* information in an earlier call to git_config(grep_config).
+ *
+ * If using PCRE, make sure that the library is configured
+ * to use the same allocator as Git (e.g. nedmalloc on Windows).
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
struct grep_opt *def = &grep_defaults;
int i;
+#ifdef USE_LIBPCRE1
+ pcre_malloc = malloc;
+ pcre_free = free;
memset(opt, 0, sizeof(*opt));
opt->repo = repo;
opt->prefix = prefix;