mirror of
https://github.com/digital-jellyfish/Virtu.git
synced 2024-11-23 19:30:59 +00:00
eaaab47a6c
--HG-- extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4019155
138 lines
4.6 KiB
C#
138 lines
4.6 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Runtime.ConstrainedExecution;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security;
|
|
using System.Security.Permissions;
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
namespace Jellyfish.Library
|
|
{
|
|
[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
|
|
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
|
|
public abstract class SafeAllocHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|
{
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
|
protected SafeAllocHandle(bool ownsHandle) :
|
|
base(ownsHandle)
|
|
{
|
|
}
|
|
}
|
|
|
|
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
|
|
public sealed class SafeGlobalAllocHandle : SafeAllocHandle
|
|
{
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
|
public SafeGlobalAllocHandle() :
|
|
base(true)
|
|
{
|
|
}
|
|
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
|
public SafeGlobalAllocHandle(IntPtr existingHandle, bool ownsHandle) :
|
|
base(ownsHandle)
|
|
{
|
|
SetHandle(existingHandle);
|
|
}
|
|
|
|
public static SafeGlobalAllocHandle Allocate(int size)
|
|
{
|
|
return Allocate(0x0, size);
|
|
}
|
|
|
|
public static SafeGlobalAllocHandle Allocate(byte[] value)
|
|
{
|
|
SafeGlobalAllocHandle alloc = Allocate(value.Length);
|
|
Marshal.Copy(value, 0, alloc.DangerousGetHandle(), value.Length);
|
|
|
|
return alloc;
|
|
}
|
|
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
return (NativeMethods.GlobalFree(handle) == IntPtr.Zero);
|
|
}
|
|
|
|
private static SafeGlobalAllocHandle Allocate(uint flags, int size)
|
|
{
|
|
SafeGlobalAllocHandle alloc = NativeMethods.GlobalAlloc(flags, (IntPtr)size);
|
|
if (alloc.IsInvalid)
|
|
{
|
|
throw new Win32Exception();
|
|
}
|
|
|
|
return alloc;
|
|
}
|
|
|
|
[SuppressUnmanagedCodeSecurity]
|
|
private static class NativeMethods
|
|
{
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
public static extern SafeGlobalAllocHandle GlobalAlloc(uint dwFlags, IntPtr sizetBytes);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
|
public static extern IntPtr GlobalFree(IntPtr hMem);
|
|
}
|
|
}
|
|
|
|
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
|
|
public sealed class SafeLocalAllocHandle : SafeAllocHandle
|
|
{
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
|
public SafeLocalAllocHandle() :
|
|
base(true)
|
|
{
|
|
}
|
|
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
|
public SafeLocalAllocHandle(IntPtr existingHandle, bool ownsHandle) :
|
|
base(ownsHandle)
|
|
{
|
|
SetHandle(existingHandle);
|
|
}
|
|
|
|
public static SafeLocalAllocHandle Allocate(int size)
|
|
{
|
|
return Allocate(0x0, size);
|
|
}
|
|
|
|
public static SafeLocalAllocHandle Allocate(byte[] value)
|
|
{
|
|
SafeLocalAllocHandle alloc = Allocate(value.Length);
|
|
Marshal.Copy(value, 0, alloc.DangerousGetHandle(), value.Length);
|
|
|
|
return alloc;
|
|
}
|
|
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
return (NativeMethods.LocalFree(handle) == IntPtr.Zero);
|
|
}
|
|
|
|
private static SafeLocalAllocHandle Allocate(uint flags, int size)
|
|
{
|
|
SafeLocalAllocHandle alloc = NativeMethods.LocalAlloc(flags, (IntPtr)size);
|
|
if (alloc.IsInvalid)
|
|
{
|
|
throw new Win32Exception();
|
|
}
|
|
|
|
return alloc;
|
|
}
|
|
|
|
[SuppressUnmanagedCodeSecurity]
|
|
private static class NativeMethods
|
|
{
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
public static extern SafeLocalAllocHandle LocalAlloc(uint dwFlags, IntPtr sizetBytes);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
|
public static extern IntPtr LocalFree(IntPtr hMem);
|
|
}
|
|
}
|
|
}
|