Path: blob/21.2-virgl/src/gallium/auxiliary/cso_cache/cso_hash.h
4565 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* @file29* Hash table implementation.30*31* This file provides a hash implementation that is capable of dealing32* with collisions. It stores colliding entries in linked list. All33* functions operating on the hash return an iterator. The iterator34* itself points to the collision list. If there wasn't any collision35* the list will have just one entry, otherwise client code should36* iterate over the entries to find the exact entry among ones that37* had the same key (e.g. memcmp could be used on the data to check38* that)39*40* @author Zack Rusin <[email protected]>41*/4243#ifndef CSO_HASH_H44#define CSO_HASH_H4546#include "pipe/p_compiler.h"4748#ifdef __cplusplus49extern "C" {50#endif515253struct cso_node {54struct cso_node *next;55void *value;56unsigned key;57};5859struct cso_hash_iter {60struct cso_hash *hash;61struct cso_node *node;62};6364struct cso_hash {65struct cso_node *fakeNext;66struct cso_node **buckets;67struct cso_node *end;68int size;69short userNumBits;70short numBits;71int numBuckets;72};7374void cso_hash_init(struct cso_hash *hash);75void cso_hash_deinit(struct cso_hash *hash);767778int cso_hash_size(struct cso_hash *hash);798081/**82* Adds a data with the given key to the hash. If entry with the given83* key is already in the hash, this current entry is instered before it84* in the collision list.85* Function returns iterator pointing to the inserted item in the hash.86*/87struct cso_hash_iter cso_hash_insert(struct cso_hash *hash, unsigned key,88void *data);89/**90* Removes the item pointed to by the current iterator from the hash.91* Note that the data itself is not erased and if it was a malloc'ed pointer92* it will have to be freed after calling this function by the callee.93* Function returns iterator pointing to the item after the removed one in94* the hash.95*/96struct cso_hash_iter cso_hash_erase(struct cso_hash *hash, struct cso_hash_iter iter);9798void *cso_hash_take(struct cso_hash *hash, unsigned key);99100101102struct cso_hash_iter cso_hash_first_node(struct cso_hash *hash);103104/**105* Returns true if a value with the given key exists in the hash106*/107bool cso_hash_contains(struct cso_hash *hash, unsigned key);108109110unsigned cso_hash_iter_key(struct cso_hash_iter iter);111112113/**114* Convenience routine to iterate over the collision list while doing a memory115* comparison to see which entry in the list is a direct copy of our template116* and returns that entry.117*/118void *cso_hash_find_data_from_template(struct cso_hash *hash,119unsigned hash_key,120void *templ,121int size);122123struct cso_node *cso_hash_data_next(struct cso_node *node);124125static inline bool126cso_hash_iter_is_null(struct cso_hash_iter iter)127{128return !iter.node || iter.node == iter.hash->end;129}130131static inline void *132cso_hash_iter_data(struct cso_hash_iter iter)133{134if (!iter.node || iter.hash->end == iter.node)135return NULL;136return iter.node->value;137}138139static inline struct cso_node **140cso_hash_find_node(struct cso_hash *hash, unsigned akey)141{142struct cso_node **node;143144if (hash->numBuckets) {145node = &hash->buckets[akey % hash->numBuckets];146assert(*node == hash->end || (*node)->next);147while (*node != hash->end && (*node)->key != akey)148node = &(*node)->next;149} else {150node = &hash->end;151}152return node;153}154155/**156* Return an iterator pointing to the first entry in the collision list.157*/158static inline struct cso_hash_iter159cso_hash_find(struct cso_hash *hash, unsigned key)160{161struct cso_node **nextNode = cso_hash_find_node(hash, key);162struct cso_hash_iter iter = {hash, *nextNode};163return iter;164}165166static inline struct cso_hash_iter167cso_hash_iter_next(struct cso_hash_iter iter)168{169struct cso_hash_iter next = {iter.hash, cso_hash_data_next(iter.node)};170return next;171}172173#ifdef __cplusplus174}175#endif176177#endif178179180