stream_map.cc
Go to the documentation of this file.
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 
20 
22 
23 #include <stdlib.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 
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 
41  gpr_free(map->keys);
42  gpr_free(map->values);
43 }
44 
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 
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);
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.
77 
78  if (count == capacity) {
79  if (map->free > capacity / 4) {
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>
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 
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  }
142  return out;
143 }
144 
146  void** pvalue = find<false>(map, key);
147  return pvalue != nullptr ? *pvalue : nullptr;
148 }
149 
151  return map->count - map->free;
152 }
153 
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 
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 }
compact
static size_t compact(uint32_t *keys, void **values, size_t count)
Definition: stream_map.cc:45
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
log.h
find
static void ** find(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:99
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
keys
const void * keys
Definition: abseil-cpp/absl/random/internal/randen.cc:49
capacity
uint16_t capacity
Definition: protobuf/src/google/protobuf/descriptor.cc:948
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_chttp2_stream_map_add
void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, uint32_t key, void *value)
Definition: stream_map.cc:59
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
grpc_chttp2_stream_map_find
void * grpc_chttp2_stream_map_find(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:145
grpc_chttp2_stream_map_for_each
void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map *map, void(*f)(void *user_data, uint32_t key, void *value), void *user_data)
Definition: stream_map.cc:166
grpc_chttp2_stream_map_delete
void * grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:129
grpc_chttp2_stream_map_rand
void * grpc_chttp2_stream_map_rand(grpc_chttp2_stream_map *map)
Definition: stream_map.cc:154
stream_map.h
grpc_chttp2_stream_map_size
size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map *map)
Definition: stream_map.cc:150
grpc_chttp2_stream_map
Definition: stream_map.h:33
value
const char * value
Definition: hpack_parser_table.cc:165
key
const char * key
Definition: hpack_parser_table.cc:164
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
grpc_chttp2_stream_map_destroy
void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map *map)
Definition: stream_map.cc:40
grpc_chttp2_stream_map_init
void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map, size_t initial_capacity)
Definition: stream_map.cc:28
alloc.h
values
std::array< int64_t, Size > values
Definition: abseil-cpp/absl/container/btree_benchmark.cc:608
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:20