/*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_RHS_H27#define CK_RHS_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_RHS_MODE_MPMC40*/41#define CK_RHS_MODE_SPMC 14243/*44* Indicates that values to be stored are not pointers but45* values. Allows for full precision. Mutually exclusive46* with CK_RHS_MODE_OBJECT.47*/48#define CK_RHS_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_RHS_MODE_DIRECT.54*/55#define CK_RHS_MODE_OBJECT 85657/*58* Indicated that the load is read-mostly, so get should be optimized59* over put and delete60*/61#define CK_RHS_MODE_READ_MOSTLY 166263/* Currently unsupported. */64#define CK_RHS_MODE_MPMC (void)6566/*67* Hash callback function.68*/69typedef unsigned long ck_rhs_hash_cb_t(const void *, unsigned long);7071/*72* Returns pointer to object if objects are equivalent.73*/74typedef bool ck_rhs_compare_cb_t(const void *, const void *);7576#if defined(CK_MD_POINTER_PACK_ENABLE) && defined(CK_MD_VMA_BITS)77#define CK_RHS_PP78#define CK_RHS_KEY_MASK ((1U << ((sizeof(void *) * 8) - CK_MD_VMA_BITS)) - 1)79#endif8081struct ck_rhs_map;82struct ck_rhs {83struct ck_malloc *m;84struct ck_rhs_map *map;85unsigned int mode;86unsigned int load_factor;87unsigned long seed;88ck_rhs_hash_cb_t *hf;89ck_rhs_compare_cb_t *compare;90};91typedef struct ck_rhs ck_rhs_t;9293struct ck_rhs_stat {94unsigned long n_entries;95unsigned int probe_maximum;96};9798struct ck_rhs_iterator {99void **cursor;100unsigned long offset;101};102typedef struct ck_rhs_iterator ck_rhs_iterator_t;103104#define CK_RHS_ITERATOR_INITIALIZER { NULL, 0 }105106/* Convenience wrapper to table hash function. */107#define CK_RHS_HASH(T, F, K) F((K), (T)->seed)108109typedef void *ck_rhs_apply_fn_t(void *, void *);110bool ck_rhs_apply(ck_rhs_t *, unsigned long, const void *, ck_rhs_apply_fn_t *, void *);111void ck_rhs_iterator_init(ck_rhs_iterator_t *);112bool ck_rhs_next(ck_rhs_t *, ck_rhs_iterator_t *, void **);113bool ck_rhs_move(ck_rhs_t *, ck_rhs_t *, ck_rhs_hash_cb_t *,114ck_rhs_compare_cb_t *, struct ck_malloc *);115bool ck_rhs_init(ck_rhs_t *, unsigned int, ck_rhs_hash_cb_t *,116ck_rhs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);117void ck_rhs_destroy(ck_rhs_t *);118void *ck_rhs_get(ck_rhs_t *, unsigned long, const void *);119bool ck_rhs_put(ck_rhs_t *, unsigned long, const void *);120bool ck_rhs_put_unique(ck_rhs_t *, unsigned long, const void *);121bool ck_rhs_set(ck_rhs_t *, unsigned long, const void *, void **);122bool ck_rhs_fas(ck_rhs_t *, unsigned long, const void *, void **);123void *ck_rhs_remove(ck_rhs_t *, unsigned long, const void *);124bool ck_rhs_grow(ck_rhs_t *, unsigned long);125bool ck_rhs_rebuild(ck_rhs_t *);126bool ck_rhs_gc(ck_rhs_t *);127unsigned long ck_rhs_count(ck_rhs_t *);128bool ck_rhs_reset(ck_rhs_t *);129bool ck_rhs_reset_size(ck_rhs_t *, unsigned long);130void ck_rhs_stat(ck_rhs_t *, struct ck_rhs_stat *);131bool ck_rhs_set_load_factor(ck_rhs_t *, unsigned int);132133#endif /* CK_RHS_H */134135136