gallium/cso_hash: cosmetic changes, no behavior changes

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3829>
This commit is contained in:
Marek Olšák 2020-01-21 21:47:47 -05:00
parent 789ed29d59
commit e0bb7b87e2
2 changed files with 43 additions and 57 deletions

View File

@ -73,22 +73,12 @@ static int countBits(int hint)
return numBits; return numBits;
} }
static void *cso_data_allocate_node(struct cso_hash_data *hash)
{
return MALLOC(sizeof(struct cso_node));
}
static void cso_free_node(struct cso_node *node)
{
FREE(node);
}
static struct cso_node * static struct cso_node *
cso_hash_create_node(struct cso_hash *hash, cso_hash_create_node(struct cso_hash *hash,
unsigned akey, void *avalue, unsigned akey, void *avalue,
struct cso_node **anextNode) struct cso_node **anextNode)
{ {
struct cso_node *node = cso_data_allocate_node(hash->data.d); struct cso_node *node = MALLOC(sizeof(struct cso_node));
if (!node) if (!node)
return NULL; return NULL;
@ -96,7 +86,7 @@ cso_hash_create_node(struct cso_hash *hash,
node->key = akey; node->key = akey;
node->value = avalue; node->value = avalue;
node->next = (struct cso_node*)(*anextNode); node->next = *anextNode;
*anextNode = node; *anextNode = node;
++hash->data.d->size; ++hash->data.d->size;
return node; return node;
@ -116,7 +106,7 @@ static void cso_data_rehash(struct cso_hash_data *hash, int hint)
} }
if (hash->numBits != hint) { if (hash->numBits != hint) {
struct cso_node *e = (struct cso_node *)(hash); struct cso_node *e = (struct cso_node *)hash;
struct cso_node **oldBuckets = hash->buckets; struct cso_node **oldBuckets = hash->buckets;
int oldNumBuckets = hash->numBuckets; int oldNumBuckets = hash->numBuckets;
int i = 0; int i = 0;
@ -168,9 +158,10 @@ static void cso_data_has_shrunk(struct cso_hash_data *hash)
static struct cso_node *cso_data_first_node(struct cso_hash_data *hash) static struct cso_node *cso_data_first_node(struct cso_hash_data *hash)
{ {
struct cso_node *e = (struct cso_node *)(hash); struct cso_node *e = (struct cso_node *)hash;
struct cso_node **bucket = hash->buckets; struct cso_node **bucket = hash->buckets;
int n = hash->numBuckets; int n = hash->numBuckets;
while (n--) { while (n--) {
if (*bucket != e) if (*bucket != e)
return *bucket; return *bucket;
@ -180,23 +171,19 @@ static struct cso_node *cso_data_first_node(struct cso_hash_data *hash)
} }
struct cso_hash_iter cso_hash_insert(struct cso_hash *hash, struct cso_hash_iter cso_hash_insert(struct cso_hash *hash,
unsigned key, void *data) unsigned key, void *data)
{ {
cso_data_might_grow(hash->data.d); cso_data_might_grow(hash->data.d);
{ struct cso_node **nextNode = cso_hash_find_node(hash, key);
struct cso_node **nextNode = cso_hash_find_node(hash, key); struct cso_node *node = cso_hash_create_node(hash, key, data, nextNode);
struct cso_node *node = cso_hash_create_node(hash, key, data, nextNode); if (!node) {
if (!node) { struct cso_hash_iter null_iter = {hash, 0};
struct cso_hash_iter null_iter = {hash, 0}; return null_iter;
return null_iter;
}
{
struct cso_hash_iter iter = {hash, node};
return iter;
}
} }
struct cso_hash_iter iter = {hash, node};
return iter;
} }
bool cso_hash_init(struct cso_hash *hash) bool cso_hash_init(struct cso_hash *hash)
@ -216,14 +203,15 @@ bool cso_hash_init(struct cso_hash *hash)
void cso_hash_deinit(struct cso_hash *hash) void cso_hash_deinit(struct cso_hash *hash)
{ {
struct cso_node *e_for_x = (struct cso_node *)(hash->data.d); struct cso_node *e_for_x = (struct cso_node *)hash->data.d;
struct cso_node **bucket = (struct cso_node **)(hash->data.d->buckets); struct cso_node **bucket = hash->data.d->buckets;
int n = hash->data.d->numBuckets; int n = hash->data.d->numBuckets;
while (n--) { while (n--) {
struct cso_node *cur = *bucket++; struct cso_node *cur = *bucket++;
while (cur != e_for_x) { while (cur != e_for_x) {
struct cso_node *next = cur->next; struct cso_node *next = cur->next;
cso_free_node(cur); FREE(cur);
cur = next; cur = next;
} }
} }
@ -253,7 +241,7 @@ struct cso_node *cso_hash_data_next(struct cso_node *node)
a.next = node->next; a.next = node->next;
if (!a.next) { if (!a.next) {
debug_printf("iterating beyond the last element\n"); debug_printf("iterating beyond the last element\n");
return 0; return NULL;
} }
if (a.next->next) if (a.next->next)
return a.next; return a.next;
@ -307,26 +295,26 @@ static struct cso_node *cso_hash_data_prev(struct cso_node *node)
return a.e; return a.e;
} }
void * cso_hash_take(struct cso_hash *hash, void *cso_hash_take(struct cso_hash *hash, unsigned akey)
unsigned akey)
{ {
struct cso_node **node = cso_hash_find_node(hash, akey); struct cso_node **node = cso_hash_find_node(hash, akey);
if (*node != hash->data.e) { if (*node != hash->data.e) {
void *t = (*node)->value; void *t = (*node)->value;
struct cso_node *next = (*node)->next; struct cso_node *next = (*node)->next;
cso_free_node(*node); FREE(*node);
*node = next; *node = next;
--hash->data.d->size; --hash->data.d->size;
cso_data_has_shrunk(hash->data.d); cso_data_has_shrunk(hash->data.d);
return t; return t;
} }
return 0; return NULL;
} }
struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter) struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter)
{ {
struct cso_hash_iter prev = {iter.hash, struct cso_hash_iter prev = {iter.hash,
cso_hash_data_prev(iter.node)}; cso_hash_data_prev(iter.node)};
return prev; return prev;
} }
@ -351,17 +339,17 @@ struct cso_hash_iter cso_hash_erase(struct cso_hash *hash, struct cso_hash_iter
return iter; return iter;
ret = cso_hash_iter_next(ret); ret = cso_hash_iter_next(ret);
node_ptr = (struct cso_node**)(&hash->data.d->buckets[node->key % hash->data.d->numBuckets]); node_ptr = &hash->data.d->buckets[node->key % hash->data.d->numBuckets];
while (*node_ptr != node) while (*node_ptr != node)
node_ptr = &(*node_ptr)->next; node_ptr = &(*node_ptr)->next;
*node_ptr = node->next; *node_ptr = node->next;
cso_free_node(node); FREE(node);
--hash->data.d->size; --hash->data.d->size;
return ret; return ret;
} }
boolean cso_hash_contains(struct cso_hash *hash, unsigned key) bool cso_hash_contains(struct cso_hash *hash, unsigned key)
{ {
struct cso_node **node = cso_hash_find_node(hash, key); struct cso_node **node = cso_hash_find_node(hash, key);
return (*node != hash->data.e); return *node != hash->data.e;
} }

View File

@ -82,7 +82,7 @@ bool cso_hash_init(struct cso_hash *hash);
void cso_hash_deinit(struct cso_hash *hash); void cso_hash_deinit(struct cso_hash *hash);
int cso_hash_size(struct cso_hash *hash); int cso_hash_size(struct cso_hash *hash);
/** /**
@ -102,7 +102,7 @@ struct cso_hash_iter cso_hash_insert(struct cso_hash *hash, unsigned key,
*/ */
struct cso_hash_iter cso_hash_erase(struct cso_hash *hash, struct cso_hash_iter iter); struct cso_hash_iter cso_hash_erase(struct cso_hash *hash, struct cso_hash_iter iter);
void *cso_hash_take(struct cso_hash *hash, unsigned key); void *cso_hash_take(struct cso_hash *hash, unsigned key);
@ -111,10 +111,10 @@ struct cso_hash_iter cso_hash_first_node(struct cso_hash *hash);
/** /**
* Returns true if a value with the given key exists in the hash * Returns true if a value with the given key exists in the hash
*/ */
boolean cso_hash_contains(struct cso_hash *hash, unsigned key); bool cso_hash_contains(struct cso_hash *hash, unsigned key);
unsigned cso_hash_iter_key(struct cso_hash_iter iter); unsigned cso_hash_iter_key(struct cso_hash_iter iter);
struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter); struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter);
@ -125,26 +125,24 @@ struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter);
* comparison to see which entry in the list is a direct copy of our template * comparison to see which entry in the list is a direct copy of our template
* and returns that entry. * and returns that entry.
*/ */
void *cso_hash_find_data_from_template( struct cso_hash *hash, void *cso_hash_find_data_from_template(struct cso_hash *hash,
unsigned hash_key, unsigned hash_key,
void *templ, void *templ,
int size ); int size);
struct cso_node *cso_hash_data_next(struct cso_node *node); struct cso_node *cso_hash_data_next(struct cso_node *node);
static inline int static inline bool
cso_hash_iter_is_null(struct cso_hash_iter iter) cso_hash_iter_is_null(struct cso_hash_iter iter)
{ {
if (!iter.node || iter.node == iter.hash->data.e) return !iter.node || iter.node == iter.hash->data.e;
return 1;
return 0;
} }
static inline void * static inline void *
cso_hash_iter_data(struct cso_hash_iter iter) cso_hash_iter_data(struct cso_hash_iter iter)
{ {
if (!iter.node || iter.hash->data.e == iter.node) if (!iter.node || iter.hash->data.e == iter.node)
return 0; return NULL;
return iter.node->value; return iter.node->value;
} }
@ -154,12 +152,12 @@ cso_hash_find_node(struct cso_hash *hash, unsigned akey)
struct cso_node **node; struct cso_node **node;
if (hash->data.d->numBuckets) { if (hash->data.d->numBuckets) {
node = (struct cso_node **)(&hash->data.d->buckets[akey % hash->data.d->numBuckets]); node = &hash->data.d->buckets[akey % hash->data.d->numBuckets];
assert(*node == hash->data.e || (*node)->next); assert(*node == hash->data.e || (*node)->next);
while (*node != hash->data.e && (*node)->key != akey) while (*node != hash->data.e && (*node)->key != akey)
node = &(*node)->next; node = &(*node)->next;
} else { } else {
node = (struct cso_node **)((const struct cso_node * const *)(&hash->data.e)); node = &hash->data.e;
} }
return node; return node;
} }