Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d44f3e8

Browse files
dotnet-bottarekgh
authored andcommitted
Mirror changes from dotnet/corefx (#15909)
* Consolidate System.Memory code to shared folder (dotnet/corefx#26393) * Consolidate System.Memory code to shared folder This change is removing the duplicate codes from System.Memory and keep only one copy under the shared folder to be easier to edit such code in one place and get reflected on the other repos. * Address the review feedback * Addressing more feedback * More cleanup * remove empty line and added a comment Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com> * Add missing throw helper methods used in the code we got from corefx * Update the exception helper * fix the break
1 parent 25d6ccb commit d44f3e8

File tree

9 files changed

+247
-73
lines changed

9 files changed

+247
-73
lines changed

src/mscorlib/Resources/Strings.resx

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
4-
Microsoft ResX Schema
5-
3+
<!--
4+
Microsoft ResX Schema
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
51-
51+
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
@@ -2866,6 +2866,9 @@
28662866
<data name="Marshaler_StringTooLong" xml:space="preserve">
28672867
<value>Marshaler restriction: Excessively long string.</value>
28682868
</data>
2869+
<data name="MemoryDisposed" xml:space="preserve">
2870+
<value>Memory&lt;T&gt; has been disposed.</value>
2871+
</data>
28692872
<data name="MissingConstructor_Name" xml:space="preserve">
28702873
<value>Constructor on type '{0}' not found.</value>
28712874
</data>

src/mscorlib/shared/System/Buffers/IRetainable.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@
77

88
namespace System.Buffers
99
{
10+
/// <summary>
11+
/// Provides a mechanism for manual lifetime management.
12+
/// </summary>
1013
public interface IRetainable
1114
{
15+
/// <summary>
16+
/// Call this method to indicate that the IRetainable object is in use.
17+
/// Do not dispose until Release is called.
18+
/// </summary>
1219
void Retain();
20+
/// <summary>
21+
/// Call this method to indicate that the IRetainable object is no longer in use.
22+
/// The object can now be disposed.
23+
/// </summary>
1324
bool Release();
1425
}
1526
}

src/mscorlib/shared/System/Buffers/MemoryHandle.cs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,46 @@
77

88
namespace System.Buffers
99
{
10+
/// <summary>
11+
/// A handle for the memory.
12+
/// </summary>
1013
public unsafe struct MemoryHandle : IDisposable
1114
{
12-
private IRetainable _owner;
15+
private IRetainable _retainable;
1316
private void* _pointer;
1417
private GCHandle _handle;
1518

19+
/// <summary>
20+
/// Creates a new memory handle for the memory.
21+
/// </summary>
22+
/// <param name="retainable">reference to manually managed object</param>
23+
/// <param name="pointer">pointer to memory, or null if a pointer was not provided when the handle was created</param>
24+
/// <param name="handle">handle used to pin array buffers</param>
1625
[CLSCompliant(false)]
17-
public MemoryHandle(IRetainable owner, void* pointer = null, GCHandle handle = default(GCHandle))
26+
public MemoryHandle(IRetainable retainable, void* pointer = null, GCHandle handle = default(GCHandle))
1827
{
19-
_owner = owner;
28+
_retainable = retainable;
2029
_pointer = pointer;
2130
_handle = handle;
2231
}
2332

33+
/// <summary>
34+
/// Returns the pointer to memory, or null if a pointer was not provided when the handle was created.
35+
/// </summary>
36+
[CLSCompliant(false)]
37+
public void* Pointer => _pointer;
38+
39+
/// <summary>
40+
/// Returns false if the pointer to memory is null.
41+
/// </summary>
42+
public bool HasPointer => _pointer != null;
43+
44+
/// <summary>
45+
/// Adds an offset to the pinned pointer.
46+
/// </summary>
47+
/// <exception cref="System.ArgumentNullException">
48+
/// Throw when pinned pointer is null.
49+
/// </exception>
2450
internal void AddOffset(int offset)
2551
{
2652
if (_pointer == null)
@@ -33,26 +59,24 @@ internal void AddOffset(int offset)
3359
}
3460
}
3561

36-
[CLSCompliant(false)]
37-
public void* Pointer => _pointer;
38-
39-
public bool HasPointer => _pointer != null;
40-
41-
public void Dispose()
42-
{
43-
if (_handle.IsAllocated)
62+
/// <summary>
63+
/// Frees the pinned handle and releases IRetainable.
64+
/// </summary>
65+
public void Dispose()
66+
{
67+
if (_handle.IsAllocated)
4468
{
4569
_handle.Free();
4670
}
4771

48-
if (_owner != null)
72+
if (_retainable != null)
4973
{
50-
_owner.Release();
51-
_owner = null;
74+
_retainable.Release();
75+
_retainable = null;
5276
}
5377

54-
_pointer = null;
78+
_pointer = null;
5579
}
56-
80+
5781
}
5882
}

src/mscorlib/shared/System/Buffers/OwnedMemory.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,88 @@
77

88
namespace System.Buffers
99
{
10+
/// <summary>
11+
/// Owner of Memory<typeparamref name="T"/> that provides appropriate lifetime management mechanisms for it.
12+
/// </summary>
1013
public abstract class OwnedMemory<T> : IDisposable, IRetainable
1114
{
15+
/// <summary>
16+
/// The number of items in the Memory<typeparamref name="T"/>.
17+
/// </summary>
1218
public abstract int Length { get; }
1319

20+
/// <summary>
21+
/// Returns a span wrapping the underlying memory.
22+
/// </summary>
1423
public abstract Span<T> Span { get; }
1524

25+
/// <summary>
26+
/// Returns a Memory<typeparamref name="T"/> if the underlying memory has not been freed.
27+
/// </summary>
28+
/// <exception cref="System.ObjectDisposedException">
29+
/// Thrown when the underlying memory has already been disposed.
30+
/// </exception>
1631
public Memory<T> Memory
1732
{
18-
get
33+
get
1934
{
20-
if (IsDisposed)
35+
if (IsDisposed)
2136
{
22-
ThrowHelper.ThrowObjectDisposedException(nameof(OwnedMemory<T>), ExceptionResource.Memory_ThrowIfDisposed);
37+
ThrowHelper.ThrowObjectDisposedException_MemoryDisposed();
2338
}
2439
return new Memory<T>(owner: this, 0, Length);
2540
}
2641
}
2742

43+
/// <summary>
44+
/// Returns a handle for the array that has been pinned and hence its address can be taken
45+
/// </summary>
2846
public abstract MemoryHandle Pin();
2947

48+
/// <summary>
49+
/// Returns an array segment.
50+
/// </summary>
3051
protected internal abstract bool TryGetArray(out ArraySegment<T> arraySegment);
3152

53+
/// <summary>
54+
/// Implements IDisposable.
55+
/// </summary>
56+
/// <exception cref="System.InvalidOperationException">
57+
/// Throw when there are still retained references to the memory
58+
/// </exception>
3259
public void Dispose()
3360
{
34-
if (IsRetained)
61+
if (IsRetained)
3562
{
36-
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Memory_OutstandingReferences);
63+
ThrowHelper.ThrowInvalidOperationException_OutstandingReferences();
3764
}
3865
Dispose(true);
3966
GC.SuppressFinalize(this);
4067
}
4168

69+
/// <summary>
70+
/// Clean up of any leftover managed and unmanaged resources.
71+
/// </summary>
4272
protected abstract void Dispose(bool disposing);
4373

74+
/// <summary>
75+
/// Return true if someone is holding a reference to the memory.
76+
/// </summary>
4477
protected abstract bool IsRetained { get; }
4578

79+
/// <summary>
80+
/// Return true if the underlying memory has been freed.
81+
/// </summary>
4682
public abstract bool IsDisposed { get; }
4783

84+
/// <summary>
85+
/// Implements IRetainable. Prevent accidental disposal of the memory.
86+
/// </summary>
4887
public abstract void Retain();
4988

89+
/// <summary>
90+
/// Implements IRetainable. The memory can now be diposed.
91+
/// </summary>
5092
public abstract bool Release();
5193

5294
}

0 commit comments

Comments
 (0)