15.2.1 Using Code Segments

A code segment is one or more bytes (but never more than 64K) of machine instructions. It represents all or part of an application's program instructions.

Important:

You must not store writable data in code segments; writing to a code segment causes a general-protection (GP) fault when your Windows version 3.1 application runs. Windows will, however, allow you to store read-only data, such as a jump table, in a code segment. For more information about running applications with Windows 3.1, see Chapter 16, “More Memory Management.”

Every application has at least one code segment. For example, the sample applications described in previous chapters have one and only one code segment. You can also create an application that has multiple code segments. In fact, most Windows applications have multiple code segments. By using multiple code segments, you reduce the size of any given code segment to the number of instructions needed to carry out some task. If you also make these segments discardable, you effectively minimize the memory requirements of your application's code segments.

When you create medium- or large-model applications, you are creating applications that use multiple code segments. Medium- and large-model applications typically have one or more source files for each segment. When working with multiple source files, compile each source file separately and explicitly name the segment to which the compiled code will belong. Then link the application, defining the segments' attributes in the application's module-definition file.

To define a segment's attributes, use the SEGMENTS statement in the module-definition file. The following example shows definitions for three segments:

SEGMENTS
        PAINT_TEXT MOVEABLE DISCARDABLE
        INIT_TEXT MOVEABLE DISCARDABLE
        WNDPROC_TEXT MOVEABLE DISCARDABLE

You may also use the CODE statement in the module-definition file to define the default attributes for all code segments. The CODE statement also defines attributes for any segments that are not explicitly defined in the SEGMENTS statement. The following example shows how to make all segments not listed in the SEGMENTS statement discardable:

CODE MOVEABLE DISCARDABLE

If you use discardable code segments in your application, you must balance segment discarding with the number of times the segment may be accessed. For example, the segment containing your main window procedure should probably not be discardable, because Windows calls the procedure often. Typically, this segment is small (approximately 4K). Because a discarded segment has to be loaded from disk when needed, the memory savings you may realize by discarding the window procedure may be offset by the performance loss that comes with accessing the disk often. To optimize performance, you should ensure that the only things in the segment containing the main window procedure are called frequently by the system.

Note:

A code segment in a library can be fixed or movable. If it is movable, it is automatically made discardable.