diff --git a/st.c b/st.c index f4ae0e8387..976bcf4648 100644 --- a/st.c +++ b/st.c @@ -120,7 +120,7 @@ #endif #ifdef ST_DEBUG -#define st_assert(cond) assert(cond) +#define st_assert assert #else #define st_assert(cond) ((void)(0 && (cond))) #endif @@ -468,6 +468,12 @@ make_tab_empty(st_table *tab) } #ifdef ST_DEBUG +#define st_assert_notinitial(ent) \ + do { \ + st_assert(ent.hash != (st_hash_t) ST_INIT_VAL); \ + st_assert(ent.key != ST_INIT_VAL); \ + st_assert(ent.record != ST_INIT_VAL); \ + } while (0) /* Check the table T consistency. It can be extremely slow. So use it only for debugging. */ static void @@ -478,26 +484,24 @@ st_check(st_table *tab) for (p = get_allocated_entries(tab), i = 0; p > 1; i++, p>>=1) ; p = i; - assert(p >= MINIMAL_POWER2); - assert(tab->entries_bound <= get_allocated_entries(tab) - && tab->entries_start <= tab->entries_bound); + st_assert(p >= MINIMAL_POWER2); + st_assert(tab->entries_bound <= get_allocated_entries(tab)); + st_assert(tab->entries_start <= tab->entries_bound); n = 0; return; if (tab->entries_bound != 0) for (i = tab->entries_start; i < tab->entries_bound; i++) { - assert(tab->entries[i].hash != (st_hash_t) ST_INIT_VAL - && tab->entries[i].key != ST_INIT_VAL - && tab->entries[i].record != ST_INIT_VAL); + st_assert_notinitial(tab->entries[i]); if (! DELETED_ENTRY_P(&tab->entries[i])) n++; } - assert(n == tab->num_entries); + st_assert(n == tab->num_entries); if (tab->bins == NULL) - assert(p <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS); + st_assert(p <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS); else { - assert(p > MAX_POWER2_FOR_TABLES_WITHOUT_BINS); + st_assert(p > MAX_POWER2_FOR_TABLES_WITHOUT_BINS); for (n = d = i = 0; i < get_bins_num(tab); i++) { - assert(get_bin(tab->bins, tab->size_ind, i) != ST_INIT_VAL); + st_assert(get_bin(tab->bins, tab->size_ind, i) != ST_INIT_VAL); if (IND_DELETED_BIN_P(tab, i)) { d++; continue; @@ -506,14 +510,12 @@ st_check(st_table *tab) continue; n++; e = get_bin(tab->bins, tab->size_ind, i) - ENTRY_BASE; - assert(tab->entries_start <= e && e < tab->entries_bound); - assert(! DELETED_ENTRY_P(&tab->entries[e])); - assert(tab->entries[e].hash != (st_hash_t) ST_INIT_VAL - && tab->entries[e].key != ST_INIT_VAL - && tab->entries[e].record != ST_INIT_VAL); + st_assert(tab->entries_start <= e && e < tab->entries_bound); + st_assert(! DELETED_ENTRY_P(&tab->entries[e])); + st_assert_notinitial(tab->entries[e]); } - assert(n == tab->num_entries); - assert(n + d < get_bins_num(tab)); + st_assert(n == tab->num_entries); + st_assert(n + d < get_bins_num(tab)); } } #endif @@ -770,9 +772,9 @@ rebuild_table(st_table *tab) if (EXPECT(bins != NULL, 1)) { bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash, curr_entry_ptr->key); - st_assert(bin_ind != UNDEFINED_BIN_IND - && (tab == new_tab || new_tab->rebuilds_num == 0) - && IND_EMPTY_BIN_P(new_tab, bin_ind)); + st_assert(bin_ind != UNDEFINED_BIN_IND); + st_assert(tab == new_tab || new_tab->rebuilds_num == 0); + st_assert(IND_EMPTY_BIN_P(new_tab, bin_ind)); set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE); } new_tab->num_entries++; @@ -782,7 +784,8 @@ rebuild_table(st_table *tab) tab->entry_power = new_tab->entry_power; tab->bin_power = new_tab->bin_power; tab->size_ind = new_tab->size_ind; - st_assert (tab->num_entries == ni && new_tab->num_entries == ni); + st_assert(tab->num_entries == ni); + st_assert(new_tab->num_entries == ni); if (tab->bins != NULL) free(tab->bins); tab->bins = new_tab->bins; @@ -855,7 +858,8 @@ find_table_entry_ind(st_table *tab, st_hash_t hash_value, st_data_t key) st_index_t bin; st_table_entry *entries = tab->entries; - st_assert(tab != NULL && tab->bins != NULL); + st_assert(tab != NULL); + st_assert(tab->bins != NULL); ind = hash_bin(hash_value, tab); #ifdef QUADRATIC_PROBE d = 1; @@ -896,7 +900,8 @@ find_table_bin_ind(st_table *tab, st_hash_t hash_value, st_data_t key) st_index_t bin; st_table_entry *entries = tab->entries; - st_assert(tab != NULL && tab->bins != NULL); + st_assert(tab != NULL); + st_assert(tab->bins != NULL); ind = hash_bin(hash_value, tab); #ifdef QUADRATIC_PROBE d = 1; @@ -937,7 +942,8 @@ find_table_bin_ind_direct(st_table *tab, st_hash_t hash_value, st_data_t key) st_index_t bin; st_table_entry *entries = tab->entries; - st_assert(tab != NULL && tab->bins != NULL); + st_assert(tab != NULL); + st_assert(tab->bins != NULL); ind = hash_bin(hash_value, tab); #ifdef QUADRATIC_PROBE d = 1; @@ -983,9 +989,10 @@ find_table_bin_ptr_and_reserve(st_table *tab, st_hash_t *hash_value, st_index_t first_deleted_bin_ind; st_table_entry *entries; - st_assert(tab != NULL && tab->bins != NULL - && tab->entries_bound <= get_allocated_entries(tab) - && tab->entries_start <= tab->entries_bound); + st_assert(tab != NULL); + st_assert(tab->bins != NULL); + st_assert(tab->entries_bound <= get_allocated_entries(tab)); + st_assert(tab->entries_start <= tab->entries_bound); ind = hash_bin(curr_hash_value, tab); #ifdef QUADRATIC_PROBE d = 1; @@ -1202,8 +1209,8 @@ st_insert2(st_table *tab, st_data_t key, st_data_t value, st_assert(tab->entries_bound < get_allocated_entries(tab)); check = tab->rebuilds_num; key = (*func)(key); - st_assert(check == tab->rebuilds_num - && do_hash(key, tab) == hash_value); + st_assert(check == tab->rebuilds_num); + st_assert(do_hash(key, tab) == hash_value); ind = tab->entries_bound++; entry = &tab->entries[ind]; entry->hash = hash_value; @@ -1339,14 +1346,14 @@ st_shift(st_table *tab, st_data_t *key, st_data_t *value) *key = curr_entry_ptr->key; if (tab->bins == NULL) { bin = find_entry(tab, curr_entry_ptr->hash, curr_entry_ptr->key); - st_assert(bin != UNDEFINED_ENTRY_IND - && &entries[bin] == curr_entry_ptr); + st_assert(bin != UNDEFINED_ENTRY_IND); + st_assert(&entries[bin] == curr_entry_ptr); } else { bin_ind = find_table_bin_ind(tab, curr_entry_ptr->hash, curr_entry_ptr->key); - st_assert(bin_ind != UNDEFINED_BIN_IND - && &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind) + st_assert(bin_ind != UNDEFINED_BIN_IND); + st_assert(&entries[get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE] == curr_entry_ptr); MARK_BIN_DELETED(tab, bin_ind); } @@ -2162,7 +2169,7 @@ rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash) st_index_t n; st_table *tab = RHASH(hash)->ntbl; - st_assert(argc % 2); + st_assert(argc % 2 == 0); if (! argc) return; if (! tab) {