From 0ec351e95e5dab5aedc8706ee3d28716e5b46fd6 Mon Sep 17 00:00:00 2001 From: "Philip.Hazel" Date: Sat, 11 Oct 2014 17:05:18 +0000 Subject: [PATCH] Create default compile and match contexts as constant static data. --- src/pcre2_compile.c | 8 ++--- src/pcre2_context.c | 66 +++++++++++++++++----------------- src/pcre2_dfa_match.c | 6 +--- src/pcre2_internal.h | 82 +++++++++++++++++++++---------------------- src/pcre2_match.c | 6 +--- 5 files changed, 77 insertions(+), 91 deletions(-) diff --git a/src/pcre2_compile.c b/src/pcre2_compile.c index 2af6e02..f8754c1 100644 --- a/src/pcre2_compile.c +++ b/src/pcre2_compile.c @@ -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 */ @@ -7390,11 +7389,8 @@ 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; - } +if (ccontext == NULL) + 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, diff --git a/src/pcre2_context.c b/src/pcre2_context.c index 6856f2d..9c3037f 100644 --- a/src/pcre2_context.c +++ b/src/pcre2_context.c @@ -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; } diff --git a/src/pcre2_dfa_match.c b/src/pcre2_dfa_match.c index d7168b0..417eff3 100644 --- a/src/pcre2_dfa_match.c +++ b/src/pcre2_dfa_match.c @@ -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. */ diff --git a/src/pcre2_internal.h b/src/pcre2_internal.h index ca476e9..e54c5c4 100644 --- a/src/pcre2_internal.h +++ b/src/pcre2_internal.h @@ -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)[]; @@ -1800,39 +1800,43 @@ extern const int PRIV(utf8_table3)[]; extern const uint8_t PRIV(utf8_table4)[]; #endif -#define _pcre2_OP_lengths PCRE2_SUFFIX(_pcre2_OP_lengths_) -#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_) -#define _pcre2_ucd_caseless_sets PCRE2_SUFFIX(_pcre2_ucd_caseless_sets_) -#define _pcre2_ucd_records PCRE2_SUFFIX(_pcre2_ucd_records_) -#define _pcre2_ucd_stage1 PCRE2_SUFFIX(_pcre2_ucd_stage1_) -#define _pcre2_ucd_stage2 PCRE2_SUFFIX(_pcre2_ucd_stage2_) -#define _pcre2_ucp_gbtable PCRE2_SUFFIX(_pcre2_ucp_gbtable_) -#define _pcre2_ucp_gentype PCRE2_SUFFIX(_pcre2_ucp_gentype_) -#define _pcre2_ucp_typerange PCRE2_SUFFIX(_pcre2_ucp_typerange_) -#define _pcre2_unicode_version PCRE2_SUFFIX(_pcre2_unicode_version_) -#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_) +#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_) +#define _pcre2_ucd_caseless_sets PCRE2_SUFFIX(_pcre2_ucd_caseless_sets_) +#define _pcre2_ucd_records PCRE2_SUFFIX(_pcre2_ucd_records_) +#define _pcre2_ucd_stage1 PCRE2_SUFFIX(_pcre2_ucd_stage1_) +#define _pcre2_ucd_stage2 PCRE2_SUFFIX(_pcre2_ucd_stage2_) +#define _pcre2_ucp_gbtable PCRE2_SUFFIX(_pcre2_ucp_gbtable_) +#define _pcre2_ucp_gentype PCRE2_SUFFIX(_pcre2_ucp_gentype_) +#define _pcre2_ucp_typerange PCRE2_SUFFIX(_pcre2_ucp_typerange_) +#define _pcre2_unicode_version PCRE2_SUFFIX(_pcre2_unicode_version_) +#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(default_tables)[]; -extern const uint32_t PRIV(hspace_list)[]; -extern const uint32_t PRIV(vspace_list)[]; -extern const uint32_t PRIV(ucd_caseless_sets)[]; -extern const ucd_record PRIV(ucd_records)[]; -extern const uint8_t PRIV(ucd_stage1)[]; -extern const uint16_t PRIV(ucd_stage2)[]; -extern const uint32_t PRIV(ucp_gbtable)[]; -extern const uint32_t PRIV(ucp_gentype)[]; -#ifdef SUPPORT_JIT -extern const int PRIV(ucp_typerange)[]; -#endif -extern const char *PRIV(unicode_version); -extern const ucp_type_table PRIV(utt)[]; -extern const char PRIV(utt_names)[]; -extern const size_t PRIV(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)[]; +extern const uint32_t PRIV(ucd_caseless_sets)[]; +extern const ucd_record PRIV(ucd_records)[]; +extern const uint8_t PRIV(ucd_stage1)[]; +extern const uint16_t PRIV(ucd_stage2)[]; +extern const uint32_t PRIV(ucp_gbtable)[]; +extern const uint32_t PRIV(ucp_gentype)[]; +#ifdef SUPPORT_JIT +extern const int PRIV(ucp_typerange)[]; +#endif +extern const char *PRIV(unicode_version); +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 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. */ #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); diff --git a/src/pcre2_match.c b/src/pcre2_match.c index 64c8379..05b258e 100644 --- a/src/pcre2_match.c +++ b/src/pcre2_match.c @@ -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