/* * Copyright 2012 Samy Al Bahra. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef _CK_HT_H #define _CK_HT_H #ifdef __x86_64__ #include #include #include #include #include #include struct ck_ht_hash { uint64_t value; }; typedef struct ck_ht_hash ck_ht_hash_t; enum ck_ht_mode { CK_HT_MODE_DIRECT, CK_HT_MODE_BYTESTRING }; struct ck_ht_entry { uintptr_t key; uintptr_t value CK_CC_PACKED; } CK_CC_ALIGNED; typedef struct ck_ht_entry ck_ht_entry_t; struct ck_ht_map; struct ck_ht { struct ck_ht_map *map; enum ck_ht_mode mode; uint64_t seed; }; typedef struct ck_ht ck_ht_t; struct ck_ht_iterator { struct ck_ht_entry *current; uint64_t offset; }; typedef struct ck_ht_iterator ck_ht_iterator_t; #define CK_HT_ITERATOR_INITIALIZER { NULL, 0 } CK_CC_INLINE static void ck_ht_iterator_init(struct ck_ht_iterator *iterator) { iterator->current = NULL; iterator->offset = 0; return; } CK_CC_INLINE static void ck_ht_entry_key_set(ck_ht_entry_t *entry, const void *key, uint16_t key_length) { entry->key = (uintptr_t)key | ((uintptr_t)key_length << 48); return; } CK_CC_INLINE static void * ck_ht_entry_key(ck_ht_entry_t *entry) { return (void *)(entry->key & (((uintptr_t)1 << 48) - 1)); } CK_CC_INLINE static uint16_t ck_ht_entry_key_length(ck_ht_entry_t *entry) { return entry->key >> 48; } CK_CC_INLINE static void * ck_ht_entry_value(ck_ht_entry_t *entry) { return (void *)(entry->value & (((uintptr_t)1 << 48) - 1)); } CK_CC_INLINE static void ck_ht_entry_set(struct ck_ht_entry *entry, ck_ht_hash_t h, const void *key, uint16_t key_length, const void *value) { entry->key = (uintptr_t)key | ((uintptr_t)key_length << 48); entry->value = (uintptr_t)value | ((uintptr_t)(h.value >> 32) << 48); return; } CK_CC_INLINE static void ck_ht_entry_set_direct(struct ck_ht_entry *entry, uintptr_t key, uintptr_t value) { entry->key = key; entry->value = value; return; } CK_CC_INLINE static uintptr_t ck_ht_entry_key_direct(ck_ht_entry_t *entry) { return entry->key; } CK_CC_INLINE static uintptr_t ck_ht_entry_value_direct(ck_ht_entry_t *entry) { return entry->value; } /* * Iteration must occur without any concurrent mutations on * the hash table. */ bool ck_ht_next(ck_ht_t *, ck_ht_iterator_t *, ck_ht_entry_t **entry); void ck_ht_hash(ck_ht_hash_t *, ck_ht_t *, const void *, uint16_t); void ck_ht_hash_direct(ck_ht_hash_t *, ck_ht_t *, uintptr_t); bool ck_ht_allocator_set(struct ck_malloc *); bool ck_ht_init(ck_ht_t *, enum ck_ht_mode, uint64_t, uint64_t); void ck_ht_destroy(ck_ht_t *); bool ck_ht_set_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *); bool ck_ht_put_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *); bool ck_ht_get_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *); bool ck_ht_grow_spmc(ck_ht_t *, uint64_t); bool ck_ht_remove_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *); bool ck_ht_reset_spmc(ck_ht_t *); uint64_t ck_ht_count(ck_ht_t *); #endif /* __x86_64__ */ #endif /* _CK_HT_H */