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_HS_MODE_MPMC
42 #define CK_HS_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_HS_MODE_OBJECT.
49 #define CK_HS_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_HS_MODE_DIRECT.
56 #define CK_HS_MODE_OBJECT 8
59 * Indicates a delete-heavy workload. This will reduce the
60 * need for garbage collection at the cost of approximately
61 * 12% to 20% increased memory usage.
63 #define CK_HS_MODE_DELETE 16
65 /* Currently unsupported. */
66 #define CK_HS_MODE_MPMC (void)
69 * Hash callback function.
71 typedef unsigned long ck_hs_hash_cb_t(const void *, unsigned long);
74 * Returns pointer to object if objects are equivalent.
76 typedef bool ck_hs_compare_cb_t(const void *, const void *);
78 #if defined(CK_MD_POINTER_PACK_ENABLE) && defined(CK_MD_VMA_BITS)
80 #define CK_HS_KEY_MASK ((1U << ((sizeof(void *) * 8) - CK_MD_VMA_BITS)) - 1)
86 struct ck_hs_map *map;
90 ck_hs_compare_cb_t *compare;
92 typedef struct ck_hs ck_hs_t;
95 unsigned long tombstones;
96 unsigned long n_entries;
97 unsigned int probe_maximum;
100 struct ck_hs_iterator {
102 unsigned long offset;
104 typedef struct ck_hs_iterator ck_hs_iterator_t;
106 #define CK_HS_ITERATOR_INITIALIZER { NULL, 0 }
108 /* Convenience wrapper to table hash function. */
109 #define CK_HS_HASH(T, F, K) F((K), (T)->seed)
111 typedef void *ck_hs_apply_fn_t(void *, void *);
112 bool ck_hs_apply(ck_hs_t *, unsigned long, const void *, ck_hs_apply_fn_t *, void *);
113 void ck_hs_iterator_init(ck_hs_iterator_t *);
114 bool ck_hs_next(ck_hs_t *, ck_hs_iterator_t *, void **);
115 bool ck_hs_move(ck_hs_t *, ck_hs_t *, ck_hs_hash_cb_t *,
116 ck_hs_compare_cb_t *, struct ck_malloc *);
117 bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *,
118 ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);
119 void ck_hs_destroy(ck_hs_t *);
120 void *ck_hs_get(ck_hs_t *, unsigned long, const void *);
121 bool ck_hs_put(ck_hs_t *, unsigned long, const void *);
122 bool ck_hs_put_unique(ck_hs_t *, unsigned long, const void *);
123 bool ck_hs_set(ck_hs_t *, unsigned long, const void *, void **);
124 bool ck_hs_fas(ck_hs_t *, unsigned long, const void *, void **);
125 void *ck_hs_remove(ck_hs_t *, unsigned long, const void *);
126 bool ck_hs_grow(ck_hs_t *, unsigned long);
127 bool ck_hs_rebuild(ck_hs_t *);
128 bool ck_hs_gc(ck_hs_t *, unsigned long, unsigned long);
129 unsigned long ck_hs_count(ck_hs_t *);
130 bool ck_hs_reset(ck_hs_t *);
131 bool ck_hs_reset_size(ck_hs_t *, unsigned long);
132 void ck_hs_stat(ck_hs_t *, struct ck_hs_stat *);