Files
mirror-processhacker/trunk/ProcessHacker/Win32/Memory/MemoryAlloc.cs
T
wj32 d540ac4f8a Process list has less CPU usage (again); so does lsass.exe
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@379 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2008-12-22 10:03:55 +00:00

205 lines
6.6 KiB
C#

/*
* Process Hacker
*
* Copyright (C) 2008 wj32
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Runtime.InteropServices;
namespace ProcessHacker
{
/// <summary>
/// Represents an unmanaged memory allocation.
/// </summary>
public class MemoryAlloc : IDisposable
{
private bool _freed = false;
private IntPtr _memory;
private int _size;
public static implicit operator int(MemoryAlloc memory)
{
return memory.Memory.ToInt32();
}
public static implicit operator IntPtr(MemoryAlloc memory)
{
return memory.Memory;
}
/// <summary>
/// Creates a memory allocation from an existing pointer. The allocation
/// referenced by the pointer will be freed automatically. The Size
/// property will be set to 0.
/// </summary>
/// <param name="memory">A pointer to an existing memory allocation.</param>
/// <returns>A new instance of a memory allocation.</returns>
public static MemoryAlloc FromPointer(IntPtr memory)
{
return new MemoryAlloc(0) { _memory = memory };
}
/// <summary>
/// Creates a new, invalid memory allocation.
/// You must set the pointer using the Memory property.
/// </summary>
protected MemoryAlloc()
{ }
/// <summary>
/// Creates a new memory allocation with the specified size.
/// </summary>
/// <param name="size">The amount of memory, in bytes, to allocate.</param>
public MemoryAlloc(int size)
{
_memory = Marshal.AllocHGlobal(size);
_size = size;
}
/// <summary>
/// Reads a signed integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <returns>The integer.</returns>
public int ReadInt32(int offset)
{
return this.ReadInt32(offset, 0);
}
/// <summary>
/// Reads a signed integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <param name="index">The index at which to begin reading, after the offset is added.</param>
/// <returns>The integer.</returns>
public int ReadInt32(int offset, int index)
{
return Marshal.ReadInt32(_memory,
offset + index * 4);
}
/// <summary>
/// Reads an unsigned integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <returns>The integer.</returns>
public uint ReadUInt32(int offset)
{
return this.ReadUInt32(offset, 0);
}
/// <summary>
/// Reads an unsigned integer.
/// </summary>
/// <param name="offset">The offset at which to begin reading.</param>
/// <param name="index">The index at which to begin reading, after the offset is added.</param>
/// <returns>The integer.</returns>
public uint ReadUInt32(int offset, int index)
{
return (uint)this.ReadInt32(offset, index);
}
/// <summary>
/// Creates a struct from the memory allocation.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <returns>The new struct.</returns>
public T ReadStruct<T>()
{
return this.ReadStruct<T>(0);
}
/// <summary>
/// Creates a struct from the memory allocation.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <param name="index">The index at which to begin reading to the struct. This is multiplied by
/// the size of the struct.</param>
/// <returns>The new struct.</returns>
public T ReadStruct<T>(int index)
{
return this.ReadStruct<T>(0, index);
}
/// <summary>
/// Creates a struct from the memory allocation.
/// </summary>
/// <typeparam name="T">The type of the struct.</typeparam>
/// <param name="offset">The offset to add before reading.</param>
/// <param name="index">The index at which to begin reading to the struct. This is multiplied by
/// the size of the struct.</param>
/// <returns>The new struct.</returns>
public T ReadStruct<T>(int offset, int index)
{
return (T)Marshal.PtrToStructure(
new IntPtr(_memory.ToInt32() + offset + Marshal.SizeOf(typeof(T)) * index), typeof(T));
}
/// <summary>
/// Resizes the memory allocation.
/// </summary>
/// <param name="newSize">The new size of the allocation.</param>
public virtual void Resize(int newSize)
{
_memory = Marshal.ReAllocHGlobal(_memory, new IntPtr(newSize));
_size = newSize;
}
/// <summary>
/// Gets a pointer to the allocated memory.
/// </summary>
public IntPtr Memory
{
get { return _memory; }
protected set { _memory = value; }
}
/// <summary>
/// Gets the size of the allocated memory.
/// </summary>
public virtual int Size
{
get { return _size; }
}
protected virtual void Free()
{
Marshal.FreeHGlobal(_memory);
}
~MemoryAlloc()
{
this.Dispose();
}
/// <summary>
/// Frees the allocated memory.
/// </summary>
public void Dispose()
{
lock (this)
{
if (!_freed)
{
_freed = true;
this.Free();
}
}
}
}
}