• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2019 The 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 #endregion
18 
19 using Grpc.Core.Utils;
20 using System;
21 using System.Threading;
22 
23 using System.Buffers;
24 
25 namespace Grpc.Core.Internal
26 {
27     // Allow creating instances of Memory<byte> from Slice.
28     // Represents a chunk of native memory, but doesn't manage its lifetime.
29     // Instances of this class are reuseable - they can be reset to point to a different memory chunk.
30     // That is important to make the instances cacheable (rather then creating new instances
31     // the old ones will be reused to reduce GC pressure).
32     internal class SliceMemoryManager : MemoryManager<byte>
33     {
34         private Slice slice;
35 
Reset(Slice slice)36         public void Reset(Slice slice)
37         {
38             this.slice = slice;
39         }
40 
Reset()41         public void Reset()
42         {
43             Reset(new Slice(IntPtr.Zero, 0));
44         }
45 
GetSpan()46         public override Span<byte> GetSpan()
47         {
48             return slice.ToSpanUnsafe();
49         }
50 
Pin(int elementIndex = 0)51         public override MemoryHandle Pin(int elementIndex = 0)
52         {
53             throw new NotSupportedException();
54         }
55 
Unpin()56         public override void Unpin()
57         {
58         }
59 
Dispose(bool disposing)60         protected override void Dispose(bool disposing)
61         {
62             // NOP
63         }
64     }
65 }
66