Skip to content

Commit

Permalink
Drop support for using alloca().
Browse files Browse the repository at this point in the history
While `alloca()` provides a minor performance boost on Linux and FreeBSD,
it provides no advantage on platforms with more modern allocators, such
as macOS.  In addition, since it is not universally portable, we are
forced to maintain both code paths, so we don't even get the advantage
of simplicity (from not having to free memory allocated locally).
  • Loading branch information
dag-erling committed Sep 11, 2024
1 parent 0d33c65 commit ad55b97
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 135 deletions.
18 changes: 0 additions & 18 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,6 @@ AC_USE_SYSTEM_EXTENSIONS
dnl Make sure locally configured headers are used (this adds the #define to config.h).
AC_DEFINE(USE_LOCAL_TRE_H, 1, [ Define to ensure locally configured headers are used ])

AC_ARG_WITH(alloca,
AS_HELP_STRING(
[--without-alloca],
[don't use alloca @<:@default=use@:>@]),
[ tre_use_alloca="$withval" ],
[ tre_use_alloca="yes" ])
if test "$tre_use_alloca" = "yes"; then
ALLOCA=""
AC_FUNC_ALLOCA
if test -z "$ALLOCA"; then
# alloca() works.
AC_DEFINE(TRE_USE_ALLOCA, 1,
[ Define if you want TRE to use alloca() instead of malloc() when
allocating memory needed for regexec operations. ])
fi
fi

AC_ARG_ENABLE(system-abi,
AS_HELP_STRING(
[--enable-system-abi],
Expand Down Expand Up @@ -560,7 +543,6 @@ TRE is now configured as follows:
LD = $LD
LDFLAGS = $LDFLAGS
LIBS = $LIBS
Use alloca(): $tre_use_alloca

* TRE options

Expand Down
31 changes: 0 additions & 31 deletions lib/regexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@
#include <config.h>
#endif /* HAVE_CONFIG_H */

#ifdef TRE_USE_ALLOCA
/* AIX requires this to be the first thing in the file. */
#ifndef __GNUC__
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
# endif
# endif
# endif
#endif
#endif /* TRE_USE_ALLOCA */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -142,11 +125,7 @@ tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len,
int *tags = NULL, eo;
if (tnfa->num_tags > 0 && nmatch > 0)
{
#ifdef TRE_USE_ALLOCA
tags = alloca(sizeof(*tags) * tnfa->num_tags);
#else /* !TRE_USE_ALLOCA */
tags = malloc(sizeof(*tags) * tnfa->num_tags);
#endif /* !TRE_USE_ALLOCA */
if (tags == NULL)
return REG_ESPACE;
}
Expand All @@ -162,10 +141,8 @@ tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len,
{
/* The backtracking matcher requires rewind and compare
capabilities from the input stream. */
#ifndef TRE_USE_ALLOCA
if (tags)
free(tags);
#endif /* !TRE_USE_ALLOCA */
return REG_BADPAT;
}
}
Expand Down Expand Up @@ -195,10 +172,8 @@ tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len,
if (status == REG_OK)
/* A match was found, so fill the submatch registers. */
tre_fill_pmatch(nmatch, pmatch, tnfa->cflags, tnfa, tags, eo);
#ifndef TRE_USE_ALLOCA
if (tags)
free(tags);
#endif /* !TRE_USE_ALLOCA */
return status;
}

Expand Down Expand Up @@ -301,22 +276,16 @@ tre_match_approx(const tre_tnfa_t *tnfa, const void *string, size_t len,

if (tnfa->num_tags > 0 && match->nmatch > 0)
{
#if TRE_USE_ALLOCA
tags = alloca(sizeof(*tags) * tnfa->num_tags);
#else /* !TRE_USE_ALLOCA */
tags = malloc(sizeof(*tags) * tnfa->num_tags);
#endif /* !TRE_USE_ALLOCA */
if (tags == NULL)
return REG_ESPACE;
}
status = tre_tnfa_run_approx(tnfa, string, (int)len, type, tags,
match, params, eflags, &eo);
if (status == REG_OK)
tre_fill_pmatch(match->nmatch, match->pmatch, tnfa->cflags, tnfa, tags, eo);
#ifndef TRE_USE_ALLOCA
if (tags)
free(tags);
#endif /* !TRE_USE_ALLOCA */
return status;
}

Expand Down
21 changes: 0 additions & 21 deletions lib/tre-match-approx.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@
#endif /* HAVE_CONFIG_H */

/* AIX requires this to be the first thing in the file. */
#ifdef TRE_USE_ALLOCA
#ifndef __GNUC__
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
# endif
# endif
# endif
#endif
#endif /* TRE_USE_ALLOCA */

#include <assert.h>
#include <stdlib.h>
Expand Down Expand Up @@ -272,11 +257,7 @@ tre_tnfa_run_approx(const tre_tnfa_t *tnfa, const void *string, int len,
total_bytes += (sizeof(long) - 1) * 3;

/* Allocate the memory. */
#ifdef TRE_USE_ALLOCA
buf = alloca(total_bytes);
#else /* !TRE_USE_ALLOCA */
buf = malloc((unsigned)total_bytes);
#endif /* !TRE_USE_ALLOCA */
if (!buf)
return REG_ESPACE;
memset(buf, 0, (size_t)total_bytes);
Expand Down Expand Up @@ -792,10 +773,8 @@ tre_tnfa_run_approx(const tre_tnfa_t *tnfa, const void *string, int len,
DPRINT(("match end offset = %d, match cost = %d\n", match_eo,
match_costs[TRE_M_COST]));

#ifndef TRE_USE_ALLOCA
if (buf)
free(buf);
#endif /* !TRE_USE_ALLOCA */

match->cost = match_costs[TRE_M_COST];
match->num_ins = match_costs[TRE_M_NUM_INS];
Expand Down
32 changes: 0 additions & 32 deletions lib/tre-match-backtrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,6 @@
#include <config.h>
#endif /* HAVE_CONFIG_H */

#ifdef TRE_USE_ALLOCA
/* AIX requires this to be the first thing in the file. */
#ifndef __GNUC__
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
# endif
# endif
# endif
#endif
#endif /* TRE_USE_ALLOCA */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -109,17 +92,10 @@ typedef struct tre_backtrack_struct {
#endif /* !TRE_MBSTATE */


#ifdef TRE_USE_ALLOCA
#define tre_bt_mem_new tre_mem_newa
#define tre_bt_mem_alloc tre_mem_alloca
#define tre_bt_mem_destroy(obj) do { } while (0)
#define xafree(obj) do { } while (0) /* do nothing, obj was obtained with alloca() */
#else /* !TRE_USE_ALLOCA */
#define tre_bt_mem_new tre_mem_new
#define tre_bt_mem_alloc tre_mem_alloc
#define tre_bt_mem_destroy tre_mem_destroy
#define xafree(obj) free(obj)
#endif /* !TRE_USE_ALLOCA */


#define BT_STACK_PUSH(_pos, _str_byte, _str_wide, _state, _state_id, _next_c, _tags, _mbstate) \
Expand Down Expand Up @@ -265,11 +241,6 @@ tre_tnfa_run_backtrack(const tre_tnfa_t *tnfa, const void *string,
DPRINT(("tnfa_execute_backtrack, input type %d\n", type));
DPRINT(("len = %d\n", len));

#ifdef TRE_USE_ALLOCA
tags = alloca(sizeof(*tags) * tnfa->num_tags);
pmatch = alloca(sizeof(*pmatch) * tnfa->num_submatches);
states_seen = alloca(sizeof(*states_seen) * tnfa->num_states);
#else /* !TRE_USE_ALLOCA */
if (tnfa->num_tags)
{
tags = malloc(sizeof(*tags) * tnfa->num_tags);
Expand Down Expand Up @@ -297,7 +268,6 @@ tre_tnfa_run_backtrack(const tre_tnfa_t *tnfa, const void *string,
goto error_exit;
}
}
#endif /* !TRE_USE_ALLOCA */

retry:
{
Expand Down Expand Up @@ -653,14 +623,12 @@ tre_tnfa_run_backtrack(const tre_tnfa_t *tnfa, const void *string,

error_exit:
tre_bt_mem_destroy(mem);
#ifndef TRE_USE_ALLOCA
if (tags)
xafree(tags);
if (pmatch)
xafree(pmatch);
if (states_seen)
xafree(states_seen);
#endif /* !TRE_USE_ALLOCA */

return ret;
}
25 changes: 0 additions & 25 deletions lib/tre-match-parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@
#include <config.h>
#endif /* HAVE_CONFIG_H */

#ifdef TRE_USE_ALLOCA
/* AIX requires this to be the first thing in the file. */
#ifndef __GNUC__
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
#pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
# endif
# endif
# endif
#endif
#endif /* TRE_USE_ALLOCA */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -161,11 +144,7 @@ tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len,
+ (rbytes + xbytes * tnfa->num_states) * 2 + tbytes + pbytes;

/* Allocate the memory. */
#ifdef TRE_USE_ALLOCA
buf = alloca(total_bytes);
#else /* !TRE_USE_ALLOCA */
buf = malloc(total_bytes);
#endif /* !TRE_USE_ALLOCA */
if (buf == NULL)
return REG_ESPACE;
memset(buf, 0, total_bytes);
Expand Down Expand Up @@ -207,10 +186,8 @@ tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len,
str_byte = strchr(orig_str, first);
if (str_byte == NULL)
{
#ifndef TRE_USE_ALLOCA
if (buf)
free(buf);
#endif /* !TRE_USE_ALLOCA */
return REG_NOMATCH;
}
DPRINT(("skipped %lu chars\n", (unsigned long)(str_byte - orig_str)));
Expand Down Expand Up @@ -485,10 +462,8 @@ tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len,

DPRINT(("match end offset = %d\n", match_eo));

#ifndef TRE_USE_ALLOCA
if (buf)
free(buf);
#endif /* !TRE_USE_ALLOCA */

*match_end_ofs = match_eo;
return match_eo >= 0 ? REG_OK : REG_NOMATCH;
Expand Down
4 changes: 0 additions & 4 deletions win32/tre-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
/* Define to the absolute path to the system regex.h */
/* #undef TRE_SYSTEM_REGEX_H_PATH */

/* Define if you want TRE to use alloca() instead of malloc() when allocating
memory needed for regexec operations. */
#define TRE_USE_ALLOCA 1

/* Define to include the system regex.h from TRE regex.h */
/* #undef TRE_USE_SYSTEM_REGEX_H */

Expand Down
4 changes: 0 additions & 4 deletions win32/tre-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
/* Define to the absolute path to the system regex.h */
/* #undef TRE_SYSTEM_REGEX_H_PATH */

/* Define if you want TRE to use alloca() instead of malloc() when allocating
memory needed for regexec operations. */
#define TRE_USE_ALLOCA 1

/* Define to include the system regex.h from TRE regex.h */
/* #undef TRE_USE_SYSTEM_REGEX_H */

Expand Down

0 comments on commit ad55b97

Please sign in to comment.