As I noted before, global memory ranges from the spot where MS-DOS first loads Windows to the top of available memory. At the bottom of global memory (the area with the lowest memory address), Windows allocates fixed segments. Fixed segments are allocated from the bottom up. At the top of global memory, Windows allocates discardable code segments. (Remember that discardable segments are also moveable segments.) Discardable segments are allocated from the top down.
Between fixed segments and discardable segments, Windows allocates moveable segments and nondiscardable data segments. The largest block of free memory is usually located below the discardable segments. The memory layout looks something like that shown in Figure 7-1 on the following page, with arrows indicating the direction in which the areas expand. When Windows needs to allocate a fixed segment, it starts searching from the bottom up for a sufficiently large free block below the area of moveable segments. If it can't find one, it starts moving moveable segments up in memory to make room. If that doesn't work, Windows begins discarding discardable segments, based on an LRU (least recently used) algorithm, again moving moveable segments. To allocate moveable but nondiscardable segments, Windows searches the free memory area below the discardable segments. If it doesn't find enough room, Windows moves other moveable segments down in memory and eventually starts discarding discardable segments.
FIGURE 7-1 ILLUSTRATION WITH LABELS AND ARTWORK
Within the area of discardable memory, Windows maintains a space large enough to accommodate the largest code segment of every currently running program. Windows never runs out of memory space when reloading code segments. However, Windows can run out of memory space when a program attempts to allocate global memory or load a resource. Sometimes it can be a little tricky to deal with this problem—you may need a text string or icon not currently in memory to display an error message. If your program needs to report that it is low on memory, you can use a message box. Windows keeps in memory all the code necessary to create a message box. You'll want to use the MB_SYSTEMMODAL flag to prevent the user from switching to another application. MB_ICONHAND (which is supposed to accompany messages about severe problems) is also always in memory. The text message in the message box should either be in your default data segment or be a string resource that has previously been copied into your data segment.