/*1* Copyright 2016 Jakub Klama <[email protected]>2* All rights reserved3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted providing 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 ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED15* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY17* 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,21* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING22* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE23* POSSIBILITY OF SUCH DAMAGE.24*25*/2627#ifndef LIB9P_HASHTABLE_H28#define LIB9P_HASHTABLE_H2930#include <pthread.h>31#include <sys/queue.h>3233struct ht {34struct ht_entry * ht_entries;35ssize_t ht_nentries;36pthread_rwlock_t ht_rwlock;37};3839struct ht_entry {40TAILQ_HEAD(, ht_item) hte_items;41};4243struct ht_item {44uint32_t hti_hash;45void * hti_data;46TAILQ_ENTRY(ht_item) hti_link;47};4849struct ht_iter {50struct ht * htit_parent;51struct ht_item * htit_curr;52struct ht_item * htit_next;53ssize_t htit_slot;54};5556#ifdef __clang__57#pragma clang diagnostic push58#pragma clang diagnostic ignored "-Wthread-safety-analysis"59#endif6061/*62* Obtain read-lock on hash table.63*/64static inline int65ht_rdlock(struct ht *h)66{6768return (pthread_rwlock_rdlock(&h->ht_rwlock));69}7071/*72* Obtain write-lock on hash table.73*/74static inline int75ht_wrlock(struct ht *h)76{7778return (pthread_rwlock_wrlock(&h->ht_rwlock));79}8081/*82* Release lock on hash table.83*/84static inline int85ht_unlock(struct ht *h)86{8788return (pthread_rwlock_unlock(&h->ht_rwlock));89}9091#ifdef __clang__92#pragma clang diagnostic pop93#endif9495void ht_init(struct ht *h, ssize_t size);96void ht_destroy(struct ht *h);97void *ht_find(struct ht *h, uint32_t hash);98void *ht_find_locked(struct ht *h, uint32_t hash);99int ht_add(struct ht *h, uint32_t hash, void *value);100int ht_remove(struct ht *h, uint32_t hash);101int ht_remove_locked(struct ht *h, uint32_t hash);102int ht_remove_at_iter(struct ht_iter *iter);103void ht_iter(struct ht *h, struct ht_iter *iter);104void *ht_next(struct ht_iter *iter);105106#endif /* LIB9P_HASHTABLE_H */107108109