Create default compile and match contexts as constant static data.

This commit is contained in:
Philip.Hazel 2014-10-11 17:05:18 +00:00
parent b6e793f343
commit 0ec351e95e
5 changed files with 77 additions and 91 deletions

View File

@ -7333,7 +7333,6 @@ pcre2_compile(PCRE2_SPTR pattern, PCRE2_SIZE patlen, uint32_t options,
{
BOOL utf; /* Set TRUE for UTF mode */
pcre2_real_code *re = NULL; /* What we will return */
pcre2_compile_context default_context; /* For use if no context given */
compile_block cb; /* "Static" compile-time data */
const uint8_t *tables; /* Char tables base pointer */
@ -7391,10 +7390,7 @@ if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0)
/* A NULL compile context means "use a default context" */
if (ccontext == NULL)
{
PRIV(compile_context_init)(&default_context, TRUE);
ccontext = &default_context;
}
ccontext = (pcre2_compile_context *)(&PRIV(default_compile_context));
/* A zero-terminated pattern is indicated by the special length value
PCRE2_ZERO_TERMINATED. Otherwise, we make a copy of the pattern and add a zero,

View File

@ -127,22 +127,19 @@ return gcontext;
}
PCRE2_EXP_DEFN void
PRIV(compile_context_init)(pcre2_compile_context *ccontext, BOOL defmemctl)
{
if (defmemctl)
{
ccontext->memctl.malloc = default_malloc;
ccontext->memctl.free = default_free;
ccontext->memctl.memory_data = NULL;
}
ccontext->stack_guard = NULL;
ccontext->tables = PRIV(default_tables);
ccontext->parens_nest_limit = PARENS_NEST_LIMIT;
ccontext->newline_convention = NEWLINE_DEFAULT;
ccontext->bsr_convention = BSR_DEFAULT;
}
/* A default compile context is set up to save having to initialize at run time
when no context is supplied to the compile function. */
const pcre2_compile_context PRIV(default_compile_context) = {
{ default_malloc, default_free, NULL },
NULL,
PRIV(default_tables),
BSR_DEFAULT,
NEWLINE_DEFAULT,
PARENS_NEST_LIMIT };
/* The create function copies the default into the new memory, but must
override the default memory handling functions if a gcontext was provided. */
PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
pcre2_compile_context_create(pcre2_general_context *gcontext)
@ -150,29 +147,28 @@ pcre2_compile_context_create(pcre2_general_context *gcontext)
pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
if (ccontext == NULL) return NULL;
PRIV(compile_context_init)(ccontext, FALSE);
*ccontext = PRIV(default_compile_context);
if (gcontext != NULL)
*((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
return ccontext;
}
PCRE2_EXP_DEFN void
PRIV(match_context_init)(pcre2_match_context *mcontext, BOOL defmemctl)
{
if (defmemctl)
{
mcontext->memctl.malloc = default_malloc;
mcontext->memctl.free = default_free;
mcontext->memctl.memory_data = NULL;
}
#ifdef HEAP_MATCH_RECURSE
mcontext->stack_memctl = mcontext->memctl;
#endif
mcontext->callout = NULL;
mcontext->callout_data = NULL;
mcontext->match_limit = MATCH_LIMIT;
mcontext->recursion_limit = MATCH_LIMIT_RECURSION;
}
/* A default match context is set up to save having to initialize at run time
when no context is supplied to a match function. */
const pcre2_match_context PRIV(default_match_context) = {
{ default_malloc, default_free, NULL },
#ifdef HEAP_MATCH_RECURSE
{ default_malloc, default_free, NULL },
#endif
NULL,
NULL,
MATCH_LIMIT,
MATCH_LIMIT_RECURSION };
/* The create function copies the default into the new memory, but must
override the default memory handling functions if a gcontext was provided. */
PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
pcre2_match_context_create(pcre2_general_context *gcontext)
@ -180,7 +176,9 @@ pcre2_match_context_create(pcre2_general_context *gcontext)
pcre2_match_context *mcontext = PRIV(memctl_malloc)(
sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
if (mcontext == NULL) return NULL;
PRIV(match_context_init)(mcontext, FALSE);
*mcontext = PRIV(default_match_context);
if (gcontext != NULL)
*((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
return mcontext;
}

View File

@ -3068,7 +3068,6 @@ pcre2_dfa_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
pcre2_match_context *mcontext, int *workspace, size_t wscount)
{
const pcre2_real_code *re = (const pcre2_real_code *)code;
pcre2_match_context default_context; /* For use if no context given */
PCRE2_SPTR start_match;
PCRE2_SPTR end_subject;
@ -3149,10 +3148,7 @@ options |= (re->flags & FF) / ((FF & -FF) / (OO & -OO));
/* A NULL match context means "use a default context" */
if (mcontext == NULL)
{
PRIV(match_context_init)(&default_context, TRUE);
mcontext = &default_context;
}
mcontext = (pcre2_match_context *)(&PRIV(default_match_context));
/* If restarting after a partial match, do some sanity checks on the contents
of the workspace. */

View File

@ -1785,12 +1785,12 @@ typedef struct {
#define MAX_NON_UTF_CHAR (0xffffffffU >> (32 - PCRE2_CODE_UNIT_WIDTH))
/* Internal shared data tables. These are tables that are used by more than one
/* Internal shared data tables and variables. These are used by more than one
of the exported public functions. They have to be "external" in the C sense,
but are not part of the PCRE2 public API. Although the data for some of the
tables is identical in all libraries, they must have different names so that
multiple libraries can be simultaneously linked to a single application.
However, UTF-8 tables are needed only when compiling the 8-bit library. */
but are not part of the PCRE2 public API. Although the data for some of them is
identical in all libraries, they must have different names so that multiple
libraries can be simultaneously linked to a single application. However, UTF-8
tables are needed only when compiling the 8-bit library. */
#if PCRE2_CODE_UNIT_WIDTH == 8
extern const int PRIV(utf8_table1)[];
@ -1801,6 +1801,8 @@ extern const uint8_t PRIV(utf8_table4)[];
#endif
#define _pcre2_OP_lengths PCRE2_SUFFIX(_pcre2_OP_lengths_)
#define _pcre2_default_compile_context PCRE2_SUFFIX(_pcre2_default_compile_context_)
#define _pcre2_default_match_context PCRE2_SUFFIX(_pcre2_default_match_context_)
#define _pcre2_default_tables PCRE2_SUFFIX(_pcre2_default_tables_)
#define _pcre2_hspace_list PCRE2_SUFFIX(_pcre2_hspace_list_)
#define _pcre2_vspace_list PCRE2_SUFFIX(_pcre2_vspace_list_)
@ -1817,6 +1819,8 @@ extern const uint8_t PRIV(utf8_table4)[];
#define _pcre2_utt_size PCRE2_SUFFIX(_pcre2_utt_size_)
extern const uint8_t PRIV(OP_lengths)[];
extern const pcre2_compile_context PRIV(default_compile_context);
extern const pcre2_match_context PRIV(default_match_context);
extern const uint8_t PRIV(default_tables)[];
extern const uint32_t PRIV(hspace_list)[];
extern const uint32_t PRIV(vspace_list)[];
@ -1855,12 +1859,10 @@ not referenced from pcre2test, and must not be defined when no code unit width
is available. */
#define _pcre2_auto_possessify PCRE2_SUFFIX(_pcre2_auto_possessify_)
#define _pcre2_compile_context_init PCRE2_SUFFIX(_pcre2_compile_context_init_)
#define _pcre2_find_bracket PCRE2_SUFFIX(_pcre2_find_bracket_)
#define _pcre2_is_newline PCRE2_SUFFIX(_pcre2_is_newline_)
#define _pcre2_jit_free PCRE2_SUFFIX(_pcre2_jit_free_)
#define _pcre2_jit_get_size PCRE2_SUFFIX(_pcre2_jit_get_size_)
#define _pcre2_match_context_init PCRE2_SUFFIX(_pcre2_match_context_init_)
#define _pcre2_memctl_malloc PCRE2_SUFFIX(_pcre2_memctl_malloc_)
#define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_)
#define _pcre2_strcmp PCRE2_SUFFIX(_pcre_strcmp_)
@ -1874,13 +1876,11 @@ is available. */
#define _pcre2_xclass PCRE2_SUFFIX(_pcre2_xclass_)
extern void _pcre2_auto_possessify(PCRE2_UCHAR *, BOOL, const compile_block *);
extern void _pcre2_compile_context_init(pcre2_compile_context *, BOOL);
extern PCRE2_SPTR _pcre2_find_bracket(PCRE2_SPTR, BOOL, int);
extern BOOL _pcre2_is_newline(PCRE2_SPTR, uint32_t, PCRE2_SPTR, uint32_t *,
BOOL);
extern void _pcre2_jit_free(void *, pcre2_memctl *);
extern size_t _pcre2_jit_get_size(void *);
extern void _pcre2_match_context_init(pcre2_match_context *, BOOL);
extern void *_pcre2_memctl_malloc(size_t, pcre2_memctl *);
extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *);
extern int _pcre2_strcmp(PCRE2_SPTR, PCRE2_SPTR);

View File

@ -6352,7 +6352,6 @@ int ocount;
const uint8_t *start_bits = NULL;
const pcre2_real_code *re = (const pcre2_real_code *)code;
pcre2_match_context default_context; /* For use if no context given */
BOOL anchored;
BOOL firstline;
@ -6440,10 +6439,7 @@ options |= (re->flags & FF) / ((FF & -FF) / (OO & -OO));
/* A NULL match context means "use a default context" */
if (mcontext == NULL)
{
PRIV(match_context_init)(&default_context, TRUE);
mcontext = &default_context;
}
mcontext = (pcre2_match_context *)(&PRIV(default_match_context));
/* These two settings are used in the code for checking a UTF string that
follows immediately afterwards. Other values in the mb block are used only