From 9f1307f1af91e734491a24aa5c93b56a2a2b89a8 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 16 Jun 2015 07:43:29 -0700 Subject: [PATCH] Implement grpc_stream_map_move_into --- src/core/transport/chttp2/stream_map.c | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/core/transport/chttp2/stream_map.c b/src/core/transport/chttp2/stream_map.c index 580e32c582e..2d80007bcb9 100644 --- a/src/core/transport/chttp2/stream_map.c +++ b/src/core/transport/chttp2/stream_map.c @@ -32,8 +32,12 @@ */ #include "src/core/transport/chttp2/stream_map.h" + +#include + #include #include +#include void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map, size_t initial_capacity) { @@ -92,6 +96,32 @@ void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, gpr_uint32 key, map->count = count + 1; } +void grpc_chttp2_stream_map_move_into(grpc_chttp2_stream_map *src, grpc_chttp2_stream_map *dst) { + /* if src is empty we dont need to do anything */ + if (src->count == src->free) { + return; + } + /* if dst is empty we simply need to swap */ + if (dst->count == dst->free) { + GPR_SWAP(grpc_chttp2_stream_map, *src, *dst); + return; + } + /* if dst doesn't have capacity, resize */ + if (dst->count + src->count > dst->capacity) { + dst->capacity = GPR_MAX(dst->capacity * 3 / 2, dst->count + src->count); + dst->keys = gpr_realloc(dst->keys, dst->capacity * sizeof(gpr_uint32)); + dst->values = gpr_realloc(dst->values, dst->capacity * sizeof(gpr_uint32)); + } + /* the first element of src must be greater than the last of dst */ + GPR_ASSERT(src->keys[0] > dst->keys[dst->count - 1]); + memcpy(dst->keys + dst->count, src->keys, src->count * sizeof(gpr_uint32)); + memcpy(dst->values + dst->count, src->values, src->count * sizeof(gpr_uint32)); + dst->count += src->count; + dst->free += src->free; + src->count = 0; + src->free = 0; +} + static void **find(grpc_chttp2_stream_map *map, gpr_uint32 key) { size_t min_idx = 0; size_t max_idx = map->count;