-

Difference between revisions of "Memory Models"

From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search
(Created page with "With many of the processors supported by the HI-TECH C compilers there are more than one address space accessible to a program. Typically one address space is more economical...")
 
 
Line 9: Line 9:
  
 
This concept of different address spaces is not catered
 
This concept of different address spaces is not catered
for by either K&R or ANSI C (except to recognize the possi-
+
for by either K&R or ANSI C (except to recognize the possibility of separate address spaces for code and data).
bility of separate address spaces for code and data).
 
 
Without any extensions to the language itself it is possible
 
Without any extensions to the language itself it is possible
 
to devise more than one memory model for a given processor,
 
to devise more than one memory model for a given processor,
Line 18: Line 17:
 
that program's memory requirements.
 
that program's memory requirements.
  
In many programs, however, only one or two data struc-
+
In many programs, however, only one or two data structures are large enough to need to be placed in the larger
tures are large enough to need to be placed in the larger
 
 
address space. Selection of a "large" memory model for the
 
address space. Selection of a "large" memory model for the
 
whole of the program makes the whole program larger and
 
whole of the program makes the whole program larger and
Line 29: Line 27:
 
the following criteria:
 
the following criteria:
  
1. As far as possible the extensions should be consistent
+
# As far as possible the extensions should be consistent with common practice.
with common practice.
+
# The extensions should fit a machine-independent model to maximize portability across processors and operating systems.
 
 
2. The extensions should fit a machine-independent model
 
to maximize portability across processors and operating
 
systems.
 
  
 
These goals have been achieved within HI-TECH C by
 
These goals have been achieved within HI-TECH C by
Line 41: Line 35:
 
Each memory model defines three address spaces
 
Each memory model defines three address spaces
 
each for code and data. These address spaces are
 
each for code and data. These address spaces are
known as the _�n_�e_�a_�r, _�f_�a_�r and _�d_�e_�f_�a_�u_�l_�t spaces. Any
+
known as the <strong>near, far</strong> and <strong>default</strong> spaces. Any
 
object qualified by the near keyword will be
 
object qualified by the near keyword will be
placed in the _�n_�e_�a_�r address space, any object qual-
+
placed in the <strong>near</strong> address space, any object qualified by the far keyword shall be placed in the
ified by the far keyword shall be placed in the
+
<strong>far</strong> address space, and all other objects shall be
_�f_�a_�r address space, and all other objects shall be
+
placed in the <strong>default</strong> address space. The <strong>near</strong>
placed in the _�d_�e_�f_�a_�u_�l_�t address space. The _�n_�e_�a_�r
+
address space shall be a (possibly improper) subspace of the <strong>default</strong> address space, while the
address space shall be a (possibly improper) sub-
+
<strong>default</strong> address space shall be a (possibly
space of the _�d_�e_�f_�a_�u_�l_�t address space, while the
+
improper) subspace of the <strong>far</strong> address space. There
_�d_�e_�f_�a_�u_�l_�t address space shall be a (possibly
+
shall be up to three kinds of pointers corresponding to the three address spaces, each capable of
improper) subspace of the _�f_�a_�r address space. There
 
shall be up to three kinds of pointers correspond-
 
ing to the three address spaces, each capable of
 
 
addressing an object in its own address space or a
 
addressing an object in its own address space or a
 
subspace of that address space.
 
subspace of that address space.
  
This implies that the address of an object may be con-
+
This implies that the address of an object may be converted to a pointer into a larger address space, e.g. a <strong>near</strong>
verted to a pointer into a larger address space, e.g. a _�n_�e_�a_�r
+
object may have its address converted to a pointer to <strong>far</strong>,
object may have its address converted to a pointer to _�f_�a_�r,
+
but a <strong>far</strong> object may not be able to be addressed by a
but a _�f_�a_�r object may not be able to be addressed by a
+
pointer to <strong>near</strong>.
pointer to _�n_�e_�a_�r.
 
  
In practice the _�d_�e_�f_�a_�u_�l_�t address space will usually
+
In practice the <strong>default</strong> address space will usually
correspond exactly to either the _�n_�e_�a_�r or _�f_�a_�r address spaces.
+
correspond exactly to either the <strong>near</strong> or <strong>far</strong> address spaces.
 
