Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ArrayBuffer.cs
Go to the documentation of this file.
1
using
System.Buffers
;
2
using
System.Runtime.InteropServices
;
3
4
namespace
System.Net
;
5
6
[StructLayout(
LayoutKind
.Auto)]
7
internal
struct
ArrayBuffer :
IDisposable
8
{
9
private
readonly
bool
_usePool
;
10
11
private
byte
[]
_bytes
;
12
13
private
int
_activeStart
;
14
15
private
int
_availableStart
;
16
17
public
int
ActiveLength
=>
_availableStart
-
_activeStart
;
18
19
public
Span<byte>
ActiveSpan
=>
new
Span<byte>
(
_bytes
,
_activeStart
,
_availableStart
-
_activeStart
);
20
21
public
ReadOnlySpan<byte>
ActiveReadOnlySpan
=>
new
ReadOnlySpan<byte>
(
_bytes
,
_activeStart
,
_availableStart
-
_activeStart
);
22
23
public
int
AvailableLength
=> _bytes.Length -
_availableStart
;
24
25
public
Memory<byte>
AvailableMemory
=>
_bytes
.AsMemory(
_availableStart
);
26
27
public
ArrayBuffer
(
int
initialSize,
bool
usePool =
false
)
28
{
29
_usePool
= usePool;
30
_bytes
= (usePool ?
ArrayPool<byte>
.
Shared
.Rent(initialSize) :
new
byte
[initialSize]);
31
_activeStart
= 0;
32
_availableStart
= 0;
33
}
34
35
public
void
Dispose
()
36
{
37
_activeStart
= 0;
38
_availableStart
= 0;
39
byte
[]
bytes
=
_bytes
;
40
_bytes
=
null
;
41
if
(
_usePool
&&
bytes
!=
null
)
42
{
43
ArrayPool<byte>
.
Shared
.Return(
bytes
);
44
}
45
}
46
47
public
void
Discard
(
int
byteCount
)
48
{
49
_activeStart
+=
byteCount
;
50
if
(
_activeStart
==
_availableStart
)
51
{
52
_activeStart
= 0;
53
_availableStart
= 0;
54
}
55
}
56
57
public
void
Commit
(
int
byteCount
)
58
{
59
_availableStart
+=
byteCount
;
60
}
61
62
public
void
EnsureAvailableSpace
(
int
byteCount
)
63
{
64
if
(
byteCount
<=
AvailableLength
)
65
{
66
return
;
67
}
68
int
num =
_activeStart
+
AvailableLength
;
69
if
(
byteCount
<= num)
70
{
71
Buffer
.
BlockCopy
(
_bytes
,
_activeStart
,
_bytes
, 0,
ActiveLength
);
72
_availableStart
=
ActiveLength
;
73
_activeStart
= 0;
74
return
;
75
}
76
int
num2 =
ActiveLength
+
byteCount
;
77
int
num3 =
_bytes
.Length;
78
do
79
{
80
num3 *= 2;
81
}
82
while
(num3 < num2);
83
byte
[]
array
= (
_usePool
?
ArrayPool<byte>
.
Shared
.Rent(num3) :
new
byte
[num3]);
84
byte
[]
bytes
=
_bytes
;
85
if
(
ActiveLength
!= 0)
86
{
87
Buffer
.
BlockCopy
(
bytes
,
_activeStart
,
array
, 0,
ActiveLength
);
88
}
89
_availableStart
=
ActiveLength
;
90
_activeStart
= 0;
91
_bytes
=
array
;
92
if
(
_usePool
)
93
{
94
ArrayPool<byte>
.
Shared
.Return(
bytes
);
95
}
96
}
97
}
System.Buffer.BlockCopy
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition
Buffer.cs:102
System.Buffer
Definition
Buffer.cs:8
System.Buffers.ArrayPool.Shared
static ArrayPool< T > Shared
Definition
ArrayPool.cs:7
System.Buffers.ArrayPool
Definition
ArrayPool.cs:4
System.IDisposable
Definition
IDisposable.cs:4
System.Buffers
Definition
Base64.cs:7
System.Net
Definition
HttpClientJsonExtensions.cs:8
System.Runtime.InteropServices.LayoutKind
LayoutKind
Definition
LayoutKind.cs:4
System.Runtime.InteropServices
Definition
SequenceMarshal.cs:4
System.ExceptionArgument.bytes
@ bytes
System.ExceptionArgument.byteCount
@ byteCount
System.ExceptionArgument.array
@ array
System.Memory
Definition
Memory.cs:14
System.Net.ArrayBuffer.ArrayBuffer
ArrayBuffer(int initialSize, bool usePool=false)
Definition
ArrayBuffer.cs:27
System.Net.ArrayBuffer.AvailableMemory
Memory< byte > AvailableMemory
Definition
ArrayBuffer.cs:27
System.Net.ArrayBuffer._usePool
readonly bool _usePool
Definition
ArrayBuffer.cs:9
System.Net.ArrayBuffer.ActiveSpan
Span< byte > ActiveSpan
Definition
ArrayBuffer.cs:19
System.Net.ArrayBuffer._bytes
byte[] _bytes
Definition
ArrayBuffer.cs:11
System.Net.ArrayBuffer.ActiveLength
int ActiveLength
Definition
ArrayBuffer.cs:17
System.Net.ArrayBuffer.ActiveReadOnlySpan
ReadOnlySpan< byte > ActiveReadOnlySpan
Definition
ArrayBuffer.cs:21
System.Net.ArrayBuffer._activeStart
int _activeStart
Definition
ArrayBuffer.cs:13
System.Net.ArrayBuffer.Dispose
void Dispose()
Definition
ArrayBuffer.cs:35
System.Net.ArrayBuffer.Commit
void Commit(int byteCount)
Definition
ArrayBuffer.cs:57
System.Net.ArrayBuffer._availableStart
int _availableStart
Definition
ArrayBuffer.cs:15
System.Net.ArrayBuffer.Discard
void Discard(int byteCount)
Definition
ArrayBuffer.cs:47
System.Net.ArrayBuffer.AvailableLength
int AvailableLength
Definition
ArrayBuffer.cs:23
System.Net.ArrayBuffer.EnsureAvailableSpace
void EnsureAvailableSpace(int byteCount)
Definition
ArrayBuffer.cs:62
System.ReadOnlySpan
Definition
ReadOnlySpan.cs:14
System.Span
Definition
Span.cs:14
source
System.Net.Security
System.Net
ArrayBuffer.cs
Generated by
1.10.0