Skip to content

Commit

Permalink
MemoryPool
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay authored Sep 6, 2024
1 parent ea3ef6f commit de75cbd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CsharpLangExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MulticastDelegates", "Multi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArrayPool", "ArrayPool\ArrayPool.csproj", "{FDB639A1-C301-4C4C-955F-DE77A2517176}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MemoryPool", "MemoryPool\MemoryPool.csproj", "{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -225,6 +227,10 @@ Global
{FDB639A1-C301-4C4C-955F-DE77A2517176}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDB639A1-C301-4C4C-955F-DE77A2517176}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDB639A1-C301-4C4C-955F-DE77A2517176}.Release|Any CPU.Build.0 = Release|Any CPU
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1C093BB-C3D5-4ABF-B3EB-F05646C11638}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions MemoryPool/MemoryPool.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
26 changes: 26 additions & 0 deletions MemoryPool/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Buffers;

// MemoryPool<T> Class
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.memorypool-1?view=net-8.0
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Memory/src/System/Buffers/MemoryPool.cs

// Use the singleton pool
// https://stackoverflow.com/questions/73661594/whats-the-point-of-c-sharp-memory-pool
using var owner = MemoryPool<double>.Shared.Rent(100);
Memory<double> mem = owner.Memory;

MemoryPool<byte> memoryPool = MemoryPool<byte>.Shared;
IMemoryOwner<byte> memoryOwner = memoryPool.Rent(1024);
Memory<byte> memory = memoryOwner.Memory;

try
{
Span<byte> span = memory.Span;
span[0] = 42;
}
finally
{
memoryOwner.Dispose();
}

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Examples of C# programming, with the goal of tracking and staying up-to-date wit
* [Dynamic types](DynamicTypes/)
* [Multicast Delegates](MulticastDelegates/)
* [ArrayPool](ArrayPool/)
* [MemoryPool](MemoryPool/)

## Notes
- [Which C# version is included in which framework version?](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version)
Expand Down

0 comments on commit de75cbd

Please sign in to comment.