-
Notifications
You must be signed in to change notification settings - Fork 201
/
RecyclableMemoryStreamManager.cs
958 lines (844 loc) · 43.8 KB
/
RecyclableMemoryStreamManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
// ---------------------------------------------------------------------
// Copyright (c) 2015-2016 Microsoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------
namespace Microsoft.IO
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
/// <summary>
/// Manages pools of <see cref="RecyclableMemoryStream"/> objects.
/// </summary>
/// <remarks>
/// <para>
/// There are two pools managed in here. The small pool contains same-sized buffers that are handed to streams
/// as they write more data.
///</para>
///<para>
/// For scenarios that need to call <see cref="RecyclableMemoryStream.GetBuffer"/>, the large pool contains buffers of various sizes, all
/// multiples/exponentials of <see cref="Options.LargeBufferMultiple"/> (1 MB by default). They are split by size to avoid overly-wasteful buffer
/// usage. There should be far fewer 8 MB buffers than 1 MB buffers, for example.
/// </para>
/// </remarks>
public partial class RecyclableMemoryStreamManager
{
/// <summary>
/// Maximum length of a single array.
/// </summary>
/// <remarks>See documentation at https://docs.microsoft.com/dotnet/api/system.array?view=netcore-3.1
/// </remarks>
internal const int MaxArrayLength = 0X7FFFFFC7;
/// <summary>
/// Default block size, in bytes.
/// </summary>
public const int DefaultBlockSize = 128 * 1024;
/// <summary>
/// Default large buffer multiple, in bytes.
/// </summary>
public const int DefaultLargeBufferMultiple = 1024 * 1024;
/// <summary>
/// Default maximum buffer size, in bytes.
/// </summary>
public const int DefaultMaximumBufferSize = 128 * 1024 * 1024;
// 0 to indicate unbounded
private const long DefaultMaxSmallPoolFreeBytes = 0L;
private const long DefaultMaxLargePoolFreeBytes = 0L;
private readonly long[] largeBufferFreeSize;
private readonly long[] largeBufferInUseSize;
private readonly ConcurrentStack<byte[]>[] largePools;
private readonly ConcurrentStack<byte[]> smallPool;
private long smallPoolFreeSize;
private long smallPoolInUseSize;
internal readonly Options options;
/// <summary>
/// Settings for controlling the behavior of RecyclableMemoryStream
/// </summary>
public Options Settings => this.options;
/// <summary>
/// Number of bytes in small pool not currently in use.
/// </summary>
public long SmallPoolFreeSize => this.smallPoolFreeSize;
/// <summary>
/// Number of bytes currently in use by stream from the small pool.
/// </summary>
public long SmallPoolInUseSize => this.smallPoolInUseSize;
/// <summary>
/// Number of bytes in large pool not currently in use.
/// </summary>
public long LargePoolFreeSize
{
get
{
long sum = 0;
foreach (long freeSize in this.largeBufferFreeSize)
{
sum += freeSize;
}
return sum;
}
}
/// <summary>
/// Number of bytes currently in use by streams from the large pool.
/// </summary>
public long LargePoolInUseSize
{
get
{
long sum = 0;
foreach (long inUseSize in this.largeBufferInUseSize)
{
sum += inUseSize;
}
return sum;
}
}
/// <summary>
/// How many blocks are in the small pool.
/// </summary>
public long SmallBlocksFree => this.smallPool.Count;
/// <summary>
/// How many buffers are in the large pool.
/// </summary>
public long LargeBuffersFree
{
get
{
long free = 0;
foreach (var pool in this.largePools)
{
free += pool.Count;
}
return free;
}
}
/// <summary>
/// Parameters for customizing the behavior of <see cref="RecyclableMemoryStreamManager"/>
/// </summary>
public class Options
{
/// <summary>
/// Gets or sets the size of the pooled blocks. This must be greater than 0.
/// </summary>
/// <remarks>The default size 131,072 (128KB)</remarks>
public int BlockSize { get; set; } = DefaultBlockSize;
/// <summary>
/// Each large buffer will be a multiple exponential of this value
/// </summary>
/// <remarks>The default value is 1,048,576 (1MB)</remarks>
public int LargeBufferMultiple { get; set; } = DefaultLargeBufferMultiple;
/// <summary>
/// Buffer beyond this length are not pooled.
/// </summary>
/// <remarks>The default value is 134,217,728 (128MB)</remarks>
public int MaximumBufferSize { get; set; } = DefaultMaximumBufferSize;
/// <summary>
/// Maximum number of bytes to keep available in the small pool.
/// </summary>
/// <remarks>
/// <para>Trying to return buffers to the pool beyond this limit will result in them being garbage collected.</para>
/// <para>The default value is 0, but all users should set a reasonable value depending on your application's memory requirements.</para>
/// </remarks>
public long MaximumSmallPoolFreeBytes { get; set; }
/// <summary>
/// Maximum number of bytes to keep available in the large pools.
/// </summary>
/// <remarks>
/// <para>Trying to return buffers to the pool beyond this limit will result in them being garbage collected.</para>
/// <para>The default value is 0, but all users should set a reasonable value depending on your application's memory requirements.</para>
/// </remarks>
public long MaximumLargePoolFreeBytes { get; set; }
/// <summary>
/// Whether to use the exponential allocation strategy (see documentation).
/// </summary>
/// <remarks>The default value is false.</remarks>
public bool UseExponentialLargeBuffer { get; set; } = false;
/// <summary>
/// Maximum stream capacity in bytes. Attempts to set a larger capacity will
/// result in an exception.
/// </summary>
/// <remarks>The default value of 0 indicates no limit.</remarks>
public long MaximumStreamCapacity { get; set; } = 0;
/// <summary>
/// Whether to save call stacks for stream allocations. This can help in debugging.
/// It should NEVER be turned on generally in production.
/// </summary>
public bool GenerateCallStacks { get; set; } = false;
/// <summary>
/// Whether dirty buffers can be immediately returned to the buffer pool.
/// </summary>
/// <remarks>
/// <para>
/// When <see cref="RecyclableMemoryStream.GetBuffer"/> is called on a stream and creates a single large buffer, if this setting is enabled, the other blocks will be returned
/// to the buffer pool immediately.
/// </para>
/// <para>
/// Note when enabling this setting that the user is responsible for ensuring that any buffer previously
/// retrieved from a stream which is subsequently modified is not used after modification (as it may no longer
/// be valid).
/// </para>
/// </remarks>
public bool AggressiveBufferReturn { get; set; } = false;
/// <summary>
/// Causes an exception to be thrown if <see cref="RecyclableMemoryStream.ToArray"/> is ever called.
/// </summary>
/// <remarks>Calling <see cref="RecyclableMemoryStream.ToArray"/> defeats the purpose of a pooled buffer. Use this property to discover code that is calling <see cref="RecyclableMemoryStream.ToArray"/>. If this is
/// set and <see cref="RecyclableMemoryStream.ToArray"/> is called, a <c>NotSupportedException</c> will be thrown.</remarks>
public bool ThrowExceptionOnToArray { get; set; } = false;
/// <summary>
/// Zero out buffers on allocation and before returning them to the pool.
/// </summary>
/// <remarks>Setting this to true causes a performance hit and should only be set if one wants to avoid accidental data leaks.</remarks>
public bool ZeroOutBuffer { get; set; } = false;
/// <summary>
/// Creates a new <see cref="Options"/> object.
/// </summary>
public Options()
{
}
/// <summary>
/// Creates a new <see cref="Options"/> object with the most common options.
/// </summary>
/// <param name="blockSize">Size of the blocks in the small pool.</param>
/// <param name="largeBufferMultiple">Size of the large buffer multiple</param>
/// <param name="maximumBufferSize">Maximum poolable buffer size.</param>
/// <param name="maximumSmallPoolFreeBytes">Maximum bytes to hold in the small pool.</param>
/// <param name="maximumLargePoolFreeBytes">Maximum bytes to hold in each of the large pools.</param>
public Options(int blockSize, int largeBufferMultiple, int maximumBufferSize, long maximumSmallPoolFreeBytes, long maximumLargePoolFreeBytes)
{
this.BlockSize = blockSize;
this.LargeBufferMultiple = largeBufferMultiple;
this.MaximumBufferSize = maximumBufferSize;
this.MaximumSmallPoolFreeBytes = maximumSmallPoolFreeBytes;
this.MaximumLargePoolFreeBytes = maximumLargePoolFreeBytes;
}
}
/// <summary>
/// Initializes the memory manager with the default block/buffer specifications. This pool may have unbounded growth unless you modify <see cref="Options"/>.
/// </summary>
public RecyclableMemoryStreamManager()
: this(new Options()) { }
/// <summary>
/// Initializes the memory manager with the given block requiredSize.
/// </summary>
/// <param name="options">Object specifying options for stream behavior.</param>
/// <exception cref="InvalidOperationException">
/// <paramref name="options.BlockSize"/> is not a positive number,
/// or <paramref name="options.LargeBufferMultiple"/> is not a positive number,
/// or <paramref name="options.MaximumBufferSize"/> is less than options.BlockSize,
/// or <paramref name="options.MaximumSmallPoolFreeBytes"/> is negative,
/// or <paramref name="options.MaximumLargePoolFreeBytes"/> is negative,
/// or <paramref name="options.MaximumBufferSize"/> is not a multiple/exponential of <paramref name="options.LargeBufferMultiple"/>.
/// </exception>
public RecyclableMemoryStreamManager(Options options)
{
if (options.BlockSize <= 0)
{
throw new InvalidOperationException($"{nameof(options.BlockSize)} must be a positive number");
}
if (options.LargeBufferMultiple <= 0)
{
throw new InvalidOperationException($"{nameof(options.LargeBufferMultiple)} must be a positive number");
}
if (options.MaximumBufferSize < options.BlockSize)
{
throw new InvalidOperationException($"{nameof(options.MaximumBufferSize)} must be at least {nameof(options.BlockSize)}");
}
if (options.MaximumSmallPoolFreeBytes < 0)
{
throw new InvalidOperationException($"{nameof(options.MaximumSmallPoolFreeBytes)} must be non-negative");
}
if (options.MaximumLargePoolFreeBytes < 0)
{
throw new InvalidOperationException($"{nameof(options.MaximumLargePoolFreeBytes)} must be non-negative");
}
this.options = options;
if (!this.IsLargeBufferSize(options.MaximumBufferSize))
{
throw new InvalidOperationException(
$"{nameof(options.MaximumBufferSize)} is not {(options.UseExponentialLargeBuffer ? "an exponential" : "a multiple")} of {nameof(options.LargeBufferMultiple)}.");
}
this.smallPool = new ConcurrentStack<byte[]>();
var numLargePools = options.UseExponentialLargeBuffer
? ((int)Math.Log(options.MaximumBufferSize / options.LargeBufferMultiple, 2) + 1)
: (options.MaximumBufferSize / options.LargeBufferMultiple);
// +1 to store size of bytes in use that are too large to be pooled
this.largeBufferInUseSize = new long[numLargePools + 1];
this.largeBufferFreeSize = new long[numLargePools];
this.largePools = new ConcurrentStack<byte[]>[numLargePools];
for (var i = 0; i < this.largePools.Length; ++i)
{
this.largePools[i] = new ConcurrentStack<byte[]>();
}
Events.Writer.MemoryStreamManagerInitialized(options.BlockSize, options.LargeBufferMultiple, options.MaximumBufferSize);
}
/// <summary>
/// Removes and returns a single block from the pool.
/// </summary>
/// <returns>A <c>byte[]</c> array.</returns>
internal byte[] GetBlock()
{
Interlocked.Add(ref this.smallPoolInUseSize, this.options.BlockSize);
if (!this.smallPool.TryPop(out byte[]? block))
{
// We'll add this back to the pool when the stream is disposed
// (unless our free pool is too large)
#if NET6_0_OR_GREATER
block = this.options.ZeroOutBuffer ? GC.AllocateArray<byte>(this.options.BlockSize) : GC.AllocateUninitializedArray<byte>(this.options.BlockSize);
#else
block = new byte[this.options.BlockSize];
#endif
this.ReportBlockCreated();
}
else
{
Interlocked.Add(ref this.smallPoolFreeSize, -this.options.BlockSize);
}
return block;
}
/// <summary>
/// Returns a buffer of arbitrary size from the large buffer pool. This buffer
/// will be at least the requiredSize and always be a multiple/exponential of largeBufferMultiple.
/// </summary>
/// <param name="requiredSize">The minimum length of the buffer.</param>
/// <param name="id">Unique ID for the stream.</param>
/// <param name="tag">The tag of the stream returning this buffer, for logging if necessary.</param>
/// <returns>A buffer of at least the required size.</returns>
/// <exception cref="OutOfMemoryException">Requested array size is larger than the maximum allowed.</exception>
internal byte[] GetLargeBuffer(long requiredSize, Guid id, string? tag)
{
requiredSize = this.RoundToLargeBufferSize(requiredSize);
if (requiredSize > MaxArrayLength)
{
throw new OutOfMemoryException($"Required buffer size exceeds maximum array length of {MaxArrayLength}.");
}
var poolIndex = this.GetPoolIndex(requiredSize);
bool createdNew = false;
bool pooled = true;
string? callStack = null;
byte[]? buffer;
if (poolIndex < this.largePools.Length)
{
if (!this.largePools[poolIndex].TryPop(out buffer))
{
buffer = AllocateArray(requiredSize, this.options.ZeroOutBuffer);
createdNew = true;
}
else
{
Interlocked.Add(ref this.largeBufferFreeSize[poolIndex], -buffer.Length);
}
}
else
{
// Buffer is too large to pool. They get a new buffer.
// We still want to track the size, though, and we've reserved a slot
// in the end of the in-use array for non-pooled bytes in use.
poolIndex = this.largeBufferInUseSize.Length - 1;
// We still want to round up to reduce heap fragmentation.
buffer = AllocateArray(requiredSize, this.options.ZeroOutBuffer);
if (this.options.GenerateCallStacks)
{
// Grab the stack -- we want to know who requires such large buffers
callStack = Environment.StackTrace;
}
createdNew = true;
pooled = false;
}
Interlocked.Add(ref this.largeBufferInUseSize[poolIndex], buffer.Length);
if (createdNew)
{
this.ReportLargeBufferCreated(id, tag, requiredSize, pooled: pooled, callStack);
}
return buffer;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static byte[] AllocateArray(long requiredSize, bool zeroInitializeArray) =>
#if NET6_0_OR_GREATER
zeroInitializeArray ? GC.AllocateArray<byte>((int)requiredSize) : GC.AllocateUninitializedArray<byte>((int)requiredSize);
#else
new byte[requiredSize];
#endif
}
private long RoundToLargeBufferSize(long requiredSize)
{
if (this.options.UseExponentialLargeBuffer)
{
long pow = 1;
while (this.options.LargeBufferMultiple * pow < requiredSize)
{
pow <<= 1;
}
return this.options.LargeBufferMultiple * pow;
}
else
{
return ((requiredSize + this.options.LargeBufferMultiple - 1) / this.options.LargeBufferMultiple) * this.options.LargeBufferMultiple;
}
}
private bool IsLargeBufferSize(int value)
{
return (value != 0) && (this.options.UseExponentialLargeBuffer
? (value == this.RoundToLargeBufferSize(value))
: (value % this.options.LargeBufferMultiple) == 0);
}
private int GetPoolIndex(long length)
{
if (this.options.UseExponentialLargeBuffer)
{
int index = 0;
while ((this.options.LargeBufferMultiple << index) < length)
{
++index;
}
return index;
}
else
{
return (int)(length / this.options.LargeBufferMultiple - 1);
}
}
/// <summary>
/// Returns the buffer to the large pool.
/// </summary>
/// <param name="buffer">The buffer to return.</param>
/// <param name="id">Unique stream ID.</param>
/// <param name="tag">The tag of the stream returning this buffer, for logging if necessary.</param>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
/// <exception cref="ArgumentException"><c>buffer.Length</c> is not a multiple/exponential of <see cref="Options.LargeBufferMultiple"/> (it did not originate from this pool).</exception>
internal void ReturnLargeBuffer(byte[] buffer, Guid id, string? tag)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (!this.IsLargeBufferSize(buffer.Length))
{
throw new ArgumentException($"{nameof(buffer)} did not originate from this memory manager. The size is not " +
$"{(this.options.UseExponentialLargeBuffer ? "an exponential" : "a multiple")} of {this.options.LargeBufferMultiple}.");
}
this.ZeroOutMemoryIfEnabled(buffer);
var poolIndex = this.GetPoolIndex(buffer.Length);
if (poolIndex < this.largePools.Length)
{
if ((this.largePools[poolIndex].Count + 1) * buffer.Length <= this.options.MaximumLargePoolFreeBytes ||
this.options.MaximumLargePoolFreeBytes == 0)
{
this.largePools[poolIndex].Push(buffer);
Interlocked.Add(ref this.largeBufferFreeSize[poolIndex], buffer.Length);
}
else
{
this.ReportBufferDiscarded(id, tag, Events.MemoryStreamBufferType.Large, Events.MemoryStreamDiscardReason.EnoughFree);
}
}
else
{
// This is a non-poolable buffer, but we still want to track its size for in-use
// analysis. We have space in the InUse array for this.
poolIndex = this.largeBufferInUseSize.Length - 1;
this.ReportBufferDiscarded(id, tag, Events.MemoryStreamBufferType.Large, Events.MemoryStreamDiscardReason.TooLarge);
}
Interlocked.Add(ref this.largeBufferInUseSize[poolIndex], -buffer.Length);
}
/// <summary>
/// Returns the blocks to the pool.
/// </summary>
/// <param name="blocks">Collection of blocks to return to the pool.</param>
/// <param name="id">Unique Stream ID.</param>
/// <param name="tag">The tag of the stream returning these blocks, for logging if necessary.</param>
/// <exception cref="ArgumentNullException"><paramref name="blocks"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="blocks"/> contains buffers that are the wrong size (or null) for this memory manager.</exception>
internal void ReturnBlocks(List<byte[]> blocks, Guid id, string? tag)
{
if (blocks == null)
{
throw new ArgumentNullException(nameof(blocks));
}
long bytesToReturn = (long)blocks.Count * (long)this.options.BlockSize;
Interlocked.Add(ref this.smallPoolInUseSize, -bytesToReturn);
foreach (var block in blocks)
{
if (block == null || block.Length != this.options.BlockSize)
{
throw new ArgumentException($"{nameof(blocks)} contains buffers that are not {nameof(this.options.BlockSize)} in length.", nameof(blocks));
}
}
foreach (var block in blocks)
{
this.ZeroOutMemoryIfEnabled(block);
if (this.options.MaximumSmallPoolFreeBytes == 0 || this.SmallPoolFreeSize < this.options.MaximumSmallPoolFreeBytes)
{
Interlocked.Add(ref this.smallPoolFreeSize, this.options.BlockSize);
this.smallPool.Push(block);
}
else
{
this.ReportBufferDiscarded(id, tag, Events.MemoryStreamBufferType.Small, Events.MemoryStreamDiscardReason.EnoughFree);
break;
}
}
}
/// <summary>
/// Returns a block to the pool.
/// </summary>
/// <param name="block">Block to return to the pool.</param>
/// <param name="id">Unique Stream ID.</param>
/// <param name="tag">The tag of the stream returning this, for logging if necessary.</param>
/// <exception cref="ArgumentNullException"><paramref name="block"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="block"/> is the wrong size for this memory manager.</exception>
internal void ReturnBlock(byte[] block, Guid id, string? tag)
{
var bytesToReturn = this.options.BlockSize;
Interlocked.Add(ref this.smallPoolInUseSize, -bytesToReturn);
if (block == null)
{
throw new ArgumentNullException(nameof(block));
}
if (block.Length != this.options.BlockSize)
{
throw new ArgumentException($"{nameof(block)} is not not {nameof(this.options.BlockSize)} in length.");
}
this.ZeroOutMemoryIfEnabled(block);
if (this.options.MaximumSmallPoolFreeBytes == 0 || this.SmallPoolFreeSize < this.options.MaximumSmallPoolFreeBytes)
{
Interlocked.Add(ref this.smallPoolFreeSize, this.options.BlockSize);
this.smallPool.Push(block);
}
else
{
this.ReportBufferDiscarded(id, tag, Events.MemoryStreamBufferType.Small, Events.MemoryStreamDiscardReason.EnoughFree);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ZeroOutMemoryIfEnabled(byte[] buffer)
{
if (this.options.ZeroOutBuffer)
{
#if NET6_0_OR_GREATER
Array.Clear(buffer);
#else
Array.Clear(buffer, 0, buffer.Length);
#endif
}
}
internal void ReportBlockCreated()
{
Events.Writer.MemoryStreamNewBlockCreated(this.smallPoolInUseSize);
this.BlockCreated?.Invoke(this, new BlockCreatedEventArgs(this.smallPoolInUseSize));
}
internal void ReportLargeBufferCreated(Guid id, string? tag, long requiredSize, bool pooled, string? callStack)
{
if (pooled)
{
Events.Writer.MemoryStreamNewLargeBufferCreated(requiredSize, this.LargePoolInUseSize);
}
else
{
Events.Writer.MemoryStreamNonPooledLargeBufferCreated(id, tag, requiredSize, callStack);
}
this.LargeBufferCreated?.Invoke(this, new LargeBufferCreatedEventArgs(id, tag, requiredSize, this.LargePoolInUseSize, pooled, callStack));
}
internal void ReportBufferDiscarded(Guid id, string? tag, Events.MemoryStreamBufferType bufferType, Events.MemoryStreamDiscardReason reason)
{
Events.Writer.MemoryStreamDiscardBuffer(id, tag, bufferType, reason,
this.SmallBlocksFree, this.smallPoolFreeSize, this.smallPoolInUseSize,
this.LargeBuffersFree, this.LargePoolFreeSize, this.LargePoolInUseSize);
this.BufferDiscarded?.Invoke(this, new BufferDiscardedEventArgs(id, tag, bufferType, reason));
}
internal void ReportStreamCreated(Guid id, string? tag, long requestedSize, long actualSize)
{
Events.Writer.MemoryStreamCreated(id, tag, requestedSize, actualSize);
this.StreamCreated?.Invoke(this, new StreamCreatedEventArgs(id, tag, requestedSize, actualSize));
}
internal void ReportStreamDisposed(Guid id, string? tag, TimeSpan lifetime, string? allocationStack, string? disposeStack)
{
Events.Writer.MemoryStreamDisposed(id, tag, (long)lifetime.TotalMilliseconds, allocationStack, disposeStack);
this.StreamDisposed?.Invoke(this, new StreamDisposedEventArgs(id, tag, lifetime, allocationStack, disposeStack));
}
internal void ReportStreamDoubleDisposed(Guid id, string? tag, string? allocationStack, string? disposeStack1, string? disposeStack2)
{
Events.Writer.MemoryStreamDoubleDispose(id, tag, allocationStack, disposeStack1, disposeStack2);
this.StreamDoubleDisposed?.Invoke(this, new StreamDoubleDisposedEventArgs(id, tag, allocationStack, disposeStack1, disposeStack2));
}
internal void ReportStreamFinalized(Guid id, string? tag, string? allocationStack)
{
Events.Writer.MemoryStreamFinalized(id, tag, allocationStack);
this.StreamFinalized?.Invoke(this, new StreamFinalizedEventArgs(id, tag, allocationStack));
}
internal void ReportStreamLength(long bytes)
{
this.StreamLength?.Invoke(this, new StreamLengthEventArgs(bytes));
}
internal void ReportStreamToArray(Guid id, string? tag, string? stack, long length)
{
Events.Writer.MemoryStreamToArray(id, tag, stack, length);
this.StreamConvertedToArray?.Invoke(this, new StreamConvertedToArrayEventArgs(id, tag, stack, length));
}
internal void ReportStreamOverCapacity(Guid id, string? tag, long requestedCapacity, string? allocationStack)
{
Events.Writer.MemoryStreamOverCapacity(id, tag, requestedCapacity, this.options.MaximumStreamCapacity, allocationStack);
this.StreamOverCapacity?.Invoke(this, new StreamOverCapacityEventArgs(id, tag, requestedCapacity, this.options.MaximumStreamCapacity, allocationStack));
}
internal void ReportUsageReport()
{
this.UsageReport?.Invoke(this, new UsageReportEventArgs(this.smallPoolInUseSize, this.smallPoolFreeSize, this.LargePoolInUseSize, this.LargePoolFreeSize));
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with no tag and a default initial capacity.
/// </summary>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream()
{
return new RecyclableMemoryStream(this);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with no tag and a default initial capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(Guid id)
{
return new RecyclableMemoryStream(this, id);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and a default initial capacity.
/// </summary>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(string? tag)
{
return new RecyclableMemoryStream(this, tag);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and a default initial capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(Guid id, string? tag)
{
return new RecyclableMemoryStream(this, id, tag);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and at least the given capacity.
/// </summary>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(string? tag, long requiredSize)
{
return new RecyclableMemoryStream(this, tag, requiredSize);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and at least the given capacity.
/// </summary>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(Guid id, string? tag, long requiredSize)
{
return new RecyclableMemoryStream(this, id, tag, requiredSize);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and at least the given capacity, possibly using
/// a single contiguous underlying buffer.
/// </summary>
/// <remarks>Retrieving a <see cref="RecyclableMemoryStream"/> which provides a single contiguous buffer can be useful in situations
/// where the initial size is known and it is desirable to avoid copying data between the smaller underlying
/// buffers to a single large one. This is most helpful when you know that you will always call <see cref="RecyclableMemoryStream.GetBuffer"/>
/// on the underlying stream.</remarks>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <param name="asContiguousBuffer">Whether to attempt to use a single contiguous buffer.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(Guid id, string? tag, long requiredSize, bool asContiguousBuffer)
{
if (!asContiguousBuffer || requiredSize <= this.options.BlockSize)
{
return this.GetStream(id, tag, requiredSize);
}
return new RecyclableMemoryStream(this, id, tag, requiredSize, this.GetLargeBuffer(requiredSize, id, tag));
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and at least the given capacity, possibly using
/// a single contiguous underlying buffer.
/// </summary>
/// <remarks>Retrieving a <see cref="RecyclableMemoryStream"/> which provides a single contiguous buffer can be useful in situations
/// where the initial size is known and it is desirable to avoid copying data between the smaller underlying
/// buffers to a single large one. This is most helpful when you know that you will always call <see cref="RecyclableMemoryStream.GetBuffer"/>
/// on the underlying stream.</remarks>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="requiredSize">The minimum desired capacity for the stream.</param>
/// <param name="asContiguousBuffer">Whether to attempt to use a single contiguous buffer.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(string? tag, long requiredSize, bool asContiguousBuffer)
{
return this.GetStream(Guid.NewGuid(), tag, requiredSize, asContiguousBuffer);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <param name="offset">The offset from the start of the buffer to copy from.</param>
/// <param name="count">The number of bytes to copy from the buffer.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(Guid id, string? tag, byte[] buffer, int offset, int count)
{
RecyclableMemoryStream? stream = null;
try
{
stream = new RecyclableMemoryStream(this, id, tag, count);
stream.Write(buffer, offset, count);
stream.Position = 0;
return stream;
}
catch
{
stream?.Dispose();
throw;
}
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(byte[] buffer)
{
return this.GetStream(null, buffer, 0, buffer.Length);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <param name="offset">The offset from the start of the buffer to copy from.</param>
/// <param name="count">The number of bytes to copy from the buffer.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(string? tag, byte[] buffer, int offset, int count)
{
return this.GetStream(Guid.NewGuid(), tag, buffer, offset, count);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(Guid id, string? tag, ReadOnlySpan<byte> buffer)
{
RecyclableMemoryStream? stream = null;
try
{
stream = new RecyclableMemoryStream(this, id, tag, buffer.Length);
stream.Write(buffer);
stream.Position = 0;
return stream;
}
catch
{
stream?.Dispose();
throw;
}
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(ReadOnlySpan<byte> buffer)
{
return this.GetStream(null, buffer);
}
/// <summary>
/// Retrieve a new <see cref="RecyclableMemoryStream"/> object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
/// </summary>
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
/// <param name="buffer">The byte buffer to copy data from.</param>
/// <returns>A <see cref="RecyclableMemoryStream"/>.</returns>
public RecyclableMemoryStream GetStream(string? tag, ReadOnlySpan<byte> buffer)
{
return this.GetStream(Guid.NewGuid(), tag, buffer);
}
/// <summary>
/// Triggered when a new block is created.
/// </summary>
public event EventHandler<BlockCreatedEventArgs>? BlockCreated;
/// <summary>
/// Triggered when a new large buffer is created.
/// </summary>
public event EventHandler<LargeBufferCreatedEventArgs>? LargeBufferCreated;
/// <summary>
/// Triggered when a new stream is created.
/// </summary>
public event EventHandler<StreamCreatedEventArgs>? StreamCreated;
/// <summary>
/// Triggered when a stream is disposed.
/// </summary>
public event EventHandler<StreamDisposedEventArgs>? StreamDisposed;
/// <summary>
/// Triggered when a stream is disposed of twice (an error).
/// </summary>
public event EventHandler<StreamDoubleDisposedEventArgs>? StreamDoubleDisposed;
/// <summary>
/// Triggered when a stream is finalized.
/// </summary>
public event EventHandler<StreamFinalizedEventArgs>? StreamFinalized;
/// <summary>
/// Triggered when a stream is disposed to report the stream's length.
/// </summary>
public event EventHandler<StreamLengthEventArgs>? StreamLength;
/// <summary>
/// Triggered when a user converts a stream to array.
/// </summary>
public event EventHandler<StreamConvertedToArrayEventArgs>? StreamConvertedToArray;
/// <summary>
/// Triggered when a stream is requested to expand beyond the maximum length specified by the responsible RecyclableMemoryStreamManager.
/// </summary>
public event EventHandler<StreamOverCapacityEventArgs>? StreamOverCapacity;
/// <summary>
/// Triggered when a buffer of either type is discarded, along with the reason for the discard.
/// </summary>
public event EventHandler<BufferDiscardedEventArgs>? BufferDiscarded;
/// <summary>
/// Periodically triggered to report usage statistics.
/// </summary>
public event EventHandler<UsageReportEventArgs>? UsageReport;
}
}