/*
* 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 .
*/
using System;
using System.Runtime.InteropServices;
namespace ProcessHacker
{
///
/// Represents an unmanaged memory allocation.
///
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;
}
///
/// 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.
///
/// A pointer to an existing memory allocation.
/// A new instance of a memory allocation.
public static MemoryAlloc FromPointer(IntPtr memory)
{
return new MemoryAlloc(0) { _memory = memory };
}
///
/// Creates a new memory allocation with the specified size.
///
/// The amount of memory, in bytes, to allocate.
public MemoryAlloc(int size)
{
_memory = Marshal.AllocHGlobal(size);
_size = size;
}
///
/// Reads a signed integer.
///
/// The offset at which to begin reading.
/// The integer.
public int ReadInt32(int offset)
{
return this.ReadInt32(offset, 0);
}
///
/// Reads a signed integer.
///
/// The offset at which to begin reading.
/// The index at which to begin reading, after the offset is added.
/// The integer.
public int ReadInt32(int offset, int index)
{
return Marshal.ReadInt32(_memory,
offset + index * 4);
}
///
/// Reads an unsigned integer.
///
/// The offset at which to begin reading.
/// The integer.
public uint ReadUInt32(int offset)
{
return this.ReadUInt32(offset, 0);
}
///
/// Reads an unsigned integer.
///
/// The offset at which to begin reading.
/// The index at which to begin reading, after the offset is added.
/// The integer.
public uint ReadUInt32(int offset, int index)
{
return (uint)this.ReadInt32(offset, index);
}
///
/// Creates a struct from the memory allocation.
///
/// The type of the struct.
/// The new struct.
public T ReadStruct()
{
return this.ReadStruct(0);
}
///
/// Creates a struct from the memory allocation.
///
/// The type of the struct.
/// The index at which to begin reading to the struct. This is multiplied by
/// the size of the struct.
/// The new struct.
public T ReadStruct(int index)
{
return this.ReadStruct(0, index);
}
///
/// Creates a struct from the memory allocation.
///
/// The type of the struct.
/// The offset to add before reading.
/// The index at which to begin reading to the struct. This is multiplied by
/// the size of the struct.
/// The new struct.
public T ReadStruct(int offset, int index)
{
return (T)Marshal.PtrToStructure(
new IntPtr(_memory.ToInt32() + offset + Marshal.SizeOf(typeof(T)) * index), typeof(T));
}
///
/// Resizes the memory allocation.
///
/// The new size of the allocation.
public void Resize(int newSize)
{
_memory = Marshal.ReAllocHGlobal(_memory, new IntPtr(newSize));
_size = newSize;
}
///
/// Gets a pointer to the allocated memory.
///
public IntPtr Memory
{
get { return _memory; }
}
///
/// Gets the size of the allocated memory.
///
public int Size
{
get { return _size; }
}
~MemoryAlloc()
{
this.Dispose();
}
///
/// Frees the allocated memory.
///
public void Dispose()
{
lock (this)
{
if (!_freed)
{
_freed = true;
Marshal.FreeHGlobal(_memory);
}
}
}
}
}