-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slight reworks around custom memory management
- Loading branch information
Showing
5 changed files
with
110 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace K4AdotNet | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="ICustomMemoryAllocator"/> interface | ||
/// via <see cref="Marshal.AllocHGlobal(int)"/> and <see cref="Marshal.FreeHGlobal(IntPtr)"/> methods. | ||
/// </summary> | ||
public sealed class HGlobalMemoryAllocator : ICustomMemoryAllocator | ||
{ | ||
/// <summary>Singleton.</summary> | ||
public static readonly ICustomMemoryAllocator Instance = new HGlobalMemoryAllocator(); | ||
|
||
/// <summary>For internal usage.</summary> | ||
internal static readonly Sensor.NativeApi.MemoryDestroyCallback MemoryDestroyCallback = new(ReleaseUnmanagedBuffer); | ||
|
||
private HGlobalMemoryAllocator() | ||
{ } | ||
|
||
/// <summary>Allocates a buffer of size at least <paramref name="size"/> bytes.</summary> | ||
/// <param name="size">Minimum size in bytes needed for the buffer. Cannot be negative.</param> | ||
/// <param name="context">Not used. Always <see cref="IntPtr.Zero"/>.</param> | ||
/// <returns>A pointer to the newly allocated memory. This memory must be released using the <see cref="Free(IntPtr, IntPtr)"/> method.</returns> | ||
public IntPtr Allocate(int size, out IntPtr context) | ||
{ | ||
context = IntPtr.Zero; | ||
return Marshal.AllocHGlobal(size); | ||
} | ||
|
||
/// <summary>Frees memory previously allocated by <see cref="Allocate(int, out IntPtr)"/> method.</summary> | ||
/// <param name="buffer">The handle returned by the original matching call to <see cref="Allocate(int, out IntPtr)"/> method.</param> | ||
/// <param name="context">Not used.</param> | ||
public void Free(IntPtr buffer, IntPtr context) | ||
=> ReleaseUnmanagedBuffer(buffer, context); | ||
|
||
private static void ReleaseUnmanagedBuffer(IntPtr buffer, IntPtr context) | ||
=> Marshal.FreeHGlobal(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters