Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Unmanaged Memory Allocations
[ All Languages » C# »  Memory]

Total Hit ( 7608)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


If you are going to be using pointers, it may make sense to allocate from an unmanaged heap. For the vast majority of needs is not any better than the using the garbage collector, but if you are allocating a large amount of memory, especially for short periods of time, need better control of when memory is release, Win32 provides a set of Memory Management functions.

The class below is from the ECMA C# Standard specification. I had created a similar Memory class, which I can't seem to find at the moment, which includes the Win32 CopyMemory, FillMemory, MoveMemory and ZeroMemory functions, missing below. I will be replacing this sometime soon.

Click here to copy the following block
using System;
using System.Runtime.InteropServices;

public unsafe class Memory
{
  // Handle for the process heap. This handle is used in all calls to the
  // HeapXXX APIs in the methods below.

  static int ph = GetProcessHeap();

  // Private instance constructor to prevent instantiation.

  private Memory() {}

  // Allocates a memory block of the given size. The allocated memory is
  // automatically initialized to zero.

  public static void* Alloc(int size)
  {
    void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);
    if (result == null) throw new OutOfMemoryException();
    return result;
  }

  // Copies count bytes from src to dst. The source and destination
  // blocks are permitted to overlap.

  public static void Copy(void* src, void* dst, int count)
  {
    byte* ps = (byte*)src;
    byte* pd = (byte*)dst;
    if (ps > pd) {
      for (; count != 0; count--) *pd++ = *ps++;
    } else if (ps < pd) {
      for (ps += count, pd += count; count != 0; count--)
        *--pd = *--ps;  
    }
  }

  // Frees a memory block.

  public static void Free(void* block) {
    if (!HeapFree(ph, 0, block)) throw new InvalidOperationException();
  }

  // Re-allocates a memory block. If the reallocation request is for a
  // larger size, the additional region of memory is automatically
  // initialized to zero.

  public static void* ReAlloc(void* block, int size) {
    void* result = HeapReAlloc(ph, HEAP_ZERO_MEMORY, block, size);
    if (result == null) throw new OutOfMemoryException();
    return result;
  }

  // Returns the size of a memory block.
  public static int SizeOf(void* block) {
    int result = HeapSize(ph, 0, block);
    if (result == -1) throw new InvalidOperationException();
    return result;
  }

  // Heap API flags
  const int HEAP_ZERO_MEMORY = 0x00000008;

  // Heap API functions

  [DllImport("kernel32")]
  static extern int GetProcessHeap();

  [DllImport("kernel32")]
  static extern void* HeapAlloc(int hHeap, int flags, int size);

  [DllImport("kernel32")]
  static extern bool HeapFree(int hHeap, int flags, void* block);

  [DllImport("kernel32")]
  static extern void* HeapReAlloc(int hHeap, int flags, void* block,
   int size);

  [DllImport("kernel32")]
  static extern int HeapSize(int hHeap, int flags, void* block);
}


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.