Files
mirror-processhacker/1.x/trunk/ProcessHacker.Native/Memory/AlignedMemoryAlloc.cs
T
dmex 056f4c0914 PH1.x: Code Cleanup/Fixes
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@4784 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2011-10-30 02:47:41 +00:00

36 lines
1.1 KiB
C#

using System;
using ProcessHacker.Common;
namespace ProcessHacker.Native
{
public sealed class AlignedMemoryAlloc : MemoryAlloc
{
private readonly IntPtr _realMemory;
public AlignedMemoryAlloc(int size, int alignment)
{
// Make sure the alignment is positive and a power of two.
if (alignment <= 0 || alignment.CountBits() != 1)
throw new ArgumentOutOfRangeException("alignment");
// Since we are going to align our pointer, we need to account for
// any padding at the beginning.
_realMemory = PrivateHeap.Allocate(size + alignment - 1);
// aligned memory = (memory + alignment - 1) & ~(alignment - 1)
this.Memory = _realMemory.Align(alignment);
this.Size = size;
}
protected override void Free()
{
PrivateHeap.Free(_realMemory);
}
public override void Resize(int newSize)
{
throw new NotSupportedException();
}
}
}