@@ -606,6 +606,73 @@ lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p
606606 return 1 ;
607607}
608608
609+ /* prints little-endian numbers, will also work on big-endian just the values will look weird */
610+ static char *
611+ lyht_dbgprint_val2str (void * val_p , int32_t hits , uint16_t rec_size )
612+ {
613+ char * val ;
614+ int32_t i , j , val_size ;
615+
616+ val_size = rec_size - (sizeof (struct ht_rec ) - 1 );
617+
618+ val = malloc (val_size * 2 + 1 );
619+ for (i = 0 , j = val_size - 1 ; i < val_size ; ++ i , -- j ) {
620+ if (hits > 0 ) {
621+ sprintf (val + i * 2 , "%02x" , * (((uint8_t * )val_p ) + j ));
622+ } else {
623+ sprintf (val + i * 2 , " " );
624+ }
625+ }
626+
627+ return val ;
628+ }
629+
630+ static void
631+ lyht_dbgprint_ht (struct hash_table * ht , const char * info )
632+ {
633+ struct ht_rec * rec ;
634+ uint32_t i , i_len ;
635+ char * val ;
636+
637+ if (LY_LLDBG > ly_log_level ) {
638+ return ;
639+ }
640+
641+ LOGDBG (LY_LDGHASH , "" );
642+ LOGDBG (LY_LDGHASH , "hash table %s (used %u, size %u):" , info , ht -> used , ht -> size );
643+
644+ val = malloc (11 );
645+ sprintf (val , "%u" , ht -> size );
646+ i_len = strlen (val );
647+ free (val );
648+
649+ for (i = 0 ; i < ht -> size ; ++ i ) {
650+ rec = lyht_get_rec (ht -> recs , ht -> rec_size , i );
651+ val = lyht_dbgprint_val2str (& rec -> val , rec -> hits , ht -> rec_size );
652+ if (rec -> hits > 0 ) {
653+ LOGDBG (LY_LDGHASH , "[%*u] val %s hash %10u %% %*u hits %2d" ,
654+ (int )i_len , i , val , rec -> hash , (int )i_len , rec -> hash & (ht -> size - 1 ), rec -> hits );
655+ } else {
656+ LOGDBG (LY_LDGHASH , "[%*u] val %s hash %10s %% %*s hits %2d" ,
657+ (int )i_len , i , val , "" , (int )i_len , "" , rec -> hits );
658+ }
659+ free (val );
660+ }
661+ LOGDBG (LY_LDGHASH , "" );
662+ }
663+
664+ static void
665+ lyht_dbgprint_value (void * val_p , uint32_t hash , uint16_t rec_size , const char * operation )
666+ {
667+ if (LY_LLDBG > ly_log_level ) {
668+ return ;
669+ }
670+
671+ char * val = lyht_dbgprint_val2str (val_p , 1 , rec_size );
672+ LOGDBG (LY_LDGHASH , "%s value %s with hash %u" , operation , val , hash );
673+ free (val );
674+ }
675+
609676int
610677lyht_insert_with_resize_cb (struct hash_table * ht , void * val_p , uint32_t hash ,
611678 values_equal_cb resize_val_equal , void * * match_p )
@@ -615,6 +682,9 @@ lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
615682 int r , ret ;
616683 values_equal_cb old_val_equal ;
617684
685+ lyht_dbgprint_ht (ht , "before" );
686+ lyht_dbgprint_value (val_p , hash , ht -> rec_size , "inserting" );
687+
618688 if (!lyht_find_first (ht , hash , & rec )) {
619689 /* we found matching shortened hash */
620690 if ((rec -> hash == hash ) && ht -> val_equal (val_p , & rec -> val , 1 , ht -> cb_data )) {
@@ -688,6 +758,8 @@ lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
688758 }
689759 }
690760 }
761+
762+ lyht_dbgprint_ht (ht , "after" );
691763 return ret ;
692764}
693765
@@ -704,8 +776,12 @@ lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
704776 int32_t i ;
705777 int first_matched = 0 , r , ret ;
706778
779+ lyht_dbgprint_ht (ht , "before" );
780+ lyht_dbgprint_value (val_p , hash , ht -> rec_size , "removing" );
781+
707782 if (lyht_find_first (ht , hash , & rec )) {
708783 /* hash not found */
784+ LOGDBG (LY_LDGHASH , "remove failed" );
709785 return 1 ;
710786 }
711787 if ((rec -> hash == hash ) && ht -> val_equal (val_p , & rec -> val , 1 , ht -> cb_data )) {
@@ -743,6 +819,7 @@ lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
743819 } else {
744820 /* value not found even in collisions */
745821 assert (!first_matched );
822+ LOGDBG (LY_LDGHASH , "remove failed" );
746823 return 1 ;
747824 }
748825
@@ -757,5 +834,6 @@ lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
757834 }
758835 }
759836
837+ lyht_dbgprint_ht (ht , "after" );
760838 return ret ;
761839}
0 commit comments