Online document ___________________________________________________________________________ Windows memory management CONTENTS ___________________________________________________________________________ Windows memory Mixed-model programming: Addressing management 1 modifiers . . . . . . . . . . . . 7 Running out of memory . . . . . . 1 Segment pointers . . . . . . . 8 Memory models . . . . . . . . . . 1 Declaring far objects . . . . . 9 The '86 registers . . . . . . . 2 Declaring functions to be near or General-purpose registers . . 2 far . . . . . . . . . . . . . . 9 Segment registers . . . . . . 2 Declaring pointers to be near, Special-purpose registers . . 2 far, or huge . . . . . . . . . 10 The flags register . . . . . . 3 Pointing to a given Memory segmentation . . . . . . 3 segment:offset address . . . 11 Pointers . . . . . . . . . . . . 4 Using library files . . . . . . 12 Near pointers . . . . . . . . 4 Linking mixed modules . . . . . 12 Far pointers . . . . . . . . . 4 Huge pointers . . . . . . . . 5 Index 14 The four memory models . . . . . 5 This document covers See Chapter 8, o What to do when you receive "Out of memory" errors. "Building a Windows o What memory models are: how to choose one, and why application," in you would (or wouldn't) want to use a particular the User's Guide memory model. for information on choosing a memory model for Windows modules. =========================================================================== Running out of memory =========================================================================== Borland C++ does not generate any intermediate data structures to disk when it is compiling (Borland C++ writes only .OBJ files to disk); instead it uses RAM for intermediate data structures between passes. Because of this, you might encounter the message "Out of memory" if there is not enough memory available for the compiler. The solution to this problem is to make your functions smaller, or to split up the file that has large functions. =========================================================================== Memory models =========================================================================== Borland C++ gives you four memory models, each suited for different program and code sizes. Each memory model uses memory differently. What do you need to know to use memory models? To answer that question, we have to - 1 - See page 5 for a take a look at the computer system you're working on. summary of each Its central processing unit (CPU) is a microprocessor memory model. belonging to the Intel iAPx86 family; an 80286, 80386, or 80486. For now, we'll just refer to it as an '86. The '86 registers ======================================================= These are some of the registers found in the '86 processor. There are other registers--but they can't be accessed directly, so they're not shown here. Figure not available online '86 registers ------------------ The general-purpose registers are the ones used most General-purpose often to hold and manipulate data. Each has some registers special functions that only it can do. For example, ------------------ o Some math operations can only be done using AX. o BX can be used as an index register. o CX is used by LOOP and some string instructions. o DX is implicitly used for some math operations. But there are many operations that all these registers can do; in many cases, you can freely exchange one for another. ------------------ The segment registers hold selectors which reference Segment registers segment descriptors. These descriptors provide ------------------ information about the starting address of the segment, size, access control, and use. ------------------ The '86 also has some special-purpose registers: Special-purpose registers o The SI and DI registers can do many of the things the ------------------ general-purpose registers can, plus they are used as index registers. They're also used by Borland C++ for register variables. o The SP register points to the current top-of-stack and is an offset into the stack segment. o The BP register is a secondary stack pointer, usually used to index into the stack in order to retrieve arguments or automatic variables. Borland C++ functions use the base pointer (BP) register as a base address for arguments and automatic - 2 - variables. Parameters have positive offsets from BP, which vary depending on the memory model. BP points to the saved previous BP value if there is a stack frame. Functions that have no arguments will not use or save BP if the Standard Stack Frame option is Off. Automatic variables are given negative offsets from BP. The offsets depend on how much space has already been assigned to local variables. ------------------ The 16-bit flags register contains all pertinent The flags register information about the state of the '86 and the results ------------------ of recent instructions. For example, if you wanted to know whether a subtraction produced a zero result, you would check the zero flag (the Z bit in the flags register) immediately after the instruction; if it were set, you would know the result was zero. Other flags, such as the carry and overflow flags, similarly report the results of arithmetic and logical operations. Figure not available online Flags register of the '86 Other flags control modes of operation of the '86. The direction flag controls the direction in which the string instructions move, and the interrupt flag controls whether external hardware, such as a keyboard or modem, is allowed to halt the current code temporar- ily so that urgent needs can be serviced. The trap flag is used only by software that debugs other software. The flags register isn't usually modified or read directly. Instead, the flags register is generally controlled through special assembler instructions (such as CLD, STI, and CMC) and through arithmetic and logical instructions that modify certain flags. Likewise, the contents of certain bits of the flags register affect the operation of instructions such as JZ, RCR, and MOVSB. The flags register is not really used as a storage location, but rather holds the status and control data for the '86. Memory ======================================================= segmentation - 3 - The Intel '86 microprocessor has a segmented memory architecture. It has a total address space of 16MB, but The 80386 and it is designed to directly address only 64K of memory 80486 processors at a time. actually have a total address o The '86 keeps track of four different segments: code, space of four data, stack, and extra. The code segment is where the gigabytes and machine instructions are; the data segment, where their segments information is; the stack is, of course, the stack; needn't be as and the extra segment is also used for extra data. small as 64K, but Windows 3.x o The '86 has four 16-bit segment registers (one for doesn't change the each segment) named CS, DS, SS, and ES; these point segment size. to the code, data, stack, and extra segments, respectively. Pointers ======================================================= Although you can declare a pointer or function to be a specific type regardless of the model used, by default the type of memory model you choose determines the default type of pointers used for code and data. Pointers come in four flavors: near (16 bits...
mek4test