@@ -141,20 +141,6 @@ ly_ctx_ht_pattern_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void
141141 return !strcmp (val1 -> pattern , val2 -> pattern );
142142}
143143
144- /**
145- * @brief Callback for freeing a pattern record.
146- */
147- static void
148- ly_ctx_ht_pattern_free_cb (void * val_p )
149- {
150- struct ly_pattern_ht_rec * val = val_p ;
151-
152- if (val -> pcode ) {
153- /* free the pcode */
154- pcre2_code_free (val -> pcode );
155- }
156- }
157-
158144/**
159145 * @brief Remove private context data from the sized array and free its contents.
160146 *
@@ -304,8 +290,11 @@ ly_ctx_shared_data_remove_and_free(struct ly_ctx_shared_data *shared_data)
304290 return ;
305291 }
306292
307- /* free all the cached pattern pcodes */
308- lyht_free (shared_data -> pattern_ht , ly_ctx_ht_pattern_free_cb );
293+ /* all the patterns must have been removed already,
294+ * either while free compiled modules (standard behavior)
295+ * or when assigning a parent to a context, it's shared data will be used (schema mount) */
296+ assert (shared_data -> pattern_ht -> used == 0 );
297+ lyht_free (shared_data -> pattern_ht , NULL );
309298
310299 /* free rest of the members */
311300 lydict_clean (shared_data -> data_dict );
@@ -544,6 +533,16 @@ ly_ctx_data_del(const struct ly_ctx *ctx)
544533 pthread_rwlock_unlock (& ly_ctx_data_rwlock );
545534}
546535
536+ void
537+ ly_ctx_ht_pattern_rec_free (struct ly_pattern_ht_rec * rec )
538+ {
539+ if (!rec ) {
540+ return ;
541+ }
542+
543+ pcre2_code_free (rec -> pcode );
544+ }
545+
547546LY_ERR
548547ly_ctx_shared_data_pattern_get (const struct ly_ctx * ctx , const char * pattern , const pcre2_code * * pcode )
549548{
@@ -559,39 +558,36 @@ ly_ctx_shared_data_pattern_get(const struct ly_ctx *ctx, const char *pattern, co
559558 * pcode = NULL ;
560559 }
561560
561+ /* get the context shared data */
562562 ctx_data = ly_ctx_shared_data_get (ctx );
563563 LY_CHECK_RET (!ctx_data , LY_EINT );
564564
565565 /* try to find the pattern code in the pattern ht */
566566 hash = lyht_hash (pattern , strlen (pattern ));
567567 rec .pattern = pattern ;
568568 if (!lyht_find (ctx_data -> pattern_ht , & rec , hash , (void * * )& found_rec )) {
569- /* found it, return it */
569+ /* pcode cached */
570570 if (pcode ) {
571571 * pcode = found_rec -> pcode ;
572572 }
573573 goto cleanup ;
574574 }
575575
576- /* didnt find it, need to compile it */
576+ /* didnt find it, either it's the first time or using printed context (which compiles the pcodes on the fly) */
577+ assert (!pcode || ly_ctx_is_printed (ctx ));
577578 LY_CHECK_GOTO (rc = lys_compile_type_pattern_check (ctx , pattern , & pcode_tmp ), cleanup );
578579
579580 /* store the compiled pattern code in the hash table */
580- hash = lyht_hash (pattern , strlen (pattern ));
581- rec .pattern = pattern ;
582581 rec .pcode = pcode_tmp ;
583582 LY_CHECK_GOTO (rc = lyht_insert_no_check (ctx_data -> pattern_ht , & rec , hash , NULL ), cleanup );
584583
585584 if (pcode ) {
586585 * pcode = pcode_tmp ;
587- pcode_tmp = NULL ;
588586 }
587+ pcode_tmp = NULL ;
589588
590589cleanup :
591- if (rc ) {
592- /* only free the pcode if we failed, because it belongs to the hash table */
593- pcre2_code_free (pcode_tmp );
594- }
590+ pcre2_code_free (pcode_tmp );
595591 return rc ;
596592}
597593
@@ -613,7 +609,8 @@ ly_ctx_shared_data_pattern_del(const struct ly_ctx *ctx, const char *pattern)
613609 rec .pattern = pattern ;
614610
615611 if (lyht_find (ctx_data -> pattern_ht , & rec , hash , (void * * )& found_rec )) {
616- /* pattern code not cached yet */
612+ /* pattern code not cached, this may happen when using printed context,
613+ * because then the pcodes are obtained on demand */
617614 return ;
618615 }
619616
0 commit comments