@@ -385,9 +385,9 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
385385 word at = path ^ key ;
386386 if (!at ) {
387387 ASSERT (is_leaf (n ));
388- if (to_leaf (kn )-> value == value ) {
388+ if (to_leaf (kn )-> to_be_freed == value ) {
389389 // do not free the value
390- to_leaf (kn )-> value = NULL ;
390+ to_leaf (kn )-> to_be_freed = NULL ;
391391 }
392392 free_leaf (c , to_leaf (kn ));
393393
@@ -453,7 +453,7 @@ void *critnib_remove(struct critnib *c, word key, void **ref) {
453453 do {
454454 del = (utils_atomic_increment_u64 (& c -> remove_count ) - 1 ) % DELETED_LIFE ;
455455 k = c -> pending_del_leaves [del ];
456- if (i_del ++ > DELETED_LIFE ) {
456+ if (i_del ++ == DELETED_LIFE ) {
457457 break ;
458458 }
459459 } while (k && (k -> ref_count > 0 ));
@@ -539,7 +539,8 @@ int critnib_release(struct critnib *c, void *ref) {
539539 struct critnib_leaf * k = (struct critnib_leaf * )ref ;
540540
541541 /* decrement the reference count */
542- if (utils_atomic_decrement_u64 (& k -> ref_count ) == 0 ) {
542+ if (k -> key && k -> to_be_freed && k -> ref_count &&
543+ (utils_atomic_decrement_u64 (& k -> ref_count ) == 0 )) {
543544 void * value = k -> to_be_freed ;
544545 utils_atomic_store_release_ptr (& k -> to_be_freed , NULL );
545546 utils_atomic_store_release_u64 (& k -> key , 0 );
@@ -597,7 +598,7 @@ void *critnib_get(struct critnib *c, word key, void **ref) {
597598 if (ref_count == 0 ) {
598599 return NULL ;
599600 }
600- if (( ref_count = utils_atomic_increment_u64 (& k -> ref_count ) ) == 1 ) {
601+ if (utils_atomic_increment_u64 (& k -> ref_count ) == 1 ) {
601602 utils_atomic_decrement_u64 (& k -> ref_count );
602603 return NULL ;
603604 }
@@ -740,7 +741,7 @@ void *critnib_find_le(struct critnib *c, word key, void **ref) {
740741 if (ref_count == 0 ) {
741742 return NULL ;
742743 }
743- if (( ref_count = utils_atomic_increment_u64 (& k -> ref_count ) ) == 1 ) {
744+ if (utils_atomic_increment_u64 (& k -> ref_count ) == 1 ) {
744745 utils_atomic_decrement_u64 (& k -> ref_count );
745746 return NULL ;
746747 }
@@ -889,7 +890,7 @@ int critnib_find(struct critnib *c, uintptr_t key, enum find_dir_t dir,
889890 if (ref_count == 0 ) {
890891 return 0 ;
891892 }
892- if (( ref_count = utils_atomic_increment_u64 (& k -> ref_count ) ) == 1 ) {
893+ if (utils_atomic_increment_u64 (& k -> ref_count ) == 1 ) {
893894 utils_atomic_decrement_u64 (& k -> ref_count );
894895 return 0 ;
895896 }
0 commit comments