From 6a721b5b3f253b86eac3f34c66e6b2d47bb08c9e Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Fri, 14 Oct 2016 12:43:34 -0700 Subject: Rename grpc_hash_table to grpc_mdstr_hash_table. --- src/core/ext/client_config/method_config.c | 58 ++++++------ src/core/ext/client_config/method_config.h | 4 +- src/core/lib/transport/hashtable.c | 140 ---------------------------- src/core/lib/transport/hashtable.h | 82 ----------------- src/core/lib/transport/mdstr_hash_table.c | 142 +++++++++++++++++++++++++++++ src/core/lib/transport/mdstr_hash_table.h | 83 +++++++++++++++++ 6 files changed, 258 insertions(+), 251 deletions(-) delete mode 100644 src/core/lib/transport/hashtable.c delete mode 100644 src/core/lib/transport/hashtable.h create mode 100644 src/core/lib/transport/mdstr_hash_table.c create mode 100644 src/core/lib/transport/mdstr_hash_table.h (limited to 'src/core') diff --git a/src/core/ext/client_config/method_config.c b/src/core/ext/client_config/method_config.c index 3699c22810..f8a82323e7 100644 --- a/src/core/ext/client_config/method_config.c +++ b/src/core/ext/client_config/method_config.c @@ -39,7 +39,7 @@ #include #include -#include "src/core/lib/transport/hashtable.h" +#include "src/core/lib/transport/mdstr_hash_table.h" #include "src/core/lib/transport/metadata.h" // @@ -63,7 +63,8 @@ static int bool_cmp(void* v1, void* v2) { return 0; } -static grpc_hash_table_vtable bool_vtable = {gpr_free, bool_copy, bool_cmp}; +static grpc_mdstr_hash_table_vtable bool_vtable = {gpr_free, bool_copy, + bool_cmp}; // timespec vtable @@ -78,8 +79,8 @@ static int timespec_cmp(void* v1, void* v2) { return gpr_time_cmp(*(gpr_timespec*)v1, *(gpr_timespec*)v2); } -static grpc_hash_table_vtable timespec_vtable = {gpr_free, timespec_copy, - timespec_cmp}; +static grpc_mdstr_hash_table_vtable timespec_vtable = {gpr_free, timespec_copy, + timespec_cmp}; // int32 vtable @@ -98,7 +99,8 @@ static int int32_cmp(void* v1, void* v2) { return 0; } -static grpc_hash_table_vtable int32_vtable = {gpr_free, int32_copy, int32_cmp}; +static grpc_mdstr_hash_table_vtable int32_vtable = {gpr_free, int32_copy, + int32_cmp}; // Hash table keys. #define GRPC_METHOD_CONFIG_WAIT_FOR_READY "grpc.wait_for_ready" // bool @@ -109,7 +111,7 @@ static grpc_hash_table_vtable int32_vtable = {gpr_free, int32_copy, int32_cmp}; "grpc.max_response_message_bytes" // int32 struct grpc_method_config { - grpc_hash_table* table; + grpc_mdstr_hash_table* table; grpc_mdstr* wait_for_ready_key; grpc_mdstr* timeout_key; grpc_mdstr* max_request_message_bytes_key; @@ -129,7 +131,7 @@ grpc_method_config* grpc_method_config_create( grpc_mdstr_from_string(GRPC_METHOD_CONFIG_MAX_REQUEST_MESSAGE_BYTES); method_config->max_response_message_bytes_key = grpc_mdstr_from_string(GRPC_METHOD_CONFIG_MAX_RESPONSE_MESSAGE_BYTES); - grpc_hash_table_entry entries[4]; + grpc_mdstr_hash_table_entry entries[4]; size_t num_entries = 0; if (wait_for_ready != NULL) { entries[num_entries].key = method_config->wait_for_ready_key; @@ -155,17 +157,17 @@ grpc_method_config* grpc_method_config_create( entries[num_entries].vtable = &int32_vtable; ++num_entries; } - method_config->table = grpc_hash_table_create(num_entries, entries); + method_config->table = grpc_mdstr_hash_table_create(num_entries, entries); return method_config; } grpc_method_config* grpc_method_config_ref(grpc_method_config* method_config) { - grpc_hash_table_ref(method_config->table); + grpc_mdstr_hash_table_ref(method_config->table); return method_config; } void grpc_method_config_unref(grpc_method_config* method_config) { - if (grpc_hash_table_unref(method_config->table)) { + if (grpc_mdstr_hash_table_unref(method_config->table)) { GRPC_MDSTR_UNREF(method_config->wait_for_ready_key); GRPC_MDSTR_UNREF(method_config->timeout_key); GRPC_MDSTR_UNREF(method_config->max_request_message_bytes_key); @@ -176,30 +178,32 @@ void grpc_method_config_unref(grpc_method_config* method_config) { int grpc_method_config_cmp(const grpc_method_config* method_config1, const grpc_method_config* method_config2) { - return grpc_hash_table_cmp(method_config1->table, method_config2->table); + return grpc_mdstr_hash_table_cmp(method_config1->table, + method_config2->table); } const bool* grpc_method_config_get_wait_for_ready( const grpc_method_config* method_config) { - return grpc_hash_table_get(method_config->table, - method_config->wait_for_ready_key); + return grpc_mdstr_hash_table_get(method_config->table, + method_config->wait_for_ready_key); } const gpr_timespec* grpc_method_config_get_timeout( const grpc_method_config* method_config) { - return grpc_hash_table_get(method_config->table, method_config->timeout_key); + return grpc_mdstr_hash_table_get(method_config->table, + method_config->timeout_key); } const int32_t* grpc_method_config_get_max_request_message_bytes( const grpc_method_config* method_config) { - return grpc_hash_table_get(method_config->table, - method_config->max_request_message_bytes_key); + return grpc_mdstr_hash_table_get( + method_config->table, method_config->max_request_message_bytes_key); } const int32_t* grpc_method_config_get_max_response_message_bytes( const grpc_method_config* method_config) { - return grpc_hash_table_get(method_config->table, - method_config->max_response_message_bytes_key); + return grpc_mdstr_hash_table_get( + method_config->table, method_config->max_response_message_bytes_key); } // @@ -218,41 +222,41 @@ static int method_config_cmp(void* valuep1, void* valuep2) { return grpc_method_config_cmp(valuep1, valuep2); } -static const grpc_hash_table_vtable method_config_table_vtable = { +static const grpc_mdstr_hash_table_vtable method_config_table_vtable = { method_config_unref, method_config_ref, method_config_cmp}; grpc_method_config_table* grpc_method_config_table_create( size_t num_entries, grpc_method_config_table_entry* entries) { - grpc_hash_table_entry* hash_table_entries = - gpr_malloc(sizeof(grpc_hash_table_entry) * num_entries); + grpc_mdstr_hash_table_entry* hash_table_entries = + gpr_malloc(sizeof(grpc_mdstr_hash_table_entry) * num_entries); for (size_t i = 0; i < num_entries; ++i) { hash_table_entries[i].key = entries[i].method_name; hash_table_entries[i].value = entries[i].method_config; hash_table_entries[i].vtable = &method_config_table_vtable; } grpc_method_config_table* method_config_table = - grpc_hash_table_create(num_entries, hash_table_entries); + grpc_mdstr_hash_table_create(num_entries, hash_table_entries); gpr_free(hash_table_entries); return method_config_table; } grpc_method_config_table* grpc_method_config_table_ref( grpc_method_config_table* table) { - return grpc_hash_table_ref(table); + return grpc_mdstr_hash_table_ref(table); } void grpc_method_config_table_unref(grpc_method_config_table* table) { - grpc_hash_table_unref(table); + grpc_mdstr_hash_table_unref(table); } int grpc_method_config_table_cmp(const grpc_method_config_table* table1, const grpc_method_config_table* table2) { - return grpc_hash_table_cmp(table1, table2); + return grpc_mdstr_hash_table_cmp(table1, table2); } grpc_method_config* grpc_method_config_table_get_method_config( const grpc_method_config_table* table, const grpc_mdstr* path) { - grpc_method_config* method_config = grpc_hash_table_get(table, path); + grpc_method_config* method_config = grpc_mdstr_hash_table_get(table, path); // If we didn't find a match for the path, try looking for a wildcard // entry (i.e., change "/service/method" to "/service/*"). if (method_config == NULL) { @@ -265,7 +269,7 @@ grpc_method_config* grpc_method_config_table_get_method_config( buf[len + 1] = '\0'; grpc_mdstr* wildcard_path = grpc_mdstr_from_string(buf); gpr_free(buf); - method_config = grpc_hash_table_get(table, wildcard_path); + method_config = grpc_mdstr_hash_table_get(table, wildcard_path); GRPC_MDSTR_UNREF(wildcard_path); } return method_config; diff --git a/src/core/ext/client_config/method_config.h b/src/core/ext/client_config/method_config.h index d228b97948..1302ac425d 100644 --- a/src/core/ext/client_config/method_config.h +++ b/src/core/ext/client_config/method_config.h @@ -37,7 +37,7 @@ #include #include -#include "src/core/lib/transport/hashtable.h" +#include "src/core/lib/transport/mdstr_hash_table.h" #include "src/core/lib/transport/metadata.h" /// Per-method configuration. @@ -79,7 +79,7 @@ const int32_t* grpc_method_config_get_max_response_message_bytes( const grpc_method_config* method_config); /// A table of method configs. -typedef grpc_hash_table grpc_method_config_table; +typedef grpc_mdstr_hash_table grpc_method_config_table; typedef struct grpc_method_config_table_entry { /// The name is of one of the following forms: diff --git a/src/core/lib/transport/hashtable.c b/src/core/lib/transport/hashtable.c deleted file mode 100644 index a016daa0ec..0000000000 --- a/src/core/lib/transport/hashtable.c +++ /dev/null @@ -1,140 +0,0 @@ -// -// Copyright 2016, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * 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. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT -// OWNER 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. -// - -#include "src/core/lib/transport/hashtable.h" - -#include -#include - -#include -#include - -#include "src/core/lib/transport/metadata.h" - -struct grpc_hash_table { - gpr_refcount refs; - size_t num_entries; - grpc_hash_table_entry* entries; -}; - -// Helper function for insert and get operations that performs quadratic -// probing (https://en.wikipedia.org/wiki/Quadratic_probing). -static size_t grpc_hash_table_find_index(const grpc_hash_table* table, - const grpc_mdstr* key, - bool find_empty) { - for (size_t i = 0; i < table->num_entries; ++i) { - const size_t idx = (key->hash + i * i) % table->num_entries; - if (table->entries[idx].key == NULL) - return find_empty ? idx : table->num_entries; - if (table->entries[idx].key == key) return idx; - } - return table->num_entries; // Not found. -} - -static void grpc_hash_table_add(grpc_hash_table* table, grpc_mdstr* key, - void* value, - const grpc_hash_table_vtable* vtable) { - GPR_ASSERT(value != NULL); - const size_t idx = - grpc_hash_table_find_index(table, key, true /* find_empty */); - GPR_ASSERT(idx != table->num_entries); // Table should never be full. - grpc_hash_table_entry* entry = &table->entries[idx]; - entry->key = GRPC_MDSTR_REF(key); - entry->value = vtable->copy_value(value); - entry->vtable = vtable; -} - -grpc_hash_table* grpc_hash_table_create(size_t num_entries, - grpc_hash_table_entry* entries) { - grpc_hash_table* table = gpr_malloc(sizeof(*table)); - memset(table, 0, sizeof(*table)); - gpr_ref_init(&table->refs, 1); - // Quadratic probing gets best performance when the table is no more - // than half full. - table->num_entries = num_entries * 2; - const size_t entry_size = sizeof(grpc_hash_table_entry) * table->num_entries; - table->entries = gpr_malloc(entry_size); - memset(table->entries, 0, entry_size); - for (size_t i = 0; i < num_entries; ++i) { - grpc_hash_table_entry* entry = &entries[i]; - grpc_hash_table_add(table, entry->key, entry->value, entry->vtable); - } - return table; -} - -grpc_hash_table* grpc_hash_table_ref(grpc_hash_table* table) { - if (table != NULL) gpr_ref(&table->refs); - return table; -} - -int grpc_hash_table_unref(grpc_hash_table* table) { - if (table != NULL && gpr_unref(&table->refs)) { - for (size_t i = 0; i < table->num_entries; ++i) { - grpc_hash_table_entry* entry = &table->entries[i]; - if (entry->key != NULL) { - GRPC_MDSTR_UNREF(entry->key); - entry->vtable->destroy_value(entry->value); - } - } - gpr_free(table->entries); - gpr_free(table); - return 1; - } - return 0; -} - -void* grpc_hash_table_get(const grpc_hash_table* table, const grpc_mdstr* key) { - const size_t idx = - grpc_hash_table_find_index(table, key, false /* find_empty */); - if (idx == table->num_entries) return NULL; // Not found. - return table->entries[idx].value; -} - -int grpc_hash_table_cmp(const grpc_hash_table* table1, - const grpc_hash_table* table2) { - // Compare by num_entries. - if (table1->num_entries < table2->num_entries) return -1; - if (table1->num_entries > table2->num_entries) return 1; - for (size_t i = 0; i < table1->num_entries; ++i) { - grpc_hash_table_entry* e1 = &table1->entries[i]; - grpc_hash_table_entry* e2 = &table2->entries[i]; - // Compare keys by hash value. - if (e1->key->hash < e2->key->hash) return -1; - if (e1->key->hash > e2->key->hash) return 1; - // Compare by vtable (pointer equality). - if (e1->vtable < e2->vtable) return -1; - if (e1->vtable > e2->vtable) return 1; - // Compare values via vtable. - const int value_result = e1->vtable->compare_value(e1->value, e2->value); - if (value_result != 0) return value_result; - } - return 0; -} diff --git a/src/core/lib/transport/hashtable.h b/src/core/lib/transport/hashtable.h deleted file mode 100644 index d5f40a2cf7..0000000000 --- a/src/core/lib/transport/hashtable.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2016, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT - * OWNER 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 GRPC_CORE_LIB_TRANSPORT_HASHTABLE_H -#define GRPC_CORE_LIB_TRANSPORT_HASHTABLE_H - -#include "src/core/lib/transport/metadata.h" - -/** Hash table implementation. - * - * This implementation uses open addressing - * (https://en.wikipedia.org/wiki/Open_addressing) with quadratic - * probing (https://en.wikipedia.org/wiki/Quadratic_probing). - * - * The keys are \a grpc_mdstr objects. The values are arbitrary pointers - * with a common vtable. - * - * Hash tables are intentionally immutable, to avoid the need for locking. - */ - -typedef struct grpc_hash_table grpc_hash_table; - -typedef struct grpc_hash_table_vtable { - void (*destroy_value)(void* value); - void* (*copy_value)(void* value); - int (*compare_value)(void* value1, void* value2); -} grpc_hash_table_vtable; - -typedef struct grpc_hash_table_entry { - grpc_mdstr* key; - void* value; /* Must not be NULL. */ - const grpc_hash_table_vtable* vtable; -} grpc_hash_table_entry; - -/** Creates a new hash table of containing \a entries, which is an array - of length \a num_entries. - Creates its own copy of all keys and values from \a entries. */ -grpc_hash_table* grpc_hash_table_create(size_t num_entries, - grpc_hash_table_entry* entries); - -grpc_hash_table* grpc_hash_table_ref(grpc_hash_table* table); -/** Returns 1 when \a table is destroyed. */ -int grpc_hash_table_unref(grpc_hash_table* table); - -/** Returns the value from \a table associated with \a key. - Returns NULL if \a key is not found. */ -void* grpc_hash_table_get(const grpc_hash_table* table, const grpc_mdstr* key); - -/** Compares two hash tables. - The sort order is stable but undefined. */ -int grpc_hash_table_cmp(const grpc_hash_table* table1, - const grpc_hash_table* table2); - -#endif /* GRPC_CORE_LIB_TRANSPORT_HASHTABLE_H */ diff --git a/src/core/lib/transport/mdstr_hash_table.c b/src/core/lib/transport/mdstr_hash_table.c new file mode 100644 index 0000000000..4be0536dd7 --- /dev/null +++ b/src/core/lib/transport/mdstr_hash_table.c @@ -0,0 +1,142 @@ +// +// Copyright 2016, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * 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. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT +// OWNER 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. +// + +#include "src/core/lib/transport/mdstr_hash_table.h" + +#include +#include + +#include +#include + +#include "src/core/lib/transport/metadata.h" + +struct grpc_mdstr_hash_table { + gpr_refcount refs; + size_t num_entries; + grpc_mdstr_hash_table_entry* entries; +}; + +// Helper function for insert and get operations that performs quadratic +// probing (https://en.wikipedia.org/wiki/Quadratic_probing). +static size_t grpc_mdstr_hash_table_find_index( + const grpc_mdstr_hash_table* table, const grpc_mdstr* key, + bool find_empty) { + for (size_t i = 0; i < table->num_entries; ++i) { + const size_t idx = (key->hash + i * i) % table->num_entries; + if (table->entries[idx].key == NULL) + return find_empty ? idx : table->num_entries; + if (table->entries[idx].key == key) return idx; + } + return table->num_entries; // Not found. +} + +static void grpc_mdstr_hash_table_add( + grpc_mdstr_hash_table* table, grpc_mdstr* key, void* value, + const grpc_mdstr_hash_table_vtable* vtable) { + GPR_ASSERT(value != NULL); + const size_t idx = + grpc_mdstr_hash_table_find_index(table, key, true /* find_empty */); + GPR_ASSERT(idx != table->num_entries); // Table should never be full. + grpc_mdstr_hash_table_entry* entry = &table->entries[idx]; + entry->key = GRPC_MDSTR_REF(key); + entry->value = vtable->copy_value(value); + entry->vtable = vtable; +} + +grpc_mdstr_hash_table* grpc_mdstr_hash_table_create( + size_t num_entries, grpc_mdstr_hash_table_entry* entries) { + grpc_mdstr_hash_table* table = gpr_malloc(sizeof(*table)); + memset(table, 0, sizeof(*table)); + gpr_ref_init(&table->refs, 1); + // Quadratic probing gets best performance when the table is no more + // than half full. + table->num_entries = num_entries * 2; + const size_t entry_size = + sizeof(grpc_mdstr_hash_table_entry) * table->num_entries; + table->entries = gpr_malloc(entry_size); + memset(table->entries, 0, entry_size); + for (size_t i = 0; i < num_entries; ++i) { + grpc_mdstr_hash_table_entry* entry = &entries[i]; + grpc_mdstr_hash_table_add(table, entry->key, entry->value, entry->vtable); + } + return table; +} + +grpc_mdstr_hash_table* grpc_mdstr_hash_table_ref(grpc_mdstr_hash_table* table) { + if (table != NULL) gpr_ref(&table->refs); + return table; +} + +int grpc_mdstr_hash_table_unref(grpc_mdstr_hash_table* table) { + if (table != NULL && gpr_unref(&table->refs)) { + for (size_t i = 0; i < table->num_entries; ++i) { + grpc_mdstr_hash_table_entry* entry = &table->entries[i]; + if (entry->key != NULL) { + GRPC_MDSTR_UNREF(entry->key); + entry->vtable->destroy_value(entry->value); + } + } + gpr_free(table->entries); + gpr_free(table); + return 1; + } + return 0; +} + +void* grpc_mdstr_hash_table_get(const grpc_mdstr_hash_table* table, + const grpc_mdstr* key) { + const size_t idx = + grpc_mdstr_hash_table_find_index(table, key, false /* find_empty */); + if (idx == table->num_entries) return NULL; // Not found. + return table->entries[idx].value; +} + +int grpc_mdstr_hash_table_cmp(const grpc_mdstr_hash_table* table1, + const grpc_mdstr_hash_table* table2) { + // Compare by num_entries. + if (table1->num_entries < table2->num_entries) return -1; + if (table1->num_entries > table2->num_entries) return 1; + for (size_t i = 0; i < table1->num_entries; ++i) { + grpc_mdstr_hash_table_entry* e1 = &table1->entries[i]; + grpc_mdstr_hash_table_entry* e2 = &table2->entries[i]; + // Compare keys by hash value. + if (e1->key->hash < e2->key->hash) return -1; + if (e1->key->hash > e2->key->hash) return 1; + // Compare by vtable (pointer equality). + if (e1->vtable < e2->vtable) return -1; + if (e1->vtable > e2->vtable) return 1; + // Compare values via vtable. + const int value_result = e1->vtable->compare_value(e1->value, e2->value); + if (value_result != 0) return value_result; + } + return 0; +} diff --git a/src/core/lib/transport/mdstr_hash_table.h b/src/core/lib/transport/mdstr_hash_table.h new file mode 100644 index 0000000000..52e5b023db --- /dev/null +++ b/src/core/lib/transport/mdstr_hash_table.h @@ -0,0 +1,83 @@ +/* + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT + * OWNER 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 GRPC_CORE_LIB_TRANSPORT_MDSTR_HASH_TABLE_H +#define GRPC_CORE_LIB_TRANSPORT_MDSTR_HASH_TABLE_H + +#include "src/core/lib/transport/metadata.h" + +/** Hash table implementation. + * + * This implementation uses open addressing + * (https://en.wikipedia.org/wiki/Open_addressing) with quadratic + * probing (https://en.wikipedia.org/wiki/Quadratic_probing). + * + * The keys are \a grpc_mdstr objects. The values are arbitrary pointers + * with a common vtable. + * + * Hash tables are intentionally immutable, to avoid the need for locking. + */ + +typedef struct grpc_mdstr_hash_table grpc_mdstr_hash_table; + +typedef struct grpc_mdstr_hash_table_vtable { + void (*destroy_value)(void* value); + void* (*copy_value)(void* value); + int (*compare_value)(void* value1, void* value2); +} grpc_mdstr_hash_table_vtable; + +typedef struct grpc_mdstr_hash_table_entry { + grpc_mdstr* key; + void* value; /* Must not be NULL. */ + const grpc_mdstr_hash_table_vtable* vtable; +} grpc_mdstr_hash_table_entry; + +/** Creates a new hash table of containing \a entries, which is an array + of length \a num_entries. + Creates its own copy of all keys and values from \a entries. */ +grpc_mdstr_hash_table* grpc_mdstr_hash_table_create( + size_t num_entries, grpc_mdstr_hash_table_entry* entries); + +grpc_mdstr_hash_table* grpc_mdstr_hash_table_ref(grpc_mdstr_hash_table* table); +/** Returns 1 when \a table is destroyed. */ +int grpc_mdstr_hash_table_unref(grpc_mdstr_hash_table* table); + +/** Returns the value from \a table associated with \a key. + Returns NULL if \a key is not found. */ +void* grpc_mdstr_hash_table_get(const grpc_mdstr_hash_table* table, + const grpc_mdstr* key); + +/** Compares two hash tables. + The sort order is stable but undefined. */ +int grpc_mdstr_hash_table_cmp(const grpc_mdstr_hash_table* table1, + const grpc_mdstr_hash_table* table2); + +#endif /* GRPC_CORE_LIB_TRANSPORT_MDSTR_HASH_TABLE_H */ -- cgit v1.2.3