Category : BASIC Source Code
Archive   : HUGARY.ZIP
Filename : SJSGREAL.C

 
Output of file : SJSGREAL.C contained in archive : HUGARY.ZIP


#define NOCOMM
#include
#include

#include "sjslib.h"

/* Similar to Window's GlobalReAlloc, except if GlobalReAlloc fails,
this function will attempt to simulate it using GlobalAlloc and fmemcpy. */
/* VBM: Declare Function SjsGlobalReAlloc% Lib "sjslib.dll" Alias "SjsGlobalReAlloc" (ByVal hMem%, ByVal dwBytes&, ByVal wFlags%) */
HANDLE FAR PASCAL
SjsGlobalReAlloc(HANDLE hOldMem, DWORD dwBytes, WORD wFlags)
{
HANDLE hNewMem;
DWORD dwOldSize;
HPBYTE hpbOld;
HPBYTE hpbNew;
WORD wCopyBytes;
DWORD dwNumBlocks;

/* Attempt to use Windows function for reallocation. */
if ((hNewMem = GlobalReAlloc(hOldMem, dwBytes, wFlags)) != NULL)
return hNewMem;

/* Attempt to create new block of memory. */
if ((hNewMem = GlobalAlloc(wFlags, dwBytes)) == NULL)
return NULL;

/* Get size of original block. */
dwOldSize = GlobalSize(hOldMem);
/* Only copy up to the smaller of the two block sizes. */
if (dwBytes < dwOldSize)
dwOldSize = dwBytes;

/* Lock old block. */
if ((hpbOld = GlobalLock(hOldMem)) == NULL)
{
/* Free new block; old handle remains valid. */
GlobalFree(hNewMem);
return NULL;
}

/* Lock new block. */
if ((hpbNew = GlobalLock(hNewMem)) == NULL)
{
/* Unlock old block, free new block; old handle remains valid. */
GlobalUnlock(hOldMem);
GlobalFree(hNewMem);
return NULL;
}

/* Copy half of a segment at one time. */
wCopyBytes = (WORD) (dwSEGSIZE / 2UL);
/* Copy up through the last full segment. */
dwNumBlocks = (dwOldSize / dwSEGSIZE) * 2UL;

while (dwNumBlocks-- > 0)
{
_fmemcpy(hpbNew, hpbOld, (size_t) wCopyBytes);
hpbNew += wCopyBytes;
hpbOld += wCopyBytes;
wCopyBytes = (WORD) (dwSEGSIZE - wCopyBytes);
}

/* Copy part of last segment used. */
if ((wCopyBytes = (WORD) (dwOldSize % dwSEGSIZE)) > 0)
_fmemcpy(hpbNew, hpbOld, (size_t) wCopyBytes);

/* Unlock the new block; unlock and free the old one. */
GlobalUnlock(hNewMem);
GlobalUnlock(hOldMem);
GlobalFree(hOldMem);
return hNewMem;
}