Home | History | Annotate | Line # | Download | only in drm
drm_hashtab.c revision 1.1
      1  1.1  riastrad /**************************************************************************
      2  1.1  riastrad  *
      3  1.1  riastrad  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
      4  1.1  riastrad  * All Rights Reserved.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the
      8  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
      9  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     10  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     11  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     12  1.1  riastrad  * the following conditions:
     13  1.1  riastrad  *
     14  1.1  riastrad  * The above copyright notice and this permission notice (including the
     15  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     16  1.1  riastrad  * of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     21  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     22  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     23  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     24  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  *
     27  1.1  riastrad  **************************************************************************/
     28  1.1  riastrad /*
     29  1.1  riastrad  * Simple open hash tab implementation.
     30  1.1  riastrad  *
     31  1.1  riastrad  * Authors:
     32  1.1  riastrad  * Thomas Hellstrm <thomas-at-tungstengraphics-dot-com>
     33  1.1  riastrad  */
     34  1.1  riastrad 
     35  1.1  riastrad #include <drm/drmP.h>
     36  1.1  riastrad #include <drm/drm_hashtab.h>
     37  1.1  riastrad #include <linux/hash.h>
     38  1.1  riastrad #include <linux/slab.h>
     39  1.1  riastrad #include <linux/export.h>
     40  1.1  riastrad 
     41  1.1  riastrad int drm_ht_create(struct drm_open_hash *ht, unsigned int order)
     42  1.1  riastrad {
     43  1.1  riastrad 	unsigned int size = 1 << order;
     44  1.1  riastrad 
     45  1.1  riastrad 	ht->order = order;
     46  1.1  riastrad 	ht->table = NULL;
     47  1.1  riastrad 	if (size <= PAGE_SIZE / sizeof(*ht->table))
     48  1.1  riastrad 		ht->table = kcalloc(size, sizeof(*ht->table), GFP_KERNEL);
     49  1.1  riastrad 	else
     50  1.1  riastrad 		ht->table = vzalloc(size*sizeof(*ht->table));
     51  1.1  riastrad 	if (!ht->table) {
     52  1.1  riastrad 		DRM_ERROR("Out of memory for hash table\n");
     53  1.1  riastrad 		return -ENOMEM;
     54  1.1  riastrad 	}
     55  1.1  riastrad 	return 0;
     56  1.1  riastrad }
     57  1.1  riastrad EXPORT_SYMBOL(drm_ht_create);
     58  1.1  riastrad 
     59  1.1  riastrad void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key)
     60  1.1  riastrad {
     61  1.1  riastrad 	struct drm_hash_item *entry;
     62  1.1  riastrad 	struct hlist_head *h_list;
     63  1.1  riastrad 	struct hlist_node *list;
     64  1.1  riastrad 	unsigned int hashed_key;
     65  1.1  riastrad 	int count = 0;
     66  1.1  riastrad 
     67  1.1  riastrad 	hashed_key = hash_long(key, ht->order);
     68  1.1  riastrad 	DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key);
     69  1.1  riastrad 	h_list = &ht->table[hashed_key];
     70  1.1  riastrad 	hlist_for_each_entry(entry, list, h_list, head)
     71  1.1  riastrad 		DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key);
     72  1.1  riastrad }
     73  1.1  riastrad 
     74  1.1  riastrad static struct hlist_node *drm_ht_find_key(struct drm_open_hash *ht,
     75  1.1  riastrad 					  unsigned long key)
     76  1.1  riastrad {
     77  1.1  riastrad 	struct drm_hash_item *entry;
     78  1.1  riastrad 	struct hlist_head *h_list;
     79  1.1  riastrad 	struct hlist_node *list;
     80  1.1  riastrad 	unsigned int hashed_key;
     81  1.1  riastrad 
     82  1.1  riastrad 	hashed_key = hash_long(key, ht->order);
     83  1.1  riastrad 	h_list = &ht->table[hashed_key];
     84  1.1  riastrad 	hlist_for_each_entry(entry, list, h_list, head) {
     85  1.1  riastrad 		if (entry->key == key)
     86  1.1  riastrad 			return list;
     87  1.1  riastrad 		if (entry->key > key)
     88  1.1  riastrad 			break;
     89  1.1  riastrad 	}
     90  1.1  riastrad 	return NULL;
     91  1.1  riastrad }
     92  1.1  riastrad 
     93  1.1  riastrad static struct hlist_node *drm_ht_find_key_rcu(struct drm_open_hash *ht,
     94  1.1  riastrad 					      unsigned long key)
     95  1.1  riastrad {
     96  1.1  riastrad 	struct drm_hash_item *entry;
     97  1.1  riastrad 	struct hlist_head *h_list;
     98  1.1  riastrad 	struct hlist_node *list;
     99  1.1  riastrad 	unsigned int hashed_key;
    100  1.1  riastrad 
    101  1.1  riastrad 	hashed_key = hash_long(key, ht->order);
    102  1.1  riastrad 	h_list = &ht->table[hashed_key];
    103  1.1  riastrad 	hlist_for_each_entry_rcu(entry, list, h_list, head) {
    104  1.1  riastrad 		if (entry->key == key)
    105  1.1  riastrad 			return list;
    106  1.1  riastrad 		if (entry->key > key)
    107  1.1  riastrad 			break;
    108  1.1  riastrad 	}
    109  1.1  riastrad 	return NULL;
    110  1.1  riastrad }
    111  1.1  riastrad 
    112  1.1  riastrad int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item)
    113  1.1  riastrad {
    114  1.1  riastrad 	struct drm_hash_item *entry;
    115  1.1  riastrad 	struct hlist_head *h_list;
    116  1.1  riastrad 	struct hlist_node *list, *parent;
    117  1.1  riastrad 	unsigned int hashed_key;
    118  1.1  riastrad 	unsigned long key = item->key;
    119  1.1  riastrad 
    120  1.1  riastrad 	hashed_key = hash_long(key, ht->order);
    121  1.1  riastrad 	h_list = &ht->table[hashed_key];
    122  1.1  riastrad 	parent = NULL;
    123  1.1  riastrad 	hlist_for_each_entry(entry, list, h_list, head) {
    124  1.1  riastrad 		if (entry->key == key)
    125  1.1  riastrad 			return -EINVAL;
    126  1.1  riastrad 		if (entry->key > key)
    127  1.1  riastrad 			break;
    128  1.1  riastrad 		parent = list;
    129  1.1  riastrad 	}
    130  1.1  riastrad 	if (parent) {
    131  1.1  riastrad 		hlist_add_after_rcu(parent, &item->head);
    132  1.1  riastrad 	} else {
    133  1.1  riastrad 		hlist_add_head_rcu(&item->head, h_list);
    134  1.1  riastrad 	}
    135  1.1  riastrad 	return 0;
    136  1.1  riastrad }
    137  1.1  riastrad EXPORT_SYMBOL(drm_ht_insert_item);
    138  1.1  riastrad 
    139  1.1  riastrad /*
    140  1.1  riastrad  * Just insert an item and return any "bits" bit key that hasn't been
    141  1.1  riastrad  * used before.
    142  1.1  riastrad  */
    143  1.1  riastrad int drm_ht_just_insert_please(struct drm_open_hash *ht, struct drm_hash_item *item,
    144  1.1  riastrad 			      unsigned long seed, int bits, int shift,
    145  1.1  riastrad 			      unsigned long add)
    146  1.1  riastrad {
    147  1.1  riastrad 	int ret;
    148  1.1  riastrad 	unsigned long mask = (1 << bits) - 1;
    149  1.1  riastrad 	unsigned long first, unshifted_key;
    150  1.1  riastrad 
    151  1.1  riastrad 	unshifted_key = hash_long(seed, bits);
    152  1.1  riastrad 	first = unshifted_key;
    153  1.1  riastrad 	do {
    154  1.1  riastrad 		item->key = (unshifted_key << shift) + add;
    155  1.1  riastrad 		ret = drm_ht_insert_item(ht, item);
    156  1.1  riastrad 		if (ret)
    157  1.1  riastrad 			unshifted_key = (unshifted_key + 1) & mask;
    158  1.1  riastrad 	} while(ret && (unshifted_key != first));
    159  1.1  riastrad 
    160  1.1  riastrad 	if (ret) {
    161  1.1  riastrad 		DRM_ERROR("Available key bit space exhausted\n");
    162  1.1  riastrad 		return -EINVAL;
    163  1.1  riastrad 	}
    164  1.1  riastrad 	return 0;
    165  1.1  riastrad }
    166  1.1  riastrad EXPORT_SYMBOL(drm_ht_just_insert_please);
    167  1.1  riastrad 
    168  1.1  riastrad int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key,
    169  1.1  riastrad 		     struct drm_hash_item **item)
    170  1.1  riastrad {
    171  1.1  riastrad 	struct hlist_node *list;
    172  1.1  riastrad 
    173  1.1  riastrad 	list = drm_ht_find_key_rcu(ht, key);
    174  1.1  riastrad 	if (!list)
    175  1.1  riastrad 		return -EINVAL;
    176  1.1  riastrad 
    177  1.1  riastrad 	*item = hlist_entry(list, struct drm_hash_item, head);
    178  1.1  riastrad 	return 0;
    179  1.1  riastrad }
    180  1.1  riastrad EXPORT_SYMBOL(drm_ht_find_item);
    181  1.1  riastrad 
    182  1.1  riastrad int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key)
    183  1.1  riastrad {
    184  1.1  riastrad 	struct hlist_node *list;
    185  1.1  riastrad 
    186  1.1  riastrad 	list = drm_ht_find_key(ht, key);
    187  1.1  riastrad 	if (list) {
    188  1.1  riastrad 		hlist_del_init_rcu(list);
    189  1.1  riastrad 		return 0;
    190  1.1  riastrad 	}
    191  1.1  riastrad 	return -EINVAL;
    192  1.1  riastrad }
    193  1.1  riastrad 
    194  1.1  riastrad int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item)
    195  1.1  riastrad {
    196  1.1  riastrad 	hlist_del_init_rcu(&item->head);
    197  1.1  riastrad 	return 0;
    198  1.1  riastrad }
    199  1.1  riastrad EXPORT_SYMBOL(drm_ht_remove_item);
    200  1.1  riastrad 
    201  1.1  riastrad void drm_ht_remove(struct drm_open_hash *ht)
    202  1.1  riastrad {
    203  1.1  riastrad 	if (ht->table) {
    204  1.1  riastrad 		if ((PAGE_SIZE / sizeof(*ht->table)) >> ht->order)
    205  1.1  riastrad 			kfree(ht->table);
    206  1.1  riastrad 		else
    207  1.1  riastrad 			vfree(ht->table);
    208  1.1  riastrad 		ht->table = NULL;
    209  1.1  riastrad 	}
    210  1.1  riastrad }
    211  1.1  riastrad EXPORT_SYMBOL(drm_ht_remove);
    212