Debian Codegraph @

crt/aws-c-common/source/linked_hash_table.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/linked_hash_table.h>
6
7 static void s_element_destroy(void *value) {
8 struct aws_linked_hash_table_node *node = value;
9
10 if (node->table->user_on_value_destroy) {
11 node->table->user_on_value_destroy(node->value);
12 }
13
14 aws_linked_list_remove(&node->node);
15 aws_mem_release(node->table->allocator, node);
16 }
17
18 int aws_linked_hash_table_init(
19 struct aws_linked_hash_table *table,
20 struct aws_allocator *allocator,
21 aws_hash_fn *hash_fn,
22 aws_hash_callback_eq_fn *equals_fn,
23 aws_hash_callback_destroy_fn *destroy_key_fn,
24 aws_hash_callback_destroy_fn *destroy_value_fn,
25 size_t initial_item_count) {
26 AWS_ASSERT(table);
27 AWS_ASSERT(allocator);
28 AWS_ASSERT(hash_fn);
29 AWS_ASSERT(equals_fn);
30
31 table->allocator = allocator;
32 table->user_on_value_destroy = destroy_value_fn;
33 table->user_on_key_destroy = destroy_key_fn;
34
35 aws_linked_list_init(&table->list);
36 return aws_hash_table_init(
37 &table->table, allocator, initial_item_count, hash_fn, equals_fn, destroy_key_fn, s_element_destroy);
38 }
39
40 void aws_linked_hash_table_clean_up(struct aws_linked_hash_table *table) {
41 /* clearing the table will remove all elements. That will also deallocate
42 * any table entries we currently have. */
43 aws_hash_table_clean_up(&table->table);
44 AWS_ZERO_STRUCT(*table);
45 }
46
47 int aws_linked_hash_table_find(struct aws_linked_hash_table *table, const void *key, void **p_value) {
48
49 struct aws_hash_element *element = NULL;
50 int err_val = aws_hash_table_find(&table->table, key, &element);
51
52 if (err_val || !element) {
53 *p_value = NULL;
54 return err_val;
55 }
56
57 struct aws_linked_hash_table_node *linked_node = element->value;
58 *p_value = linked_node->value;
59
60 return AWS_OP_SUCCESS;
61 }
62
63 int aws_linked_hash_table_find_and_move_to_back(struct aws_linked_hash_table *table, const void *key, void **p_value) {
64
65 struct aws_hash_element *element = NULL;
66 int err_val = aws_hash_table_find(&table->table, key, &element);
67
68 if (err_val || !element) {
69 *p_value = NULL;
70 return err_val;
71 }
72
73 struct aws_linked_hash_table_node *linked_node = element->value;
74 *p_value = linked_node->value;
75 /* on access, remove from current place in list and move it to the back. */
76 aws_linked_hash_table_move_node_to_end_of_list(table, linked_node);
77 return AWS_OP_SUCCESS;
78 }
79
80 int aws_linked_hash_table_put(struct aws_linked_hash_table *table, const void *key, void *p_value) {
81
82 struct aws_linked_hash_table_node *node =
83 aws_mem_calloc(table->allocator, 1, sizeof(struct aws_linked_hash_table_node));
84
85 if (!node) {
86 return AWS_OP_ERR;
87 }
88
89 struct aws_hash_element *element = NULL;
90 int was_added = 0;
91 int err_val = aws_hash_table_create(&table->table, key, &element, &was_added);
92
93 if (err_val) {
94 aws_mem_release(table->allocator, node);
95 return err_val;
96 }
97
98 if (element->value) {
99 AWS_ASSERT(!was_added);
100
101 /*
102 * There's an existing element with a key that is "equal" to the submitted key. We need to destroy that
103 * existing element's value if applicable.
104 */
105 s_element_destroy(element->value);
106
107 /*
108 * We're reusing an old element. The keys might be different references but "equal" via comparison. In that
109 * case we need to destroy the key (if appropriate) and point the element to the new key. This underhanded
110 * mutation of the element is safe with respect to the hash table because the keys are "equal."
111 */
112 if (table->user_on_key_destroy && element->key != key) {
113 table->user_on_key_destroy((void *)element->key);
114 }
115
116 /*
117 * Potentially a NOOP, but under certain circumstances (when the key and value are a part of the same structure
118 * and we're overwriting the existing entry, for example), this is necessary. Equality via function does not
119 * imply equal pointers.
120 */
121 element->key = key;
122 }
123
124 node->value = p_value;
125 node->key = key;
126 node->table = table;
127 element->value = node;
128
129 aws_linked_list_push_back(&table->list, &node->node);
130
131 return AWS_OP_SUCCESS;
132 }
133
134 int aws_linked_hash_table_remove(struct aws_linked_hash_table *table, const void *key) {
135 /* allocated table memory and the linked list entry will be removed in the
136 * callback. */
137 return aws_hash_table_remove(&table->table, key, NULL, NULL);
138 }
139
140 void aws_linked_hash_table_clear(struct aws_linked_hash_table *table) {
141 /* clearing the table will remove all elements. That will also deallocate
142 * any entries we currently have. */
143 aws_hash_table_clear(&table->table);
144 }
145
146 size_t aws_linked_hash_table_get_element_count(const struct aws_linked_hash_table *table) {
147 return aws_hash_table_get_entry_count(&table->table);
148 }
149
150 void aws_linked_hash_table_move_node_to_end_of_list(
151 struct aws_linked_hash_table *table,
152 struct aws_linked_hash_table_node *node) {
153
154 aws_linked_list_remove(&node->node);
155 aws_linked_list_push_back(&table->list, &node->node);
156 }
157
158 const struct aws_linked_list *aws_linked_hash_table_get_iteration_list(const struct aws_linked_hash_table *table) {
159 return &table->list;
160 }