aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/slice/slice_hash_table.h
blob: f86f25ea7c1b7e52cd0fb7a18967f673c74cb828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright 2016 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GRPC_CORE_LIB_SLICE_SLICE_HASH_TABLE_H
#define GRPC_CORE_LIB_SLICE_SLICE_HASH_TABLE_H

#include "src/core/lib/transport/metadata.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Hash table implementation.
 *
 * This implementation uses open addressing
 * (https://en.wikipedia.org/wiki/Open_addressing) with linear
 * probing (https://en.wikipedia.org/wiki/Linear_probing).
 *
 * The keys are \a grpc_slice objects.  The values are arbitrary pointers
 * with a common destroy function.
 *
 * Hash tables are intentionally immutable, to avoid the need for locking.
 */

typedef struct grpc_slice_hash_table grpc_slice_hash_table;

typedef struct grpc_slice_hash_table_entry {
  grpc_slice key;
  void* value; /* Must not be NULL. */
} grpc_slice_hash_table_entry;

/** Creates a new hash table of containing \a entries, which is an array
    of length \a num_entries.  Takes ownership of all keys and values in \a
    entries.  Values will be cleaned up via \a destroy_value(). If not NULL, \a
    value_cmp will be used to compare values in the context of \a
    grpc_slice_hash_table_cmp. If NULL, raw pointer (\a GPR_ICMP) comparison
    will be used. */
grpc_slice_hash_table* grpc_slice_hash_table_create(
    size_t num_entries, grpc_slice_hash_table_entry* entries,
    void (*destroy_value)(grpc_exec_ctx* exec_ctx, void* value),
    int (*value_cmp)(void* a, void* b));

grpc_slice_hash_table* grpc_slice_hash_table_ref(grpc_slice_hash_table* table);
void grpc_slice_hash_table_unref(grpc_exec_ctx* exec_ctx,
                                 grpc_slice_hash_table* table);

/** Returns the value from \a table associated with \a key.
    Returns NULL if \a key is not found. */
void* grpc_slice_hash_table_get(const grpc_slice_hash_table* table,
                                const grpc_slice key);

/** Compares \a a vs. \a b.
 * A table is considered "smaller" (resp. "greater") if:
 *  - GPR_ICMP(a->value_cmp, b->value_cmp) < 1 (resp. > 1),
 *  - else, it contains fewer (resp. more) entries,
 *  - else, if strcmp(a_key, b_key) < 1 (resp. > 1),
 *  - else, if value_cmp(a_value, b_value) < 1 (resp. > 1). */
int grpc_slice_hash_table_cmp(const grpc_slice_hash_table* a,
                              const grpc_slice_hash_table* b);

#ifdef __cplusplus
}
#endif

#endif /* GRPC_CORE_LIB_SLICE_SLICE_HASH_TABLE_H */