Skip to content

Commit 57d4660

Browse files
carenasgitster
authored andcommitted
grep: make PCRE1 aware of custom allocator
63e7e9d ("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] https://public-inbox.org/git/[email protected] [2] https://public-inbox.org/git/[email protected] Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51cf315 commit 57d4660

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

grep.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,20 @@ int grep_config(const char *var, const char *value, void *cb)
150150
* Initialize one instance of grep_opt and copy the
151151
* default values from the template we read the configuration
152152
* information in an earlier call to git_config(grep_config).
153+
*
154+
* If using PCRE, make sure that the library is configured
155+
* to use the same allocator as Git (e.g. nedmalloc on Windows).
153156
*/
154157
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
155158
{
156159
struct grep_opt *def = &grep_defaults;
157160
int i;
158161

162+
#ifdef USE_LIBPCRE1
163+
pcre_malloc = malloc;
164+
pcre_free = free;
165+
#endif
166+
159167
memset(opt, 0, sizeof(*opt));
160168
opt->repo = repo;
161169
opt->prefix = prefix;

0 commit comments

Comments
 (0)