@@ -86,6 +86,12 @@ static str database_url = {NULL, 0};
8686
8787void * dp_srg = NULL ;
8888
89+ pcre2_general_context * dp_gcontext = NULL ;
90+ pcre2_compile_context * dp_ccontext = NULL ;
91+
92+ void * wrap_shm_malloc (PCRE2_SIZE size , void * memory_data );
93+ void wrap_shm_free (void * p , void * memory_data );
94+
8995
9096static const param_export_t mod_params []= {
9197 { "partition" , STR_PARAM |USE_FUNC_PARAM ,
@@ -398,6 +404,18 @@ static int mod_init(void)
398404 return -1 ;
399405 }
400406
407+ dp_gcontext = pcre2_general_context_create (wrap_shm_malloc , wrap_shm_free , NULL );
408+ if (!dp_gcontext ) {
409+ LM_ERR ("Unable to create pcre general context\n" );
410+ return -1 ;
411+ }
412+
413+ dp_ccontext = pcre2_compile_context_create (dp_gcontext );
414+ if (!dp_ccontext ) {
415+ LM_ERR ("Unable to create pcre compile context\n" );
416+ return -1 ;
417+ }
418+
401419 return 0 ;
402420}
403421
@@ -452,6 +470,18 @@ static void mod_destroy(void)
452470 }
453471
454472 destroy_data ();
473+
474+ if (dp_ccontext )
475+ {
476+ pcre2_compile_context_free (dp_ccontext );
477+ dp_ccontext = NULL ;
478+ }
479+
480+ if (dp_gcontext )
481+ {
482+ pcre2_general_context_free (dp_gcontext );
483+ dp_gcontext = NULL ;
484+ }
455485}
456486
457487
@@ -883,28 +913,11 @@ void wrap_shm_free(void * p, void * memory_data)
883913
884914pcre2_code * wrap_pcre_compile (char * pattern , int flags )
885915{
886- pcre2_general_context * gcontext ;
887- pcre2_compile_context * ccontext ;
888916 pcre2_code * ret ;
889917 int error ;
890918 PCRE2_SIZE erroffset ;
891919 int pcre_flags = 0 ;
892920
893- // TODO generate once per worker
894- gcontext = pcre2_general_context_create (wrap_shm_malloc , wrap_shm_free , NULL );
895- if (!gcontext ) {
896- LM_ERR ("Unable to create pcre general context\n" );
897- return NULL ;
898- }
899-
900- // TODO generate once per worker
901- ccontext = pcre2_compile_context_create (gcontext );
902- if (!ccontext ) {
903- LM_ERR ("Unable to create pcre compile context\n" );
904- pcre2_general_context_free (gcontext );
905- return NULL ;
906- }
907-
908921 if (flags & DP_CASE_INSENSITIVE )
909922 pcre_flags |= PCRE2_CASELESS ;
910923
@@ -914,16 +927,14 @@ pcre2_code * wrap_pcre_compile(char * pattern, int flags)
914927 pcre_flags , /* default options */
915928 & error , /* for error message */
916929 & erroffset , /* for error offset */
917- ccontext ); /* compile context, to allocate memory in shm */
918-
919- pcre2_compile_context_free (ccontext );
920- pcre2_general_context_free (gcontext );
930+ dp_ccontext ); /* compile context, to allocate memory in shm */
921931
922932 return ret ;
923933}
924934
925935void wrap_pcre_free ( pcre2_code * re )
926936{
927- // TODO pcre2_code_free
937+ // *not* pcre2_code_free
938+ // shm_free is used because pcre2_general_context_create overrides malloc with wrap_shm_malloc
928939 shm_free (re );
929940}
0 commit comments