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 #ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_STREAM_MAP_H 20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_STREAM_MAP_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <stddef.h> 25 26 /* Data structure to map a uint32_t to a data object (represented by a void*) 27 28 Represented as a sorted array of keys, and a corresponding array of values. 29 Lookups are performed with binary search. 30 Adds are restricted to strictly higher keys than previously seen (this is 31 guaranteed by http2). */ 32 struct grpc_chttp2_stream_map { 33 uint32_t* keys; 34 void** values; 35 size_t count; 36 size_t free; 37 size_t capacity; 38 }; 39 void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map* map, 40 size_t initial_capacity); 41 void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map* map); 42 43 /* Add a new key: given http2 semantics, new keys must always be greater than 44 existing keys - this is asserted */ 45 void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map* map, uint32_t key, 46 void* value); 47 48 /* Delete an existing key - returns the previous value of the key if it existed, 49 or NULL otherwise */ 50 void* grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map* map, uint32_t key); 51 52 /* Return an existing key, or NULL if it does not exist */ 53 void* grpc_chttp2_stream_map_find(grpc_chttp2_stream_map* map, uint32_t key); 54 55 /* Return a random entry */ 56 void* grpc_chttp2_stream_map_rand(grpc_chttp2_stream_map* map); 57 58 /* How many (populated) entries are in the stream map? */ 59 size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map* map); 60 61 /* Callback on each stream */ 62 void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map* map, 63 void (*f)(void* user_data, uint32_t key, 64 void* value), 65 void* user_data); 66 67 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_STREAM_MAP_H */ 68