If all three address spaces correspond to the same memory
 
If all three address spaces correspond to the same memory
 
then there is only one memory model possible. This occurs
 
then there is only one memory model possible. This occurs
with the 68000 processor. Where the _�d_�e_�f_�a_�u_�l_�t code and data
+
with the 68000 processor. Where the <strong>default</strong> code and data
spaces may each correspond to either the _�n_�e_�a_�r or _�f_�a_�r address
+
spaces may each correspond to either the <strong>near</strong> or <strong>far</strong> address
 
spaces then there will be a total of four memory models.
 
spaces then there will be a total of four memory models.
 
This is the case with the 8086 processor.
 
This is the case with the 8086 processor.
Line 79: Line 69:
 
described above.
 
described above.
  
This model also corresponds well with other implementa-
+
This model also corresponds well with other implementations using the near and far keywords, although such implementations do not appear to have been designed around a formal, portable model.
tions using the near and far keywords, although such imple-
 
mentations do not appear to have been designed around a for-
 
mal, portable model.
 

Latest revision as of 19:13, 30 July 2017

With many of the processors supported by the HI-TECH C compilers there are more than one address space accessible to a program. Typically one address space is more economical to access than another, larger address space. Thus it is desirable to be able to tailor a prorgram's use of memory to achieve the greatest economy in addressing (thus minimizing program size and maximizing speed) while allowing access to as much memory as the program requires.

This concept of different address spaces is not catered for by either K&R or ANSI C (except to recognize the possibility of separate address spaces for code and data). Without any extensions to the language itself it is possible to devise more than one memory model for a given processor, selected at compile time. This has the effect of selecting one addressing method for all data and/or code. This permits the model for a particular program to be chosen depending on that program's memory requirements.

In many programs, however, only one or two data structures are large enough to need to be placed in the larger address space. Selection of a "large" memory model for the whole of the program makes the whole program larger and slower just to allow a few large data structures. This can be overcome by allowing individual selection of the address space for each data structure. Unfortunately this entails extensions to the language, never a desirable approach. To minimize the effect of such extensions they should satisfy the following criteria:

  1. As far as possible the extensions should be consistent with common practice.
  2. The extensions should fit a machine-independent model to maximize portability across processors and operating systems.

These goals have been achieved within HI-TECH C by means of the following model:

Each memory model defines three address spaces each for code and data. These address spaces are known as the near, far and default spaces. Any object qualified by the near keyword will be placed in the near address space, any object qualified by the far keyword shall be placed in the far address space, and all other objects shall be placed in the default address space. The near address space shall be a (possibly improper) subspace of the default address space, while the default address space shall be a (possibly improper) subspace of the far address space. There shall be up to three kinds of pointers corresponding to the three address spaces, each capable of addressing an object in its own address space or a subspace of that address space.

This implies that the address of an object may be converted to a pointer into a larger address space, e.g. a near object may have its address converted to a pointer to far, but a far object may not be able to be addressed by a pointer to near.

In practice the default address space will usually correspond exactly to either the near or far address spaces. If all three address spaces correspond to the same memory then there is only one memory model possible. This occurs with the 68000 processor. Where the default code and data spaces may each correspond to either the near or far address spaces then there will be a total of four memory models. This is the case with the 8086 processor.

The keywords far and near are supported by all the HI- TECH C compilers, but the exact correspondence of address spaces is determined by the individual characteristics of each processor and the choice of memory model (if there is a choice). However code written using these keywords will be portable providing it obeys the constraints of the model described above.

This model also corresponds well with other implementations using the near and far keywords, although such implementations do not appear to have been designed around a formal, portable model.