From fb5411fba9d91f73814b6c107a268c374ad58721 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 9 Aug 2019 14:30:28 -0700 Subject: [PATCH] support GetMemory() --- .../Grpc.Core/Internal/SliceBufferSafeHandle.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/SliceBufferSafeHandle.cs b/src/csharp/Grpc.Core/Internal/SliceBufferSafeHandle.cs index 76a97f4a0a2..0ce37317f1a 100644 --- a/src/csharp/Grpc.Core/Internal/SliceBufferSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/SliceBufferSafeHandle.cs @@ -39,6 +39,8 @@ namespace Grpc.Core.Internal private IntPtr tailSpacePtr; private int tailSpaceLen; + private SliceMemoryManager memoryManagerLazy; + private SliceBufferSafeHandle() { } @@ -63,14 +65,20 @@ namespace Grpc.Core.Internal GrpcPreconditions.CheckArgument(tailSpaceLen >= count); tailSpaceLen = tailSpaceLen - count; tailSpacePtr += count; + memoryManagerLazy?.Reset(); } // provides access to the "tail space" of this buffer. // Use GetSpan when possible for better efficiency. public Memory GetMemory(int sizeHint = 0) { - // TODO: implement - throw new NotImplementedException(); + GetSpan(sizeHint); + if (memoryManagerLazy == null) + { + memoryManagerLazy = new SliceMemoryManager(); + } + memoryManagerLazy.Reset(new Slice(tailSpacePtr, tailSpaceLen)); + return memoryManagerLazy.Memory; } // provides access to the "tail space" of this buffer. @@ -97,6 +105,7 @@ namespace Grpc.Core.Internal // deletes all the data in the slice buffer tailSpacePtr = IntPtr.Zero; tailSpaceLen = 0; + memoryManagerLazy?.Reset(); Native.grpcsharp_slice_buffer_reset_and_unref(this); }