Message ID | 20210204210556.25242-11-avarab@gmail.com (mailing list archive) |
---|---|
State | Accepted |
Commit | c1760352e0b27cfbdffd97dec50a9eb552318993 |
Headers | show |
Series | grep/pcre2: memory allocation fixes | expand |
Hi Ævar, On Thu, 4 Feb 2021, Ævar Arnfjörð Bjarmason wrote: > Move the definitions of the pcre2_{malloc,free} functions above the > compile_pcre2_pattern() function they're used it. Before the preceding s/it/in/ Thank you for this entire patch series. I like its intention and its execution. Ciao, Dscho > commit they used to be needed earlier, but now we can move them to be > adjacent to the other PCREv2 functions. > > Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> > --- > grep.c | 46 ++++++++++++++++++++++------------------------ > 1 file changed, 22 insertions(+), 24 deletions(-) > > diff --git a/grep.c b/grep.c > index 2599f329cd..636ac48bf0 100644 > --- a/grep.c > +++ b/grep.c > @@ -40,30 +40,6 @@ static struct grep_opt grep_defaults = { > .output = std_output, > }; > > -#ifdef USE_LIBPCRE2 > -#define GREP_PCRE2_DEBUG_MALLOC 0 > - > -static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) > -{ > - void *pointer = malloc(size); > -#if GREP_PCRE2_DEBUG_MALLOC > - static int count = 1; > - fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size); > -#endif > - return pointer; > -} > - > -static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) > -{ > -#if GREP_PCRE2_DEBUG_MALLOC > - static int count = 1; > - if (pointer) > - fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++); > -#endif > - free(pointer); > -} > -#endif > - > static const char *color_grep_slots[] = { > [GREP_COLOR_CONTEXT] = "context", > [GREP_COLOR_FILENAME] = "filename", > @@ -355,6 +331,28 @@ static int is_fixed(const char *s, size_t len) > } > > #ifdef USE_LIBPCRE2 > +#define GREP_PCRE2_DEBUG_MALLOC 0 > + > +static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) > +{ > + void *pointer = malloc(size); > +#if GREP_PCRE2_DEBUG_MALLOC > + static int count = 1; > + fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size); > +#endif > + return pointer; > +} > + > +static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) > +{ > +#if GREP_PCRE2_DEBUG_MALLOC > + static int count = 1; > + if (pointer) > + fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++); > +#endif > + free(pointer); > +} > + > static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) > { > int error; > -- > 2.30.0.284.gd98b1dd5eaa7 > >
diff --git a/grep.c b/grep.c index 2599f329cd..636ac48bf0 100644 --- a/grep.c +++ b/grep.c @@ -40,30 +40,6 @@ static struct grep_opt grep_defaults = { .output = std_output, }; -#ifdef USE_LIBPCRE2 -#define GREP_PCRE2_DEBUG_MALLOC 0 - -static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) -{ - void *pointer = malloc(size); -#if GREP_PCRE2_DEBUG_MALLOC - static int count = 1; - fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size); -#endif - return pointer; -} - -static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) -{ -#if GREP_PCRE2_DEBUG_MALLOC - static int count = 1; - if (pointer) - fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++); -#endif - free(pointer); -} -#endif - static const char *color_grep_slots[] = { [GREP_COLOR_CONTEXT] = "context", [GREP_COLOR_FILENAME] = "filename", @@ -355,6 +331,28 @@ static int is_fixed(const char *s, size_t len) } #ifdef USE_LIBPCRE2 +#define GREP_PCRE2_DEBUG_MALLOC 0 + +static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) +{ + void *pointer = malloc(size); +#if GREP_PCRE2_DEBUG_MALLOC + static int count = 1; + fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size); +#endif + return pointer; +} + +static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) +{ +#if GREP_PCRE2_DEBUG_MALLOC + static int count = 1; + if (pointer) + fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++); +#endif + free(pointer); +} + static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) { int error;
Move the definitions of the pcre2_{malloc,free} functions above the compile_pcre2_pattern() function they're used it. Before the preceding commit they used to be needed earlier, but now we can move them to be adjacent to the other PCREv2 functions. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> --- grep.c | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-)