2 * Copyright 2012-2015 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include <ck_malloc.h>
34 #include <ck_stdint.h>
35 #include <ck_stdbool.h>
36 #include <ck_stddef.h>
39 * Indicates a single-writer many-reader workload. Mutually
40 * exclusive with CK_RHS_MODE_MPMC
42 #define CK_RHS_MODE_SPMC 1
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.
49 #define CK_RHS_MODE_DIRECT 2
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.
56 #define CK_RHS_MODE_OBJECT 8
59 * Indicated that the load is read-mostly, so get should be optimized
62 #define CK_RHS_MODE_READ_MOSTLY 16
64 /* Currently unsupported. */
65 #define CK_RHS_MODE_MPMC (void)
68 * Hash callback function.
70 typedef unsigned long ck_rhs_hash_cb_t(const void *, unsigned long);
73 * Returns pointer to object if objects are equivalent.
75 typedef bool ck_rhs_compare_cb_t(const void *, const void *);
77 #if defined(CK_MD_POINTER_PACK_ENABLE) && defined(CK_MD_VMA_BITS)
79 #define CK_RHS_KEY_MASK ((1U << ((sizeof(void *) * 8) - CK_MD_VMA_BITS)) - 1)
85 struct ck_rhs_map *map;
87 unsigned int load_factor;
90 ck_rhs_compare_cb_t *compare;
92 typedef struct ck_rhs ck_rhs_t;
95 unsigned long n_entries;
96 unsigned int probe_maximum;
99 struct ck_rhs_iterator {
101 unsigned long offset;
103 typedef struct ck_rhs_iterator ck_rhs_iterator_t;
105 #define CK_RHS_ITERATOR_INITIALIZER { NULL, 0 }
107 /* Convenience wrapper to table hash function. */
108 #define CK_RHS_HASH(T, F, K) F((K), (T)->seed)
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);
134 #endif /* CK_RHS_H */