Line data Source code
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 : #include "src/core/transport/chttp2/stream_map.h"
35 :
36 : #include <string.h>
37 :
38 : #include <grpc/support/alloc.h>
39 : #include <grpc/support/log.h>
40 : #include <grpc/support/useful.h>
41 :
42 12174 : void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map,
43 : size_t initial_capacity) {
44 12174 : GPR_ASSERT(initial_capacity > 1);
45 12174 : map->keys = gpr_malloc(sizeof(gpr_uint32) * initial_capacity);
46 12174 : map->values = gpr_malloc(sizeof(void *) * initial_capacity);
47 12172 : map->count = 0;
48 12172 : map->free = 0;
49 12172 : map->capacity = initial_capacity;
50 12172 : }
51 :
52 12027 : void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map *map) {
53 12027 : gpr_free(map->keys);
54 12027 : gpr_free(map->values);
55 12027 : }
56 :
57 26624 : static size_t compact(gpr_uint32 *keys, void **values, size_t count) {
58 : size_t i, out;
59 :
60 3123096 : for (i = 0, out = 0; i < count; i++) {
61 3096472 : if (values[i]) {
62 1406679 : keys[out] = keys[i];
63 1406679 : values[out] = values[i];
64 1406679 : out++;
65 : }
66 : }
67 :
68 26624 : return out;
69 : }
70 :
71 5226241 : void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, gpr_uint32 key,
72 : void *value) {
73 5226241 : size_t count = map->count;
74 5226241 : size_t capacity = map->capacity;
75 5226241 : gpr_uint32 *keys = map->keys;
76 5226241 : void **values = map->values;
77 :
78 5226241 : GPR_ASSERT(count == 0 || keys[count - 1] < key);
79 5226241 : GPR_ASSERT(value);
80 :
81 5226241 : if (count == capacity) {
82 27648 : if (map->free > capacity / 4) {
83 26624 : count = compact(keys, values, count);
84 26624 : map->free = 0;
85 : } else {
86 : /* resize when less than 25% of the table is free, because compaction
87 : won't help much */
88 1024 : map->capacity = capacity = 3 * capacity / 2;
89 1024 : map->keys = keys = gpr_realloc(keys, capacity * sizeof(gpr_uint32));
90 1024 : map->values = values = gpr_realloc(values, capacity * sizeof(void *));
91 : }
92 : }
93 :
94 5226241 : keys[count] = key;
95 5226241 : values[count] = value;
96 5226241 : map->count = count + 1;
97 5226241 : }
98 :
99 13032082 : void grpc_chttp2_stream_map_move_into(grpc_chttp2_stream_map *src,
100 : grpc_chttp2_stream_map *dst) {
101 : /* if src is empty we dont need to do anything */
102 13032082 : if (src->count == src->free) {
103 12081588 : return;
104 : }
105 : /* if dst is empty we simply need to swap */
106 949122 : if (dst->count == dst->free) {
107 797585 : GPR_SWAP(grpc_chttp2_stream_map, *src, *dst);
108 797585 : return;
109 : }
110 : /* the first element of src must be greater than the last of dst...
111 : * however the maps may need compacting for this property to hold */
112 151537 : if (src->keys[0] <= dst->keys[dst->count - 1]) {
113 0 : src->count = compact(src->keys, src->values, src->count);
114 0 : src->free = 0;
115 0 : dst->count = compact(dst->keys, dst->values, dst->count);
116 0 : dst->free = 0;
117 : }
118 151537 : GPR_ASSERT(src->keys[0] > dst->keys[dst->count - 1]);
119 : /* if dst doesn't have capacity, resize */
120 151537 : if (dst->count + src->count > dst->capacity) {
121 169 : dst->capacity = GPR_MAX(dst->capacity * 3 / 2, dst->count + src->count);
122 169 : dst->keys = gpr_realloc(dst->keys, dst->capacity * sizeof(gpr_uint32));
123 169 : dst->values = gpr_realloc(dst->values, dst->capacity * sizeof(void *));
124 : }
125 151537 : memcpy(dst->keys + dst->count, src->keys, src->count * sizeof(gpr_uint32));
126 151537 : memcpy(dst->values + dst->count, src->values, src->count * sizeof(void *));
127 151537 : dst->count += src->count;
128 151537 : dst->free += src->free;
129 151537 : src->count = 0;
130 151537 : src->free = 0;
131 : }
132 :
133 20991089 : static void **find(grpc_chttp2_stream_map *map, gpr_uint32 key) {
134 20990252 : size_t min_idx = 0;
135 20991089 : size_t max_idx = map->count;
136 : size_t mid_idx;
137 20991089 : gpr_uint32 *keys = map->keys;
138 20991089 : void **values = map->values;
139 : gpr_uint32 mid_key;
140 :
141 20991089 : if (max_idx == 0) return NULL;
142 :
143 197988847 : while (min_idx < max_idx) {
144 : /* find the midpoint, avoiding overflow */
145 177540450 : mid_idx = min_idx + ((max_idx - min_idx) / 2);
146 177540450 : mid_key = keys[mid_idx];
147 :
148 177540450 : if (mid_key < key) {
149 110208321 : min_idx = mid_idx + 1;
150 67332129 : } else if (mid_key > key) {
151 50218349 : max_idx = mid_idx;
152 : } else /* mid_key == key */
153 : {
154 17113723 : return &values[mid_idx];
155 : }
156 : }
157 :
158 1667674 : return NULL;
159 : }
160 :
161 4829290 : void *grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map,
162 : gpr_uint32 key) {
163 4829290 : void **pvalue = find(map, key);
164 4834077 : void *out = NULL;
165 4834265 : if (pvalue != NULL) {
166 4833952 : out = *pvalue;
167 4833952 : *pvalue = NULL;
168 4833952 : map->free += (out != NULL);
169 : /* recognize complete emptyness and ensure we can skip
170 : * defragmentation later */
171 4833952 : if (map->free == map->count) {
172 1495115 : map->free = map->count = 0;
173 : }
174 : }
175 4834265 : return out;
176 : }
177 :
178 16122988 : void *grpc_chttp2_stream_map_find(grpc_chttp2_stream_map *map, gpr_uint32 key) {
179 16122988 : void **pvalue = find(map, key);
180 16164558 : return pvalue != NULL ? *pvalue : NULL;
181 : }
182 :
183 15406868 : size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map *map) {
184 15406868 : return map->count - map->free;
185 : }
186 :
187 49 : void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map *map,
188 : void (*f)(void *user_data, gpr_uint32 key,
189 : void *value),
190 : void *user_data) {
191 : size_t i;
192 :
193 320745 : for (i = 0; i < map->count; i++) {
194 320696 : if (map->values[i]) {
195 196432 : f(user_data, map->keys[i], map->values[i]);
196 : }
197 : }
198 49 : }
|