xref: /freebsd/sys/contrib/ck/include/ck_rhs.h (revision 675aae732d3dd2ec0f12f39df3a1fbfe693a4ebd)
1 /*
2  * Copyright 2012-2015 Samy Al Bahra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef CK_RHS_H
28 #define CK_RHS_H
29 
30 #include <ck_cc.h>
31 #include <ck_malloc.h>
32 #include <ck_md.h>
33 #include <ck_pr.h>
34 #include <ck_stdint.h>
35 #include <ck_stdbool.h>
36 #include <ck_stddef.h>
37 
38 /*
39  * Indicates a single-writer many-reader workload. Mutually
40  * exclusive with CK_RHS_MODE_MPMC
41  */
42 #define CK_RHS_MODE_SPMC		1
43 
44 /*
45  * Indicates that values to be stored are not pointers but
46  * values. Allows for full precision. Mutually exclusive
47  * with CK_RHS_MODE_OBJECT.
48  */
49 #define CK_RHS_MODE_DIRECT	2
50 
51 /*
52  * Indicates that the values to be stored are pointers.
53  * Allows for space optimizations in the presence of pointer
54  * packing. Mutually exclusive with CK_RHS_MODE_DIRECT.
55  */
56 #define CK_RHS_MODE_OBJECT	8
57 
58 /*
59  * Indicated that the load is read-mostly, so get should be optimized
60  * over put and delete
61  */
62 #define CK_RHS_MODE_READ_MOSTLY	16
63 
64 /* Currently unsupported. */
65 #define CK_RHS_MODE_MPMC    (void)
66 
67 /*
68  * Hash callback function.
69  */
70 typedef unsigned long ck_rhs_hash_cb_t(const void *, unsigned long);
71 
72 /*
73  * Returns pointer to object if objects are equivalent.
74  */
75 typedef bool ck_rhs_compare_cb_t(const void *, const void *);
76 
77 #if defined(CK_MD_POINTER_PACK_ENABLE) && defined(CK_MD_VMA_BITS)
78 #define CK_RHS_PP
79 #define CK_RHS_KEY_MASK ((1U << ((sizeof(void *) * 8) - CK_MD_VMA_BITS)) - 1)
80 #endif
81 
82 struct ck_rhs_map;
83 struct ck_rhs {
84 	struct ck_malloc *m;
85 	struct ck_rhs_map *map;
86 	unsigned int mode;
87 	unsigned int load_factor;
88 	unsigned long seed;
89 	ck_rhs_hash_cb_t *hf;
90 	ck_rhs_compare_cb_t *compare;
91 };
92 typedef struct ck_rhs ck_rhs_t;
93 
94 struct ck_rhs_stat {
95 	unsigned long n_entries;
96 	unsigned int probe_maximum;
97 };
98 
99 struct ck_rhs_iterator {
100 	void **cursor;
101 	unsigned long offset;
102 };
103 typedef struct ck_rhs_iterator ck_rhs_iterator_t;
104 
105 #define CK_RHS_ITERATOR_INITIALIZER { NULL, 0 }
106 
107 /* Convenience wrapper to table hash function. */
108 #define CK_RHS_HASH(T, F, K) F((K), (T)->seed)
109 
110 typedef void *ck_rhs_apply_fn_t(void *, void *);
111 bool ck_rhs_apply(ck_rhs_t *, unsigned long, const void *, ck_rhs_apply_fn_t *, void *);
112 void ck_rhs_iterator_init(ck_rhs_iterator_t *);
113 bool ck_rhs_next(ck_rhs_t *, ck_rhs_iterator_t *, void **);
114 bool ck_rhs_move(ck_rhs_t *, ck_rhs_t *, ck_rhs_hash_cb_t *,
115     ck_rhs_compare_cb_t *, struct ck_malloc *);
116 bool ck_rhs_init(ck_rhs_t *, unsigned int, ck_rhs_hash_cb_t *,
117     ck_rhs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);
118 void ck_rhs_destroy(ck_rhs_t *);
119 void *ck_rhs_get(ck_rhs_t *, unsigned long, const void *);
120 bool ck_rhs_put(ck_rhs_t *, unsigned long, const void *);
121 bool ck_rhs_put_unique(ck_rhs_t *, unsigned long, const void *);
122 bool ck_rhs_set(ck_rhs_t *, unsigned long, const void *, void **);
123 bool ck_rhs_fas(ck_rhs_t *, unsigned long, const void *, void **);
124 void *ck_rhs_remove(ck_rhs_t *, unsigned long, const void *);
125 bool ck_rhs_grow(ck_rhs_t *, unsigned long);
126 bool ck_rhs_rebuild(ck_rhs_t *);
127 bool ck_rhs_gc(ck_rhs_t *);
128 unsigned long ck_rhs_count(ck_rhs_t *);
129 bool ck_rhs_reset(ck_rhs_t *);
130 bool ck_rhs_reset_size(ck_rhs_t *, unsigned long);
131 void ck_rhs_stat(ck_rhs_t *, struct ck_rhs_stat *);
132 bool ck_rhs_set_load_factor(ck_rhs_t *, unsigned int);
133 
134 #endif /* CK_RHS_H */
135