SliceMemoryManager.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using Grpc.Core.Utils;
  17. using System;
  18. using System.Threading;
  19. using System.Buffers;
  20. namespace Grpc.Core.Internal
  21. {
  22. // Allow creating instances of Memory<byte> from Slice.
  23. // Represents a chunk of native memory, but doesn't manage its lifetime.
  24. // Instances of this class are reuseable - they can be reset to point to a different memory chunk.
  25. // That is important to make the instances cacheable (rather then creating new instances
  26. // the old ones will be reused to reduce GC pressure).
  27. internal class SliceMemoryManager : MemoryManager<byte>
  28. {
  29. private Slice slice;
  30. public void Reset(Slice slice)
  31. {
  32. this.slice = slice;
  33. }
  34. public void Reset()
  35. {
  36. Reset(new Slice(IntPtr.Zero, 0));
  37. }
  38. public override Span<byte> GetSpan()
  39. {
  40. return slice.ToSpanUnsafe();
  41. }
  42. public override MemoryHandle Pin(int elementIndex = 0)
  43. {
  44. throw new NotSupportedException();
  45. }
  46. public override void Unpin()
  47. {
  48. }
  49. protected override void Dispose(bool disposing)
  50. {
  51. // NOP
  52. }
  53. }
  54. }