[comp.windows.ms] How to use lots of mem in a Win program?

michaelt@microsoft.UUCP (Michael THURLKILL) (08/31/90)

In article <prk.651226036@aries> prk@planet.bt.co.uk (Peter Knight) writes:
>burgoyne@eng.umd.edu (John R. Burgoyne) writes:
>
>
>>The question I am trying to get answered is the following. How do we go about
>>using large amounts of memory in our programs we develop with SDK version
>>3.0? I am using MS C compiler 6.0, SDK version 3.0, Windows version 3.0. The
>>MS Windows SDK book "Tools" says on page 1-3 that the compact and large
>>models are not recommended for Windows programs because data segments of
>>programs created with compact or large models are fixed, and because only one
>>instance of such programs can be run. If anyone can answer any of the
>>following questions, I would be very happy if you do so.
>
>>Can one use the medium model and have data larger than 64K if it is global?
>
Well, by definition, medium model has only one 64k data segment, so
through normal C calls you can't access > 64k. What you will want to
do ideally is use the Windows GlobalAlloc function to allocate memory
from the global heap. If you allocate a memory block, using GlobalAlloc,
of <= 64k, you will use a pointer declared as FAR to access the memory.
If you allocate a block > 64k (in Standard mode the maximum size of
an allocation is 1mb. in Enhanced mode the maximum size is either 
16mb or there is no limit, I forget), you will use a pointer declared
as HUGE to access the memory. Although fmalloc and hmalloc can be used,
it is generally recommended to use the Windows GlobalAlloc function
directly. fmalloc and hmalloc map to GlobalAlloc(GMEM_FIXED, xxx).
There is currently a problem in that GlobalAlloc(GMEM_FIXED, xxx) leaves
the memory page locked. Thus, for practical purposes it is best to call
GlobalAlloc(GMEM_MOVEABLE, xxx) and then call GlobalLock to get a pointer
to the memory.
>
>>Can one use the medium model and have global arrays larger than 64K?
>
>Yes and no.  You can only manipulate these arrays if they are gotten 
>by using the far malloc call.  They will have to be accessed by pointers
>
Correct. Although, as mentioned above, it would probably be better to 
use GlobalAlloc rather than fmalloc.

>
>>What is the general strategy for using lots of memory in a Windows program?
>
Since in Standard mode and Enhanced mode of Win3 Windows apps are run
in protect mode, you have direct access to extended memory. Thus, the 
recommended stragegy is to call GlobalAlloc to allocate memory. In real
mode, you may need to make LIM 3.2 calls to allocate memory from 
expanded memory (if available) if there isn't enough memory available
below 640k.

Mike Thurlkill