From 1f0476267b92943679bcedcdc0c520218dcc11f0 Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Thu, 2 May 2019 13:55:15 -0400 Subject: [PATCH] Add a better comment and rename consume_first to remove_first. --- .../transport/chttp2/transport/frame_data.cc | 30 +++++++++---------- .../compression/stream_compression_gzip.cc | 4 +-- src/core/lib/iomgr/tcp_posix.cc | 2 +- .../security/transport/security_handshaker.cc | 2 +- src/core/lib/slice/slice_buffer.cc | 2 +- src/core/lib/slice/slice_internal.h | 7 +++-- test/core/slice/slice_buffer_test.cc | 6 ++-- 7 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/core/ext/transport/chttp2/transport/frame_data.cc b/src/core/ext/transport/chttp2/transport/frame_data.cc index 5a0492f72ab..3734c0150b7 100644 --- a/src/core/ext/transport/chttp2/transport/frame_data.cc +++ b/src/core/ext/transport/chttp2/transport/frame_data.cc @@ -112,14 +112,14 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( char* msg; if (cur == end) { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); continue; } switch (p->state) { case GRPC_CHTTP2_DATA_ERROR: p->state = GRPC_CHTTP2_DATA_ERROR; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return GRPC_ERROR_REF(p->error); case GRPC_CHTTP2_DATA_FH_0: s->stats.incoming.framing_bytes++; @@ -144,12 +144,12 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( p->error = grpc_error_set_int(p->error, GRPC_ERROR_INT_OFFSET, cur - beg); p->state = GRPC_CHTTP2_DATA_ERROR; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return GRPC_ERROR_REF(p->error); } if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_1; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); continue; } /* fallthrough */ @@ -158,7 +158,7 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( p->frame_size = (static_cast(*cur)) << 24; if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_2; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); continue; } /* fallthrough */ @@ -167,7 +167,7 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( p->frame_size |= (static_cast(*cur)) << 16; if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_3; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); continue; } /* fallthrough */ @@ -176,7 +176,7 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( p->frame_size |= (static_cast(*cur)) << 8; if (++cur == end) { p->state = GRPC_CHTTP2_DATA_FH_4; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); continue; } /* fallthrough */ @@ -207,14 +207,14 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( grpc_slice_buffer_sub_first(slices, static_cast(cur - beg), static_cast(end - beg)); } else { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); } return GRPC_ERROR_NONE; case GRPC_CHTTP2_DATA_FRAME: { GPR_ASSERT(p->parsing_frame != nullptr); GPR_ASSERT(slice_out != nullptr); if (cur == end) { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); continue; } uint32_t remaining = static_cast(end - cur); @@ -225,17 +225,17 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( grpc_slice_sub(*slice, static_cast(cur - beg), static_cast(end - beg)), slice_out))) { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return error; } if (GRPC_ERROR_NONE != (error = p->parsing_frame->Finished(GRPC_ERROR_NONE, true))) { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return error; } p->parsing_frame = nullptr; p->state = GRPC_CHTTP2_DATA_FH_0; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return GRPC_ERROR_NONE; } else if (remaining < p->frame_size) { s->stats.incoming.data_bytes += remaining; @@ -247,7 +247,7 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( return error; } p->frame_size -= remaining; - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return GRPC_ERROR_NONE; } else { GPR_ASSERT(remaining > p->frame_size); @@ -258,12 +258,12 @@ grpc_error* grpc_deframe_unprocessed_incoming_frames( *slice, static_cast(cur - beg), static_cast(cur + p->frame_size - beg)), slice_out)) { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return error; } if (GRPC_ERROR_NONE != (error = p->parsing_frame->Finished(GRPC_ERROR_NONE, true))) { - grpc_slice_buffer_consume_first(slices); + grpc_slice_buffer_remove_first(slices); return error; } p->parsing_frame = nullptr; diff --git a/src/core/lib/compression/stream_compression_gzip.cc b/src/core/lib/compression/stream_compression_gzip.cc index f2c4bb92981..452b22b7628 100644 --- a/src/core/lib/compression/stream_compression_gzip.cc +++ b/src/core/lib/compression/stream_compression_gzip.cc @@ -60,7 +60,7 @@ static bool gzip_flate(grpc_stream_compression_context_gzip* ctx, if (r < 0 && r != Z_BUF_ERROR) { gpr_log(GPR_ERROR, "zlib error (%d)", r); grpc_slice_unref_internal(slice_out); - grpc_slice_buffer_consume_first(in); + grpc_slice_buffer_remove_first(in); return false; } else if (r == Z_STREAM_END && ctx->flate == inflate) { eoc = true; @@ -70,7 +70,7 @@ static bool gzip_flate(grpc_stream_compression_context_gzip* ctx, in, GRPC_SLICE_LENGTH(*slice) - ctx->zs.avail_in, GRPC_SLICE_LENGTH(*slice)); } else { - grpc_slice_buffer_consume_first(in); + grpc_slice_buffer_remove_first(in); } } if (flush != 0 && ctx->zs.avail_out > 0 && !eoc) { diff --git a/src/core/lib/iomgr/tcp_posix.cc b/src/core/lib/iomgr/tcp_posix.cc index 45af4931cf5..6c1955669cd 100644 --- a/src/core/lib/iomgr/tcp_posix.cc +++ b/src/core/lib/iomgr/tcp_posix.cc @@ -980,7 +980,7 @@ static bool tcp_flush(grpc_tcp* tcp, grpc_error** error) { // unref all and forget about all slices that have been written to this // point for (size_t idx = 0; idx < unwind_slice_idx; ++idx) { - grpc_slice_buffer_consume_first(tcp->outgoing_buffer); + grpc_slice_buffer_remove_first(tcp->outgoing_buffer); } return false; } else if (errno == EPIPE) { diff --git a/src/core/lib/security/transport/security_handshaker.cc b/src/core/lib/security/transport/security_handshaker.cc index 117e01a77a7..fdc64727b96 100644 --- a/src/core/lib/security/transport/security_handshaker.cc +++ b/src/core/lib/security/transport/security_handshaker.cc @@ -148,7 +148,7 @@ size_t SecurityHandshaker::MoveReadBufferIntoHandshakeBuffer() { memcpy(handshake_buffer_ + offset, GRPC_SLICE_START_PTR(*next_slice), GRPC_SLICE_LENGTH(*next_slice)); offset += GRPC_SLICE_LENGTH(*next_slice); - grpc_slice_buffer_consume_first(args_->read_buffer); + grpc_slice_buffer_remove_first(args_->read_buffer); } return bytes_in_read_buffer; } diff --git a/src/core/lib/slice/slice_buffer.cc b/src/core/lib/slice/slice_buffer.cc index 083e35791db..29937bfc9bf 100644 --- a/src/core/lib/slice/slice_buffer.cc +++ b/src/core/lib/slice/slice_buffer.cc @@ -370,7 +370,7 @@ grpc_slice grpc_slice_buffer_take_first(grpc_slice_buffer* sb) { return slice; } -void grpc_slice_buffer_consume_first(grpc_slice_buffer* sb) { +void grpc_slice_buffer_remove_first(grpc_slice_buffer* sb) { GPR_DEBUG_ASSERT(sb->count > 0); sb->length -= GRPC_SLICE_LENGTH(sb->slices[0]); grpc_slice_unref_internal(sb->slices[0]); diff --git a/src/core/lib/slice/slice_internal.h b/src/core/lib/slice/slice_internal.h index e6563f4720d..aa7c3dbce96 100644 --- a/src/core/lib/slice/slice_internal.h +++ b/src/core/lib/slice/slice_internal.h @@ -242,14 +242,15 @@ void grpc_slice_buffer_partial_unref_internal(grpc_slice_buffer* sb, size_t idx); void grpc_slice_buffer_destroy_internal(grpc_slice_buffer* sb); -// Returns the first slice in the slice buffer. +// Returns a pointer to the first slice in the slice buffer without giving +// ownership to or a reference count on that slice. inline grpc_slice* grpc_slice_buffer_peek_first(grpc_slice_buffer* sb) { GPR_DEBUG_ASSERT(sb->count > 0); return &sb->slices[0]; } -// Consumes the first slice in the slice buffer. -void grpc_slice_buffer_consume_first(grpc_slice_buffer* sb); +// Removes the first slice from the slice buffer. +void grpc_slice_buffer_remove_first(grpc_slice_buffer* sb); // Calls grpc_slice_sub with the given parameters on the first slice. void grpc_slice_buffer_sub_first(grpc_slice_buffer* sb, size_t begin, diff --git a/test/core/slice/slice_buffer_test.cc b/test/core/slice/slice_buffer_test.cc index 5ad11a34563..7d4acfed003 100644 --- a/test/core/slice/slice_buffer_test.cc +++ b/test/core/slice/slice_buffer_test.cc @@ -130,19 +130,19 @@ void test_slice_buffer_first() { GPR_ASSERT(buf.count == 3); GPR_ASSERT(buf.length == 10); - grpc_slice_buffer_consume_first(&buf); + grpc_slice_buffer_remove_first(&buf); first = grpc_slice_buffer_peek_first(&buf); GPR_ASSERT(GPR_SLICE_LENGTH(*first) == GPR_SLICE_LENGTH(slices[1])); GPR_ASSERT(buf.count == 2); GPR_ASSERT(buf.length == 9); - grpc_slice_buffer_consume_first(&buf); + grpc_slice_buffer_remove_first(&buf); first = grpc_slice_buffer_peek_first(&buf); GPR_ASSERT(GPR_SLICE_LENGTH(*first) == GPR_SLICE_LENGTH(slices[2])); GPR_ASSERT(buf.count == 1); GPR_ASSERT(buf.length == 5); - grpc_slice_buffer_consume_first(&buf); + grpc_slice_buffer_remove_first(&buf); GPR_ASSERT(buf.count == 0); GPR_ASSERT(buf.length == 0); }