1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/transport/chttp2/transport/stream_map.h"
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27
grpc_chttp2_stream_map_init(grpc_chttp2_stream_map * map,size_t initial_capacity)28 void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map* map,
29 size_t initial_capacity) {
30 GPR_DEBUG_ASSERT(initial_capacity > 1);
31 map->keys =
32 static_cast<uint32_t*>(gpr_malloc(sizeof(uint32_t) * initial_capacity));
33 map->values =
34 static_cast<void**>(gpr_malloc(sizeof(void*) * initial_capacity));
35 map->count = 0;
36 map->free = 0;
37 map->capacity = initial_capacity;
38 }
39
grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map * map)40 void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map* map) {
41 gpr_free(map->keys);
42 gpr_free(map->values);
43 }
44
compact(uint32_t * keys,void ** values,size_t count)45 static size_t compact(uint32_t* keys, void** values, size_t count) {
46 size_t i, out;
47
48 for (i = 0, out = 0; i < count; i++) {
49 if (values[i]) {
50 keys[out] = keys[i];
51 values[out] = values[i];
52 out++;
53 }
54 }
55
56 return out;
57 }
58
grpc_chttp2_stream_map_add(grpc_chttp2_stream_map * map,uint32_t key,void * value)59 void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map* map, uint32_t key,
60 void* value) {
61 size_t count = map->count;
62 size_t capacity = map->capacity;
63 uint32_t* keys = map->keys;
64 void** values = map->values;
65
66 // The first assertion ensures that the table is monotonically increasing.
67 GPR_ASSERT(count == 0 || keys[count - 1] < key);
68 GPR_DEBUG_ASSERT(value);
69 // Asserting that the key is not already in the map can be a debug assertion.
70 // Why: we're already checking that the map elements are monotonically
71 // increasing. If we re-add a key, i.e. if the key is already present, then
72 // either it is the most recently added key in the map (in which case the
73 // first assertion fails due to key == last_key) or there is a more recently
74 // added (larger) key at the end of the map: in which case the first assertion
75 // still fails due to key < last_key.
76 GPR_DEBUG_ASSERT(grpc_chttp2_stream_map_find(map, key) == nullptr);
77
78 if (count == capacity) {
79 if (map->free > capacity / 4) {
80 count = compact(keys, values, count);
81 map->free = 0;
82 } else {
83 /* resize when less than 25% of the table is free, because compaction
84 won't help much */
85 map->capacity = capacity = 2 * capacity;
86 map->keys = keys = static_cast<uint32_t*>(
87 gpr_realloc(keys, capacity * sizeof(uint32_t)));
88 map->values = values =
89 static_cast<void**>(gpr_realloc(values, capacity * sizeof(void*)));
90 }
91 }
92
93 keys[count] = key;
94 values[count] = value;
95 map->count = count + 1;
96 }
97
98 template <bool strict_find>
find(grpc_chttp2_stream_map * map,uint32_t key)99 static void** find(grpc_chttp2_stream_map* map, uint32_t key) {
100 size_t min_idx = 0;
101 size_t max_idx = map->count;
102 size_t mid_idx;
103 uint32_t* keys = map->keys;
104 void** values = map->values;
105 uint32_t mid_key;
106
107 GPR_DEBUG_ASSERT(!strict_find || max_idx > 0);
108 if (!strict_find && max_idx == 0) return nullptr;
109
110 while (min_idx < max_idx) {
111 /* find the midpoint, avoiding overflow */
112 mid_idx = min_idx + ((max_idx - min_idx) / 2);
113 mid_key = keys[mid_idx];
114
115 if (mid_key < key) {
116 min_idx = mid_idx + 1;
117 } else if (mid_key > key) {
118 max_idx = mid_idx;
119 } else /* mid_key == key */
120 {
121 return &values[mid_idx];
122 }
123 }
124
125 GPR_DEBUG_ASSERT(!strict_find);
126 return nullptr;
127 }
128
grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map * map,uint32_t key)129 void* grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map* map, uint32_t key) {
130 void** pvalue = find<true>(map, key);
131 GPR_DEBUG_ASSERT(pvalue != nullptr);
132 void* out = *pvalue;
133 GPR_DEBUG_ASSERT(out != nullptr);
134 *pvalue = nullptr;
135 map->free++;
136 /* recognize complete emptyness and ensure we can skip
137 defragmentation later */
138 if (map->free == map->count) {
139 map->free = map->count = 0;
140 }
141 GPR_DEBUG_ASSERT(grpc_chttp2_stream_map_find(map, key) == nullptr);
142 return out;
143 }
144
grpc_chttp2_stream_map_find(grpc_chttp2_stream_map * map,uint32_t key)145 void* grpc_chttp2_stream_map_find(grpc_chttp2_stream_map* map, uint32_t key) {
146 void** pvalue = find<false>(map, key);
147 return pvalue != nullptr ? *pvalue : nullptr;
148 }
149
grpc_chttp2_stream_map_size(grpc_chttp2_stream_map * map)150 size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map* map) {
151 return map->count - map->free;
152 }
153
grpc_chttp2_stream_map_rand(grpc_chttp2_stream_map * map)154 void* grpc_chttp2_stream_map_rand(grpc_chttp2_stream_map* map) {
155 if (map->count == map->free) {
156 return nullptr;
157 }
158 if (map->free != 0) {
159 map->count = compact(map->keys, map->values, map->count);
160 map->free = 0;
161 GPR_ASSERT(map->count > 0);
162 }
163 return map->values[(static_cast<size_t>(rand())) % map->count];
164 }
165
grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map * map,void (* f)(void * user_data,uint32_t key,void * value),void * user_data)166 void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map* map,
167 void (*f)(void* user_data, uint32_t key,
168 void* value),
169 void* user_data) {
170 size_t i;
171
172 for (i = 0; i < map->count; i++) {
173 if (map->values[i]) {
174 f(user_data, map->keys[i], map->values[i]);
175 }
176 }
177 }
178