Added number of inserted elements to hashtable
Added tracking of the number of inserted elements to the hashtable. Also added utility function to get the size of the hashtable.
This commit is contained in:
parent
cd6f7ce046
commit
8363673be6
@ -148,6 +148,7 @@ hashtable_alloc_real(
|
||||
rval->vfreefn = nullfn;
|
||||
rval->n_readers = 0;
|
||||
rval->writelock = 0;
|
||||
rval->n_elements = 0;
|
||||
spinlock_init(&rval->spin);
|
||||
if ((rval->entries = (HASHENTRIES **)calloc(rval->hashsize, sizeof(HASHENTRIES *))) == NULL)
|
||||
{
|
||||
@ -296,6 +297,7 @@ hashtable_add(HASHTABLE *table, void *key, void *value)
|
||||
ptr->next = table->entries[hashkey % table->hashsize];
|
||||
table->entries[hashkey % table->hashsize] = ptr;
|
||||
}
|
||||
table->n_elements++;
|
||||
hashtable_write_unlock(table);
|
||||
|
||||
return 1;
|
||||
@ -362,6 +364,8 @@ HASHENTRIES *entry, *ptr;
|
||||
table->vfreefn(entry->value);
|
||||
free(entry);
|
||||
}
|
||||
table->n_elements--;
|
||||
assert(table->n_elements >= 0);
|
||||
hashtable_write_unlock(table);
|
||||
return 1;
|
||||
}
|
||||
@ -770,3 +774,17 @@ char buf[40];
|
||||
close(fd);
|
||||
return rval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of elements added to the hashtable
|
||||
* @param table Hashtable to measure
|
||||
* @return Number of inserted elements or 0 if table is NULL
|
||||
*/
|
||||
int hashtable_size(HASHTABLE *table)
|
||||
{
|
||||
assert(table);
|
||||
spinlock_acquire(&table->spin);
|
||||
int rval = table->n_elements;
|
||||
spinlock_release(&table->spin);
|
||||
return rval;
|
||||
}
|
||||
|
@ -85,6 +85,7 @@ typedef struct hashtable {
|
||||
int n_readers; /**< Number of clients reading the table */
|
||||
int writelock; /**< The table is locked by a writer */
|
||||
bool ht_isflat; /**< Indicates whether hashtable is in stack or heap */
|
||||
int n_elements; /*< Number of added elements */
|
||||
#if defined(SS_DEBUG)
|
||||
skygw_chk_t ht_chk_tail;
|
||||
#endif
|
||||
@ -130,4 +131,5 @@ extern HASHITERATOR *hashtable_iterator(HASHTABLE *);
|
||||
extern void *hashtable_next(HASHITERATOR *);
|
||||
/**< Return the key of the hash table iterator */
|
||||
extern void hashtable_iterator_free(HASHITERATOR *);
|
||||
extern int hashtable_size(HASHTABLE *table);
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user