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 */ BOOL utf; /* Set TRUE for UTF mode */
pcre2_real_code *re = NULL; /* What we will return */ 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 */ compile_block cb; /* "Static" compile-time data */
const uint8_t *tables; /* Char tables base pointer */ const uint8_t *tables; /* Char tables base pointer */
@ -7390,11 +7389,8 @@ if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0)
/* A NULL compile context means "use a default context" */ /* A NULL compile context means "use a default context" */
if (ccontext == NULL) if (ccontext == NULL)
{ ccontext = (pcre2_compile_context *)(&PRIV(default_compile_context));
PRIV(compile_context_init)(&default_context, TRUE);
ccontext = &default_context;
}
/* A zero-terminated pattern is indicated by the special length value /* 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, 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 /* A default compile context is set up to save having to initialize at run time
PRIV(compile_context_init)(pcre2_compile_context *ccontext, BOOL defmemctl) when no context is supplied to the compile function. */
{
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;
}
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_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
pcre2_compile_context_create(pcre2_general_context *gcontext) 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)( pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext); sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
if (ccontext == NULL) return NULL; 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; return ccontext;
} }
PCRE2_EXP_DEFN void /* A default match context is set up to save having to initialize at run time
PRIV(match_context_init)(pcre2_match_context *mcontext, BOOL defmemctl) when no context is supplied to a match function. */
{
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;
}
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_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
pcre2_match_context_create(pcre2_general_context *gcontext) 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)( pcre2_match_context *mcontext = PRIV(memctl_malloc)(
sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext); sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
if (mcontext == NULL) return NULL; 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; 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) pcre2_match_context *mcontext, int *workspace, size_t wscount)
{ {
const pcre2_real_code *re = (const pcre2_real_code *)code; 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 start_match;
PCRE2_SPTR end_subject; 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" */ /* A NULL match context means "use a default context" */
if (mcontext == NULL) if (mcontext == NULL)
{ mcontext = (pcre2_match_context *)(&PRIV(default_match_context));
PRIV(match_context_init)(&default_context, TRUE);
mcontext = &default_context;
}
/* If restarting after a partial match, do some sanity checks on the contents /* If restarting after a partial match, do some sanity checks on the contents
of the workspace. */ of the workspace. */

View File

@ -1785,12 +1785,12 @@ typedef struct {
#define MAX_NON_UTF_CHAR (0xffffffffU >> (32 - PCRE2_CODE_UNIT_WIDTH)) #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, 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 but are not part of the PCRE2 public API. Although the data for some of them is
tables is identical in all libraries, they must have different names so that identical in all libraries, they must have different names so that multiple
multiple libraries can be simultaneously linked to a single application. libraries can be simultaneously linked to a single application. However, UTF-8
However, UTF-8 tables are needed only when compiling the 8-bit library. */ tables are needed only when compiling the 8-bit library. */
#if PCRE2_CODE_UNIT_WIDTH == 8 #if PCRE2_CODE_UNIT_WIDTH == 8
extern const int PRIV(utf8_table1)[]; extern const int PRIV(utf8_table1)[];
@ -1800,39 +1800,43 @@ extern const int PRIV(utf8_table3)[];
extern const uint8_t PRIV(utf8_table4)[]; extern const uint8_t PRIV(utf8_table4)[];
#endif #endif
#define _pcre2_OP_lengths PCRE2_SUFFIX(_pcre2_OP_lengths_) #define _pcre2_OP_lengths PCRE2_SUFFIX(_pcre2_OP_lengths_)
#define _pcre2_default_tables PCRE2_SUFFIX(_pcre2_default_tables_) #define _pcre2_default_compile_context PCRE2_SUFFIX(_pcre2_default_compile_context_)
#define _pcre2_hspace_list PCRE2_SUFFIX(_pcre2_hspace_list_) #define _pcre2_default_match_context PCRE2_SUFFIX(_pcre2_default_match_context_)
#define _pcre2_vspace_list PCRE2_SUFFIX(_pcre2_vspace_list_) #define _pcre2_default_tables PCRE2_SUFFIX(_pcre2_default_tables_)
#define _pcre2_ucd_caseless_sets PCRE2_SUFFIX(_pcre2_ucd_caseless_sets_) #define _pcre2_hspace_list PCRE2_SUFFIX(_pcre2_hspace_list_)
#define _pcre2_ucd_records PCRE2_SUFFIX(_pcre2_ucd_records_) #define _pcre2_vspace_list PCRE2_SUFFIX(_pcre2_vspace_list_)
#define _pcre2_ucd_stage1 PCRE2_SUFFIX(_pcre2_ucd_stage1_) #define _pcre2_ucd_caseless_sets PCRE2_SUFFIX(_pcre2_ucd_caseless_sets_)
#define _pcre2_ucd_stage2 PCRE2_SUFFIX(_pcre2_ucd_stage2_) #define _pcre2_ucd_records PCRE2_SUFFIX(_pcre2_ucd_records_)
#define _pcre2_ucp_gbtable PCRE2_SUFFIX(_pcre2_ucp_gbtable_) #define _pcre2_ucd_stage1 PCRE2_SUFFIX(_pcre2_ucd_stage1_)
#define _pcre2_ucp_gentype PCRE2_SUFFIX(_pcre2_ucp_gentype_) #define _pcre2_ucd_stage2 PCRE2_SUFFIX(_pcre2_ucd_stage2_)
#define _pcre2_ucp_typerange PCRE2_SUFFIX(_pcre2_ucp_typerange_) #define _pcre2_ucp_gbtable PCRE2_SUFFIX(_pcre2_ucp_gbtable_)
#define _pcre2_unicode_version PCRE2_SUFFIX(_pcre2_unicode_version_) #define _pcre2_ucp_gentype PCRE2_SUFFIX(_pcre2_ucp_gentype_)
#define _pcre2_utt PCRE2_SUFFIX(_pcre2_utt_) #define _pcre2_ucp_typerange PCRE2_SUFFIX(_pcre2_ucp_typerange_)
#define _pcre2_utt_names PCRE2_SUFFIX(_pcre2_utt_names_) #define _pcre2_unicode_version PCRE2_SUFFIX(_pcre2_unicode_version_)
#define _pcre2_utt_size PCRE2_SUFFIX(_pcre2_utt_size_) #define _pcre2_utt PCRE2_SUFFIX(_pcre2_utt_)
#define _pcre2_utt_names PCRE2_SUFFIX(_pcre2_utt_names_)
#define _pcre2_utt_size PCRE2_SUFFIX(_pcre2_utt_size_)
extern const uint8_t PRIV(OP_lengths)[]; extern const uint8_t PRIV(OP_lengths)[];
extern const uint8_t PRIV(default_tables)[]; extern const pcre2_compile_context PRIV(default_compile_context);
extern const uint32_t PRIV(hspace_list)[]; extern const pcre2_match_context PRIV(default_match_context);
extern const uint32_t PRIV(vspace_list)[]; extern const uint8_t PRIV(default_tables)[];
extern const uint32_t PRIV(ucd_caseless_sets)[]; extern const uint32_t PRIV(hspace_list)[];
extern const ucd_record PRIV(ucd_records)[]; extern const uint32_t PRIV(vspace_list)[];
extern const uint8_t PRIV(ucd_stage1)[]; extern const uint32_t PRIV(ucd_caseless_sets)[];
extern const uint16_t PRIV(ucd_stage2)[]; extern const ucd_record PRIV(ucd_records)[];
extern const uint32_t PRIV(ucp_gbtable)[]; extern const uint8_t PRIV(ucd_stage1)[];
extern const uint32_t PRIV(ucp_gentype)[]; extern const uint16_t PRIV(ucd_stage2)[];
#ifdef SUPPORT_JIT extern const uint32_t PRIV(ucp_gbtable)[];
extern const int PRIV(ucp_typerange)[]; extern const uint32_t PRIV(ucp_gentype)[];
#endif #ifdef SUPPORT_JIT
extern const char *PRIV(unicode_version); extern const int PRIV(ucp_typerange)[];
extern const ucp_type_table PRIV(utt)[]; #endif
extern const char PRIV(utt_names)[]; extern const char *PRIV(unicode_version);
extern const size_t PRIV(utt_size); extern const ucp_type_table PRIV(utt)[];
extern const char PRIV(utt_names)[];
extern const size_t PRIV(utt_size);
/* Mode-dependent macros and hidden and private structures are defined in a /* Mode-dependent macros and hidden and private structures are defined in a
separate file so that pcre2test can include them at all supported widths. When separate file so that pcre2test can include them at all supported widths. When
@ -1855,12 +1859,10 @@ not referenced from pcre2test, and must not be defined when no code unit width
is available. */ is available. */
#define _pcre2_auto_possessify PCRE2_SUFFIX(_pcre2_auto_possessify_) #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_find_bracket PCRE2_SUFFIX(_pcre2_find_bracket_)
#define _pcre2_is_newline PCRE2_SUFFIX(_pcre2_is_newline_) #define _pcre2_is_newline PCRE2_SUFFIX(_pcre2_is_newline_)
#define _pcre2_jit_free PCRE2_SUFFIX(_pcre2_jit_free_) #define _pcre2_jit_free PCRE2_SUFFIX(_pcre2_jit_free_)
#define _pcre2_jit_get_size PCRE2_SUFFIX(_pcre2_jit_get_size_) #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_memctl_malloc PCRE2_SUFFIX(_pcre2_memctl_malloc_)
#define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_) #define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_)
#define _pcre2_strcmp PCRE2_SUFFIX(_pcre_strcmp_) #define _pcre2_strcmp PCRE2_SUFFIX(_pcre_strcmp_)
@ -1874,13 +1876,11 @@ is available. */
#define _pcre2_xclass PCRE2_SUFFIX(_pcre2_xclass_) #define _pcre2_xclass PCRE2_SUFFIX(_pcre2_xclass_)
extern void _pcre2_auto_possessify(PCRE2_UCHAR *, BOOL, const compile_block *); 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 PCRE2_SPTR _pcre2_find_bracket(PCRE2_SPTR, BOOL, int);
extern BOOL _pcre2_is_newline(PCRE2_SPTR, uint32_t, PCRE2_SPTR, uint32_t *, extern BOOL _pcre2_is_newline(PCRE2_SPTR, uint32_t, PCRE2_SPTR, uint32_t *,
BOOL); BOOL);
extern void _pcre2_jit_free(void *, pcre2_memctl *); extern void _pcre2_jit_free(void *, pcre2_memctl *);
extern size_t _pcre2_jit_get_size(void *); 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 void *_pcre2_memctl_malloc(size_t, pcre2_memctl *);
extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *); extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *);
extern int _pcre2_strcmp(PCRE2_SPTR, PCRE2_SPTR); extern int _pcre2_strcmp(PCRE2_SPTR, PCRE2_SPTR);

View File

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