/*1* Copyright 2012-2015 Samy Al Bahra.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#ifndef CK_HS_H27#define CK_HS_H2829#include <ck_cc.h>30#include <ck_malloc.h>31#include <ck_md.h>32#include <ck_pr.h>33#include <ck_stdint.h>34#include <ck_stdbool.h>35#include <ck_stddef.h>3637/*38* Indicates a single-writer many-reader workload. Mutually39* exclusive with CK_HS_MODE_MPMC40*/41#define CK_HS_MODE_SPMC 14243/*44* Indicates that values to be stored are not pointers but45* values. Allows for full precision. Mutually exclusive46* with CK_HS_MODE_OBJECT.47*/48#define CK_HS_MODE_DIRECT 24950/*51* Indicates that the values to be stored are pointers.52* Allows for space optimizations in the presence of pointer53* packing. Mutually exclusive with CK_HS_MODE_DIRECT.54*/55#define CK_HS_MODE_OBJECT 85657/*58* Indicates a delete-heavy workload. This will reduce the59* need for garbage collection at the cost of approximately60* 12% to 20% increased memory usage.61*/62#define CK_HS_MODE_DELETE 166364/* Currently unsupported. */65#define CK_HS_MODE_MPMC (void)6667/*68* Hash callback function.69*/70typedef unsigned long ck_hs_hash_cb_t(const void *, unsigned long);7172/*73* Returns pointer to object if objects are equivalent.74*/75typedef bool ck_hs_compare_cb_t(const void *, const void *);7677#if defined(CK_MD_POINTER_PACK_ENABLE) && defined(CK_MD_VMA_BITS)78#define CK_HS_PP79#define CK_HS_KEY_MASK ((1U << ((sizeof(void *) * 8) - CK_MD_VMA_BITS)) - 1)80#endif8182struct ck_hs_map;83struct ck_hs {84struct ck_malloc *m;85struct ck_hs_map *map;86unsigned int mode;87unsigned long seed;88ck_hs_hash_cb_t *hf;89ck_hs_compare_cb_t *compare;90};91typedef struct ck_hs ck_hs_t;9293struct ck_hs_stat {94unsigned long tombstones;95unsigned long n_entries;96unsigned int probe_maximum;97};9899struct ck_hs_iterator {100void **cursor;101unsigned long offset;102struct ck_hs_map *map;103};104typedef struct ck_hs_iterator ck_hs_iterator_t;105106#define CK_HS_ITERATOR_INITIALIZER { NULL, 0, NULL }107108/* Convenience wrapper to table hash function. */109#define CK_HS_HASH(T, F, K) F((K), (T)->seed)110111/* Computes the hash of n bytes of k for the specified hash map. */112static inline unsigned long113ck_hs_hash(const struct ck_hs *hs, const void *k)114{115116return hs->hf(k, hs->seed);117}118119typedef void *ck_hs_apply_fn_t(void *, void *);120bool ck_hs_apply(ck_hs_t *, unsigned long, const void *, ck_hs_apply_fn_t *, void *);121void ck_hs_iterator_init(ck_hs_iterator_t *);122bool ck_hs_next(ck_hs_t *, ck_hs_iterator_t *, void **);123bool ck_hs_next_spmc(ck_hs_t *, ck_hs_iterator_t *, void **);124bool ck_hs_move(ck_hs_t *, ck_hs_t *, ck_hs_hash_cb_t *,125ck_hs_compare_cb_t *, struct ck_malloc *);126bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *,127ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);128void ck_hs_destroy(ck_hs_t *);129void *ck_hs_get(ck_hs_t *, unsigned long, const void *);130bool ck_hs_put(ck_hs_t *, unsigned long, const void *);131bool ck_hs_put_unique(ck_hs_t *, unsigned long, const void *);132bool ck_hs_set(ck_hs_t *, unsigned long, const void *, void **);133bool ck_hs_fas(ck_hs_t *, unsigned long, const void *, void **);134void *ck_hs_remove(ck_hs_t *, unsigned long, const void *);135bool ck_hs_grow(ck_hs_t *, unsigned long);136bool ck_hs_rebuild(ck_hs_t *);137bool ck_hs_gc(ck_hs_t *, unsigned long, unsigned long);138unsigned long ck_hs_count(ck_hs_t *);139bool ck_hs_reset(ck_hs_t *);140bool ck_hs_reset_size(ck_hs_t *, unsigned long);141void ck_hs_stat(ck_hs_t *, struct ck_hs_stat *);142143#endif /* CK_HS_H */144145146