NO

Author Topic: VirtualReAlloc  (Read 5318 times)

lance

  • Guest
VirtualReAlloc
« on: November 03, 2007, 07:59:26 am »
There is speculation that the new Vista will limit malloc/calloc/realloc memory for applications.
The alternative is to use VirtualAlloc, however Microsoft does not have a realloc equivalent, so I wrote one.

I think that it works, however I haven't extensively tested it...could you would please try it out.
And post hints, comments, suggestions, etc... 8)

Code: [Select]
   //Lance's new Microsoft VirtualRealloc (beta)
   LPVOID VirtualRealloc(LPVOID lpAddress, DWORD dwSize, DWORD flProtect)
      {
      DWORD dBytesOld;
      LPVOID lpAddressNew;
      MEMORY_BASIC_INFORMATION mbiStatus;

      //Check size of current array in bytes, quit if no bytes available.
      if(!VirtualQuery(lpAddress, &mbiStatus, sizeof(mbiStatus))) return NULL;
dBytesOld=mbiStatus.RegionSize;
if(!dBytesOld) return NULL;

      //Create a new array, quit if failed to create.
      lpAddressNew=VirtualAlloc(NULL,dwSize,MEM_COMMIT,flProtect);
if(!lpAddressNew) return NULL;

      VirtualQuery(lpAddressNew, &mbiStatus, sizeof(mbiStatus));

      //Copy old array onto new array, making sure not to exceed any boundries.
      if(dBytesOld>mbiStatus.RegionSize) dBytesOld=mbiStatus.RegionSize;
      CopyMemory(lpAddressNew,(void*)lpAddress,dBytesOld);

      //Free old array and return address of new array.
      VirtualFree((void*)lpAddress,0,MEM_RELEASE);
      return lpAddressNew;
      }

« Last Edit: November 03, 2007, 08:00:57 am by lance »