C memory model

C memory model

Memory models in the C programming language are a way to specify assumptions that the compiler should make when generating code for segmented memory or 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.

Memory segmentation

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 = 84359

Note that this process leads to aliasing 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.

Pointer sizes

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 megabyte of memory. Note thatpointer arithmetic (addition and subtraction) does not modifythe segment portion of the pointer, only its offset. Operations which exceed the bounds of zero or 65355 (0xFFFF) will undergo modulo 64K operation just as any normal 16 bit operation.

For example, the code below will wrap around and overwrite itself:

char far* myfarptr = (char far*) 0x50000000L ; unsigned long counter ; for(counter=0; counter<128*1024; counter++) // access 128K memory *(myfarptr+counter) = 7 ; // write all 7s into it

The moment "counter" becomes (0x10000), the resulting absolute address will roll over to 0x5000:0000.

"Huge" pointers are essentially far pointers, but are normalized every time they are modified so that they have the highest possiblesegment for that address. This is very slow but allows thepointer to point to multiple segments, and allows for accurate pointercomparisons, as if the platform were a flat memory model.

Memory models

The memory models are:

* In the Tiny model, all four segment registers point to thesame segment. In all models with near data pointers, SS equals DS.

Other platforms

In protected mode a segment cannot be writable, readable and executable.Fact|date=April 2007 Therefore, when implementing the Small and Tiny memory models the code segment register must point to the same physical address and have the same limit as the data segment register. 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 is never allowed). However, on the 80386, with its flat memory model it is possible to protect individual memory pages against writing.Fact|date=April 2007

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 which support that.Fact|date=April 2007 However segmentation in 32-bit mode does not allow to access a larger address space than what a 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.Fact|date=April 2007 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.Fact|date=April 2007 Technically, the "flat" 32-bit address space is a "tiny" memory model for the segmented address space.Fact|date=April 2007

References

* "Turbo C++ Version 3.0 User's Guide". Borland International, Copyright 1992.

ee also

* Memory model (computing)
* segmented memory
* x86
* flat memory model
* pointers
* Memory segment


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Memory model — may refer to: Psychology Atkinson–Shiffrin memory model Baddeley s model of working memory Memory prediction model Informatics In computer software, a memory model describes how threads interact through memory, or what assumptions the compiler… …   Wikipedia

  • Memory model (computing) — In computing, a memory model describes the interactions of threads through memory and specifies the assumptions the compiler is allowed to make when generating code for segmented memory or paged memory platforms. History and significance A memory …   Wikipedia

  • Flat memory model — In computer systems design, a flat memory model refers to a linear addressing paradigm, such that the CPU can directly (and sequentially) address all of the available memory locations, without having to resort to any sort of bank switching or… …   Wikipedia

  • Java Memory Model — The Java memory model describes how threads in the Java programming language interact through memory. Together with the description of single threaded execution of code, the memory model provides the semantics of the Java programming language.… …   Wikipedia

  • Intel Memory Model — The Intel x86 cpu has six memory models which control how the segment registers are used and the default size of pointers. Contents 1 Memory segmentation 2 Pointer sizes 3 Memory models 4 …   Wikipedia

  • Atkinson–Shiffrin memory model — Note that in this diagram, sensory memory is detached from either form of memory, and represents its development from short term and long term memory, due to its storage being used primarily on a run time basis for physical or psychosomatic… …   Wikipedia

  • Atkinson-Shiffrin memory model — The Atkinson Shiffrin model, Multi store model or Multi memory model is a psychological model proposed in 1968 by Richard Atkinson and Richard Shiffrin as a proposal for the structure of memory. It proposed that human memory involves a sequence… …   Wikipedia

  • Memory consolidation — is a category of processes that stabilize a memory trace after the initial acquisition.[1] Consolidation is distinguished into two specific processes, synaptic consolidation, which occurs within the first few hours after learning, and system… …   Wikipedia

  • Memory barrier — Memory barrier, also known as membar or memory fence or fence instruction, is a type of barrier and a class of instruction which causes a central processing unit (CPU) or compiler to enforce an ordering constraint on memory operations issued… …   Wikipedia

  • Memory architecture — describes the methods used to implement electronic computer data storage in a manner that is a combination of the fastest, most reliable, most durable, and least expensive way to store and retrieve information. Depending on the specific… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”