OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #ifndef GRPC_INTERNAL_CORE_STATISTICS_HASH_TABLE_H |
| 35 #define GRPC_INTERNAL_CORE_STATISTICS_HASH_TABLE_H |
| 36 |
| 37 #include <stddef.h> |
| 38 |
| 39 #include <grpc/support/port_platform.h> |
| 40 |
| 41 /* A chain based hash table with fixed number of buckets. |
| 42 Your probably shouldn't use this code directly. It is implemented for the |
| 43 use case in census trace store and stats store, where number of entries in |
| 44 the table is in the scale of upto several thousands, entries are added and |
| 45 removed from the table very frequently (~100k/s), the frequency of find() |
| 46 operations is roughly several times of the frequency of insert() and erase() |
| 47 Comparing to find(), the insert(), erase() and get_all_entries() operations |
| 48 are much less freqent (<1/s). |
| 49 |
| 50 Per bucket memory overhead is about (8 + sizeof(intptr_t) bytes. |
| 51 Per entry memory overhead is about (8 + 2 * sizeof(intptr_t) bytes. |
| 52 |
| 53 All functions are not thread-safe. Synchronization will be provided in the |
| 54 upper layer (in trace store and stats store). |
| 55 */ |
| 56 |
| 57 /* Opaque hash table struct */ |
| 58 typedef struct unresizable_hash_table census_ht; |
| 59 |
| 60 /* Currently, the hash_table can take two types of keys. (uint64 for trace |
| 61 store and const char* for stats store). */ |
| 62 typedef union { |
| 63 uint64_t val; |
| 64 void *ptr; |
| 65 } census_ht_key; |
| 66 |
| 67 typedef enum census_ht_key_type { |
| 68 CENSUS_HT_UINT64 = 0, |
| 69 CENSUS_HT_POINTER = 1 |
| 70 } census_ht_key_type; |
| 71 |
| 72 typedef struct census_ht_option { |
| 73 /* Type of hash key */ |
| 74 census_ht_key_type key_type; |
| 75 /* Desired number of buckets, preferably a prime number */ |
| 76 int32_t num_buckets; |
| 77 /* Fucntion to calculate uint64 hash value of the key. Only takes effect if |
| 78 key_type is POINTER. */ |
| 79 uint64_t (*hash)(const void *); |
| 80 /* Function to compare two keys, returns 0 iff equal. Only takes effect if |
| 81 key_type is POINTER */ |
| 82 int (*compare_keys)(const void *k1, const void *k2); |
| 83 /* Value deleter. NULL if no specialized delete function is needed. */ |
| 84 void (*delete_data)(void *); |
| 85 /* Key deleter. NULL if table does not own the key. (e.g. key is part of the |
| 86 value or key is not owned by the table.) */ |
| 87 void (*delete_key)(void *); |
| 88 } census_ht_option; |
| 89 |
| 90 /* Creates a hashtable with fixed number of buckets according to the settings |
| 91 specified in 'options' arg. Function pointers "hash" and "compare_keys" must |
| 92 be provided if key_type is POINTER. Asserts if fail to create. */ |
| 93 census_ht *census_ht_create(const census_ht_option *options); |
| 94 |
| 95 /* Deletes hash table instance. Frees all dynamic memory owned by ht.*/ |
| 96 void census_ht_destroy(census_ht *ht); |
| 97 |
| 98 /* Inserts the input key-val pair into hash_table. If an entry with the same key |
| 99 exists in the table, the corresponding value will be overwritten by the input |
| 100 val. */ |
| 101 void census_ht_insert(census_ht *ht, census_ht_key key, void *val); |
| 102 |
| 103 /* Returns pointer to data, returns NULL if not found. */ |
| 104 void *census_ht_find(const census_ht *ht, census_ht_key key); |
| 105 |
| 106 /* Erase hash table entry with input key. Noop if key is not found. */ |
| 107 void census_ht_erase(census_ht *ht, census_ht_key key); |
| 108 |
| 109 typedef struct census_ht_kv { |
| 110 census_ht_key k; |
| 111 void *v; |
| 112 } census_ht_kv; |
| 113 |
| 114 /* Returns an array of pointers to all values in the hash table. Order of the |
| 115 elements can be arbitrary. Sets 'num' to the size of returned array. Caller |
| 116 owns returned array. */ |
| 117 census_ht_kv *census_ht_get_all_elements(const census_ht *ht, size_t *num); |
| 118 |
| 119 /* Returns number of elements kept. */ |
| 120 size_t census_ht_get_size(const census_ht *ht); |
| 121 |
| 122 /* Functor applied on each key-value pair while iterating through entries in the |
| 123 table. The functor should not mutate data. */ |
| 124 typedef void (*census_ht_itr_cb)(census_ht_key key, const void *val_ptr, |
| 125 void *state); |
| 126 |
| 127 /* Iterates through all key-value pairs in the hash_table. The callback function |
| 128 should not invalidate data entries. */ |
| 129 uint64_t census_ht_for_all(const census_ht *ht, census_ht_itr_cb); |
| 130 |
| 131 #endif /* GRPC_INTERNAL_CORE_STATISTICS_HASH_TABLE_H */ |
OLD | NEW |