Category : Files from Magazines
Archive   : MEMORY2A.ZIP
Filename : SUBMEM.C

 
Output of file : SUBMEM.C contained in archive : MEMORY2A.ZIP

/*----------------------------------------------------------
---*\
| SUB.C - Sub-segment allocation routines, for creating |
| local heaps in dynamically allocated segments. |
\*----------------------------------------------------------
---*/
#include

typedef DWORD HANDLE32;

/*----------------------------------------------------------
---*\
| Static Data Definitions. |
\*----------------------------------------------------------
---*/
HANDLE hSegment;

/*----------------------------------------------------------
---*\
| SubInitialize - Call first to allocate a segment from the |
| global heap. |
\*----------------------------------------------------------
---*/
BOOL FAR PASCAL SubInitialize(VOID)
{
BOOL bRetVal;
LPSTR lp;
WORD wSeg;
WORD wSize;

hSegment = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT,
4096);
if (!hSegment)
return FALSE;

lp = GlobalLock (hSegment);
if (!lp)
return FALSE;

wSeg = HIWORD (lp);
wSize = (WORD)GlobalSize (hSegment) - 16;

bRetVal = LocalInit (wSeg, 0, wSize);

GlobalUnlock (hSegment);
GlobalUnlock (hSegment); /* Undo LocalInit's GlobalLock. */

return bRetVal;
}


/*----------------------------------------------------------
---*\
| SubAlloc - Allocate a subsegment. |
| |
| Input: wFlags = local heap allocation flags. |
| wBytes = number of bytes to allocate. |
| |
| Returns: A 4-byte "handle", put together as: |
| HIWORD = Handle to global segment. |
| LOWORD = Handle to local object. |
\*----------------------------------------------------------
---*/
HANDLE32 FAR PASCAL SubAlloc(WORD wFlags, WORD wBytes)
{
HANDLE hMem;
LPSTR lp;
WORD wSeg;

lp = GlobalLock (hSegment);
if (!lp)
return 0L;

wSeg = HIWORD (lp);

_asm { push ds
mov ax, wSeg
mov ds, ax }

hMem = LocalAlloc (wFlags, wBytes);

_asm { pop ds }

GlobalUnlock (hSegment);

if (!hMem)
return 0L;
else
return MAKELONG (hMem, hSegment);
}

/*----------------------------------------------------------
---*\
| SubFree - Free a subsegment. |
| |
| Input: A 4-byte "handle", put together as: |
| HIWORD = Handle to global segment. |
| LOWORD = Handle to local object. |
| |
| Returns: The original handle, if successful. Otherwise, |
| returns NULL. |
\*----------------------------------------------------------
---*/
HANDLE32 FAR PASCAL SubFree(HANDLE32 hSubMem)
{
HANDLE hSeg;
HANDLE hMem;
LPSTR lp;
WORD wSeg;

hSeg = HIWORD (hSubMem);
hMem = LOWORD (hSubMem);

lp = GlobalLock (hSeg);
if (!lp)
return 0L;

wSeg = HIWORD (lp);

_asm { push ds
mov ax, wSeg
mov ds, ax }

hMem = LocalFree (hMem);

_asm { pop ds }

GlobalUnlock (hSeg);

if (!hMem)
return 0L;
else
return MAKELONG (hMem, hSeg);
}

/*----------------------------------------------------------
---*\
| SubLock - Lock a subsegment and the segment it lives in. |
| |
| Input: A 4-byte "handle", put together as: |
| HIWORD = Handle to global segment. |
| LOWORD = Handle to local object. |
| |
| Returns: A far pointer to the object. |
\*----------------------------------------------------------
---*/
LPSTR FAR PASCAL SubLock(HANDLE32 hSubMem)
{
HANDLE hSeg;
HANDLE hMem;
LPSTR lp;
PSTR p;
WORD wSeg;

hSeg = HIWORD (hSubMem);
hMem = LOWORD (hSubMem);

lp = GlobalLock (hSeg);
if (!lp)
return 0L;

wSeg = HIWORD (lp);

_asm { push ds
mov ax, wSeg
mov ds, ax }

p = LocalLock (hMem);

_asm { pop ds }

/* No Matching GlobalUnlock -- leave segment locked */
/* for caller to use. We'll unlock twice in SubUnlock. */

if (!p)
return (LPSTR)0;
else
return (LPSTR)MAKELONG (p, wSeg);
}

/*----------------------------------------------------------
---*\
| SubSize - Returns the size of a subsegment. |
| |
| Input: A 4-byte "handle", put together as: |
| HIWORD = Handle to global segment. |
| LOWORD = Handle to local object. |
| |
| Returns: a WORD value with the subsegment size, or zero |
| if the handle is invalid. |
\*----------------------------------------------------------
---*/
WORD FAR PASCAL SubSize(HANDLE32 hSubMem)
{
HANDLE hSeg;
HANDLE hMem;
LPSTR lp;
WORD wSeg;
WORD wSize;

hSeg = HIWORD (hSubMem);
hMem = LOWORD (hSubMem);

lp = GlobalLock (hSeg);
if (!lp)
return 0;

wSeg = HIWORD (lp);

_asm { push ds
mov ax, wSeg
mov ds, ax }

wSize = LocalSize (hMem);

_asm { pop ds }

GlobalUnlock (hSeg);

return wSize;
}

/*----------------------------------------------------------
---*\
| SubUnlock - Unlock a subsegment, and the segment it lives |
| in. |
| Input: A 4-byte "handle", put together as: |
| HIWORD = Handle to global segment. |
| LOWORD = Handle to local object. |
| |
| Returns: The LocalUnlock return value, which is zero if |
| the block's reference count was decreased to |
| zero, otherwise it is non-zero. |
\*----------------------------------------------------------
---*/
BOOL FAR PASCAL SubUnlock(HANDLE32 hSubMem)
{
BOOL bRetVal;
HANDLE hSeg;
HANDLE hMem;
LPSTR lp;
WORD wSeg;

hSeg = HIWORD (hSubMem);
hMem = LOWORD (hSubMem);

lp = GlobalLock (hSeg);
if (!lp)
return 0L;

wSeg = HIWORD (lp);

_asm { push ds
mov ax, wSeg
mov ds, ax }

bRetVal = LocalUnlock (hMem);

_asm { pop ds }

GlobalUnlock (hSeg);
GlobalUnlock (hSeg);

return bRetVal;
}



  3 Responses to “Category : Files from Magazines
Archive   : MEMORY2A.ZIP
Filename : SUBMEM.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/