C language > Work in progress

VirtualReAlloc

(1/1)

lance:
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: ---   //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;
      }


--- End code ---

Navigation

[0] Message Index

Go to full version