Memory model
Memory models in the
C programming language are a way to specify assumptionsthat the
compiler should make when generating code for
segmented memoryor
paged memory platforms.
For example, on the 16-bit
x86 platform, six memory models exist. They control what assumptions are made regarding the segment
registers,and the default size of pointers.
Four registers are used to refer to four segments on the 16-bit
x86segmented memory architecture.
DS (data segment),
CS (code segment),
SS (stack segment), and
ES (extra segment).A logical address on this platform is written
segment:
offset, in
hexadecimal. In real mode, in order to calculate the physical address of a byte of memory, one left-shifts the contents of the appropriate register 4 bits, and then adds the offset.
For example the logical address 7522:F139 yields the 20-bit physical address:
75220 + F139 = 84359Note that this process leads to
aliasing (computing) of memory, such that any givenphysical address may have multiple logical representations. This makes comparison of pointers difficult.
In
protected mode, the
GDT and
LDT are used for this purpose.
Pointers can either be
near,
far, or
huge.
Near pointers referto the current segment, so neither DS nor CS must be modified to dereference thepointer. They are the fastest pointers, but are limited to point to 64kilobytes of memory (the current segment).
Far pointers contain the new value of DS or CS within them. To usethem the register must be changed, the memory dereferenced, and then theregister restored. They may reference up to 1
gibibyte of memory. Note thatpointer arithmetic, such as addition and subtraction, if done directly,
never modifythe segment portion of the pointer, only its offset until the new offset exceeds 0xFFFF or is under 0.
Huge pointers are essentially far pointers, but are normalized every time they are modified so that they have the smallest possiblesegment for that address. This is very slow and is harder and even slower in
protected mode, but allows thepointer to point to multiple segments, and allows for accurate pointercomparisons, as if the platform were a
flat memory model.
The memory models are:
| Model | Data | Code |-
| Small
| near
| near
|-
| Medium
| near
| far
|-
| Compact
| far
| near
|-
| Large
| far
| far
|-
| Huge
| huge
| huge
|-
| Tiny*
| near
| near
|-
* In the Tiny model, all four segment registers point to thesame segment. In all models with near data pointers,
SS equals
DS.
Protected mode note: When implementing the Small and Tiny memory models in
protected mode, because a segment
cannot be writable, readable
and executable, and therefore the code segment register
have to point to a different selector that points to the same physical address and have the same limit. This defeated one of the features of the
80286, which makes sure data segments are never executable and code segment are never writable (which means that
self-modifying code are never allowed), which is the same feature that the implementation of the flat address space on the
80386 also defeats nowadays, but must be done because the program expects that. However on the 80386 it is possible to protect individual memory pages against writing.
Memory models are not limited to 16-bit programs. It is possible to use segmentation in 32-bit protected mode as well (resulting in 48-bit pointers) and there exist C language compilers (eg.
CAD-UL) which support that. However segmentation in 32-bit mode does not allow to access a larger address space than what an single segment would cover, unless some segments are not always present in memory and the linear address space is just used as a
cache over a larger segmented virtual space. It mostly allows to better protect access to various objects (areas up to 1 megabyte long can benefit from a 1-byte access protection granularity, versus the coarse 4 KiB granularity offered by sole paging), and is therefore only used in specialized applications, like telecommunications software. Technically, the "flat" 32-bit address space is a "tiny" memory model for the segmented address space.
*
Turbo C++ Version 3.0 User's Guide. Borland International, Copyright 1992.
*
segmented memory*
x86*
flat memory model*
pointers*
Memory segment