| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup genericadt
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_CONC_HASH_TABLE_H_
|
|---|
| 36 | #define KERN_CONC_HASH_TABLE_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <stdint.h>
|
|---|
| 39 | #include <adt/list.h>
|
|---|
| 40 | #include <synch/rcu_types.h>
|
|---|
| 41 | #include <macros.h>
|
|---|
| 42 | #include <synch/workqueue.h>
|
|---|
| 43 |
|
|---|
| 44 | typedef uintptr_t cht_ptr_t;
|
|---|
| 45 |
|
|---|
| 46 | /** Concurrent hash table node link. */
|
|---|
| 47 | typedef struct cht_link {
|
|---|
| 48 | /* Must be placed first.
|
|---|
| 49 | *
|
|---|
| 50 | * The function pointer (rcu_link.func) is used to store the item's
|
|---|
| 51 | * memoized hash.
|
|---|
| 52 | */
|
|---|
| 53 | union {
|
|---|
| 54 | rcu_item_t rcu_link;
|
|---|
| 55 | size_t hash;
|
|---|
| 56 | };
|
|---|
| 57 | /** Link to the next item in the bucket including any marks. */
|
|---|
| 58 | cht_ptr_t link;
|
|---|
| 59 | } cht_link_t;
|
|---|
| 60 |
|
|---|
| 61 | /** Set of operations for a concurrent hash table. */
|
|---|
| 62 | typedef struct cht_ops {
|
|---|
| 63 | size_t (*hash)(const cht_link_t *item);
|
|---|
| 64 | size_t (*key_hash)(void *key);
|
|---|
| 65 | bool (*equal)(const cht_link_t *item1, const cht_link_t *item2);
|
|---|
| 66 | bool (*key_equal)(void *key, const cht_link_t *item);
|
|---|
| 67 | void (*remove_callback)(cht_link_t *item);
|
|---|
| 68 | } cht_ops_t;
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | typedef struct cht_buckets {
|
|---|
| 72 | size_t order;
|
|---|
| 73 | cht_ptr_t head[1];
|
|---|
| 74 | } cht_buckets_t;
|
|---|
| 75 |
|
|---|
| 76 | /** Concurrent hash table structure. */
|
|---|
| 77 | typedef struct {
|
|---|
| 78 | cht_ops_t *op;
|
|---|
| 79 |
|
|---|
| 80 | cht_buckets_t *b;
|
|---|
| 81 | cht_buckets_t *new_b;
|
|---|
| 82 | size_t invalid_hash;
|
|---|
| 83 |
|
|---|
| 84 | size_t min_order;
|
|---|
| 85 | size_t max_load;
|
|---|
| 86 | work_t resize_work;
|
|---|
| 87 | atomic_t resize_reqs;
|
|---|
| 88 |
|
|---|
| 89 | atomic_t item_cnt;
|
|---|
| 90 | } cht_t;
|
|---|
| 91 |
|
|---|
| 92 | #define cht_get_inst(item, type, member) \
|
|---|
| 93 | member_to_inst((item), type, member)
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 | #define cht_read_lock() rcu_read_lock()
|
|---|
| 97 | #define cht_read_unlock() rcu_read_unlock()
|
|---|
| 98 |
|
|---|
| 99 | extern bool cht_create(cht_t *h, size_t init_size, size_t min_size,
|
|---|
| 100 | size_t max_load, cht_ops_t *op);
|
|---|
| 101 | extern void cht_destroy(cht_t *h);
|
|---|
| 102 |
|
|---|
| 103 | extern cht_link_t *cht_find(cht_t *h, void *key);
|
|---|
| 104 | extern cht_link_t *cht_find_lazy(cht_t *h, void *key);
|
|---|
| 105 | extern cht_link_t *cht_find_next(cht_t *h, const cht_link_t *item);
|
|---|
| 106 | extern cht_link_t *cht_find_next_lazy(cht_t *h, const cht_link_t *item);
|
|---|
| 107 |
|
|---|
| 108 | extern void cht_insert(cht_t *h, cht_link_t *item);
|
|---|
| 109 | extern bool cht_insert_unique(cht_t *h, cht_link_t *item);
|
|---|
| 110 | extern size_t cht_remove_key(cht_t *h, void *key);
|
|---|
| 111 | extern bool cht_remove_item(cht_t *h, cht_link_t *item);
|
|---|
| 112 |
|
|---|
| 113 | #endif
|
|---|
| 114 |
|
|---|
| 115 | /** @}
|
|---|
| 116 | */
|
|---|