-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSafeCoTaskMemAllocHandle.cs
60 lines (52 loc) · 1.66 KB
/
SafeCoTaskMemAllocHandle.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
/////////////////////////////////////////////////////////////////////////////////
// paint.net //
// Copyright (C) dotPDN LLC, Rick Brewster, and contributors. //
// All Rights Reserved. //
/////////////////////////////////////////////////////////////////////////////////
using PaintDotNet.Interop;
using System;
using System.Runtime.InteropServices;
namespace PaintDotNet.MemoryManagement
{
public sealed class SafeCoTaskMemAllocHandle
: SafeHandleZeroIsInvalid
{
public static SafeCoTaskMemAllocHandle Alloc(int cb)
{
SafeCoTaskMemAllocHandle safeHandle = new SafeCoTaskMemAllocHandle();
IntPtr pBuffer = Marshal.AllocCoTaskMem(cb);
safeHandle.TakeHandle(ref pBuffer);
return safeHandle;
}
public IntPtr Address
{
get
{
return this.handle;
}
}
private SafeCoTaskMemAllocHandle()
: base(true)
{
}
public SafeCoTaskMemAllocHandle(bool ownsHandle)
: base(ownsHandle)
{
}
public SafeCoTaskMemAllocHandle(IntPtr pBuffer, bool ownsHandle)
: base(ownsHandle)
{
SetHandle(pBuffer);
}
private void TakeHandle(ref IntPtr pBuffer)
{
SetHandle(pBuffer);
pBuffer = IntPtr.Zero;
}
protected override bool ReleaseHandle()
{
Marshal.FreeCoTaskMem(this.handle);
return true;
}
}
}