xref: /netbsd-src/sys/external/bsd/drm2/dist/include/drm/drm_hashtab.h (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: drm_hashtab.h,v 1.4 2021/12/18 23:45:46 riastradh Exp $	*/
2 
3 /**************************************************************************
4  *
5  * Copyright 2006 Tungsten Graphics, Inc., Bismack, ND. USA.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  *
29  **************************************************************************/
30 /*
31  * Simple open hash tab implementation.
32  *
33  * Authors:
34  * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
35  */
36 
37 #ifndef DRM_HASHTAB_H
38 #define DRM_HASHTAB_H
39 
40 #include <linux/kernel.h>
41 #include <linux/list.h>
42 #include <linux/types.h>
43 
44 #define drm_hash_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
45 
46 struct drm_hash_item {
47 	struct hlist_node head;
48 	unsigned long key;
49 };
50 
51 struct drm_open_hash {
52 	struct hlist_head *table;
53 	u8 order;
54 };
55 
56 int drm_ht_create(struct drm_open_hash *ht, unsigned int order);
57 int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item);
58 int drm_ht_just_insert_please(struct drm_open_hash *ht, struct drm_hash_item *item,
59 			      unsigned long seed, int bits, int shift,
60 			      unsigned long add);
61 int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key, struct drm_hash_item **item);
62 
63 void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key);
64 int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key);
65 int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item);
66 void drm_ht_remove(struct drm_open_hash *ht);
67 
68 /*
69  * RCU-safe interface
70  *
71  * The user of this API needs to make sure that two or more instances of the
72  * hash table manipulation functions are never run simultaneously.
73  * The lookup function drm_ht_find_item_rcu may, however, run simultaneously
74  * with any of the manipulation functions as long as it's called from within
75  * an RCU read-locked section.
76  */
77 #define drm_ht_insert_item_rcu drm_ht_insert_item
78 #define drm_ht_just_insert_please_rcu drm_ht_just_insert_please
79 #define drm_ht_remove_key_rcu drm_ht_remove_key
80 #define drm_ht_remove_item_rcu drm_ht_remove_item
81 #define drm_ht_find_item_rcu drm_ht_find_item
82 
83 #endif
84