15.1 Overview

LINK can create DOS programs with up to 2047 “overlays.” Overlays are sections of a program that are loaded into memory only as needed. With overlays, you can run a DOS program that would otherwise be too large to fit into available memory. Under DOS, available memory is the portion of conventional memory that is not taken up by system or other programs. An overlaid program can fit into available memory if the size of its root (the part of the program that always remains in memory) and the memory needed for the overlay area do not exceed the size of available memory.

An overlaid program consists of one .EXE file. The file remains open during program execution. MOVE reads this file when it needs to load an overlay. Overlays are loaded on call; initially, only the root of the program is loaded into memory.

Overlays occupy a heap located in the remaining available memory that is not occupied by the root. More than one overlay can be in memory at a time. When the program calls a function contained in an overlay that is not already in memory, the overlay is copied into the overlay heap from the executable file. If the overlay heap does not have enough room for the new overlay, an existing overlay is discarded. MOVE uses a form of a least recently used (LRU) algorithm to determine which overlay to discard.

If expanded or extended memory is available, MOVE can cache the discarded overlays in this memory. Caching saves time when a discarded overlay is needed again because copying an overlay from expanded or extended memory to conventional memory takes less time than reading it in from the disk. If the cache is too small to hold all discarded overlays, MOVE uses a form of an LRU algorithm to decide which overlay to overwrite. If neither extended nor expanded memory is available, MOVE must open the executable file and read in each overlay as it is needed.

Overlaid programs execute more slowly than nonoverlaid programs because of the time needed to swap overlays. You can optimize your program by grouping functions that call each other into the same overlay or into overlays that are in memory at the same time.

Only code segments can be put into overlays. Data segments must go into the root. To swap data to disk or to expanded or extended memory, use virtual memory. See “Using the Virtual Memory Manager” in Chapter 4, “Managing Memory in C,” in the Programming Techniques manual.