.. highlight:: none


.. index::
   pair: C interface; design

.. _design-interface-c:


C interface design
==================

.. mps:prefix:: design.mps.interface.c


Introduction
------------

:mps:tag:`scope` This document is the design for the Memory Pool System
(MPS) interface to the C Language, impl.h.mps.

:mps:tag:`bg` See `mail.richard.1996-07-24.10-57`_.

.. _mail.richard.1996-07-24.10-57: https://info.ravenbrook.com/project/mps/mail/1996/07/24/10-57/0.txt


Analysis
--------

Goals
.....

:mps:tag:`goal.c` The file impl.h.mps is the C external interface to the
MPS. It is the default interface between client code written in C and
the MPS.

:mps:tag:`goal.cpp` impl.h.mps is not specifically designed to be
an interface to C++, but should be usable from C++.


Requirements
............

:mps:tag:`req` The interface must provide an interface from client code
written in C to the functionality of the MPS required by the product
(see req.product), and Open Dylan (req.dylan).

:mps:tag:`req.separation` The external interface may not include internal
MPS header files (such as ``pool.h``).

:mps:tag:`req.flexibility` It is essential that the interface cope well with
change, in order to avoid restricting possible future MPS
developments. This means that the interface must be "open ended" in
its definitions. This accounts for some of the apparently tortuous
methods of doing things (such as the keyword argument mechanism; see
design.mps.keyword-arguments_). The requirement is that the MPS should
be able to add new functionality, or alter the implementation of
existing functionality, without affecting existing client code. A
stronger requirement is that the MPS should be able to change without
*recompiling* client code. This is not always possible.

.. _design.mps.keyword-arguments: keyword-arguments.html

:mps:tag:`req.name.iso` The interface shall not conflict in terms of
naming with any interfaces specified by ISO C and all reasonable
future versions.

:mps:tag:`req.name.general` The interface shall use a documented and
reasonably small portion of the namespace so that clients can use the
MPS C interface in combination with other interfaces without name
conflicts.


Architecture
------------

:mps:tag:`fig.arch` The architecture of the MPS Interface

[missing figure]

Just behind ``mps.h`` is the file ``mpsi.c``, the "MPS interface
layer" which does the job of converting types and checking parameters
before calling through to the MPS proper, using internal MPS methods.


Naming conventions
------------------

:mps:tag:`naming` The external interface names should adhere to the
documented interface conventions; these are found in the “`Interface
conventions <interface.html_>`_” chapter of the Reference Manual. They are
paraphrased/recreated here.

.. _interface.html: ../../../topic/interface.html

:mps:tag:`naming.file` All files in the external interface have names
starting with ``mps``.

:mps:tag:`naming.unixy` The external interface does not follow the same
naming conventions as the internal code. The interface is designed to
resemble a more conventional C, Unix, or Posix naming convention.

:mps:tag:`naming.case` Identifiers are in lower case, except
non-function-like macros, which are in upper case.

:mps:tag:`naming.global` All documented identifiers begin ``mps_`` or
:c:macro:`MPS_`.

:mps:tag:`naming.all` All identifiers defined by the MPS begin ``mps_`` or
:c:macro:`MPS_` or ``_mps_``.

:mps:tag:`naming.type` Types are suffixed ``_t``, except for structure and union types.

:mps:tag:`naming.struct` Structure types and tags are suffixed ``_s``.

:mps:tag:`naming.union` Unions types and tags are suffixed ``_u``.

:mps:tag:`naming.scope` The naming conventions apply to all identifiers (see
ISO C §6.1.2); this includes names of functions, variables, types
(through typedef), structure and union tags, enumeration members,
structure and union members, macros, macro parameters, labels.

:mps:tag:`naming.scope.labels` labels (for ``goto`` statements) should be
rare, only in special block macros and probably not even then.

:mps:tag:`naming.scope.other` The naming convention would also extend to
enumeration types and parameters in functions prototypes but both of
those are prohibited from having names in an interface file.


Type conventions
----------------

:mps:tag:`type.gen` The interface defines memory addresses as ``void *`` and
sizes as ``size_t`` for compatibility with standard C (in particular,
with :c:func:`malloc()`). These types must be binary compatible with the
internal types :c:type:`Addr` and :c:type:`Size` respectively. Note that this
restricts the definitions of the internal types :c:type:`Addr` and :c:type:`Size`
when the MPS is interfaced with C, but does not restrict the MPS in
general.

:mps:tag:`type.opaque` Opaque types are defined as pointers to structures
which are never defined. These types are cast to the corresponding
internal types in ``mpsi.c``.

:mps:tag:`type.trans` Some transparent structures are defined. The client is
expected to read these, or poke about in them, under documented
restrictions. The most important is the allocation point structure
(:c:type:`mps_ap_s`) which is part of allocation buffers. The transparent
structures must be binary compatible with corresponding internal
structures. For example, the fields of :c:type:`mps_ap_s` must correspond
with :c:type:`APStruct` internally. This is checked by ``mpsi.c`` in
:c:func:`mps_check()`.

:mps:tag:`type.pseudo` Some pseudo-opaque structures are defined. These only
exist so that code can be inlined using macros. The client code
shouldn't mess with them. The most important case of this is the scan
state (:c:type:`mps_ss_s`) which is accessed by the in-line scanning macros,
``MPS_SCAN_*`` and ``MPS_FIX*``.

:mps:tag:`type.enum` There are no enumeration types in the interface. Note
that enum specifiers (to declare integer constants) are fine as long
as no type is declared. See guide.impl.c.misc.enum.type.

:mps:tag:`type.fun` Whenever function types or derived function types (such
as pointer to function) are declared a prototype should be used and
the parameters to the function should not be named. This includes the
case where you are declaring the prototype for an interface function.

:mps:tag:`type.fun.example` So use::

    extern mps_res_t mps_alloc(mps_addr_t *, mps_pool_t, size_t, ...);

rather than::

    extern mps_res_t mps_alloc(mps_addr_t *addr_return, mps_pool_t pool , size_t size, ...);

and::

    typedef mps_addr_t (*mps_fmt_class_t)(mps_addr_t);

rather than::

    typedef mps_addr_t (*mps_fmt_class_t)(mps_addr_t object);

See guide.impl.c.misc.prototype.parameters.


Checking
--------

:mps:tag:`check.testt` Before any use of a parameter ``foo`` belonging to a
pointer type ``Foo``, it is checked using ``TESTT(Foo, foo)``. The
macro :c:func:`TESTT()` in impl.h.check performs simple thread-safe checking
of ``foo``, so it can be called outside of :c:func:`ArenaEnter()` and
:c:func:`ArenaLeave()`.

:mps:tag:`check.avert` With the arena lock held, ``foo`` is checked using
``AVERT(Foo, foo)``. This macro has different definitions depending on
how the MPS is compiled (see design.mps.config.def.var_). It may
expand to :c:func:`TESTT()`, or it may call the full checking function for
the type.

.. _design.mps.config.def.var: config.html#design.mps.config.def.var

:mps:tag:`check.types` We use definitions of types in both our external
interface and our internal code, and we want to make sure that they
are compatible. (The external interface changes less often and hides
more information.) This checking uses the following macros, originally
from `mail.richard.1996-08-07.09-49`_.

.. _mail.richard.1996-08-07.09-49: https://info.ravenbrook.com/project/mps/mail/1996/08/07/09-49/0.txt

.. c:macro:: COMPATLVALUE(lvalue1, lvalue2)

:mps:tag:`check.types.compat.lvalue` This macro checks the assignment
compatibility of two lvalues. It uses ``sizeof`` to ensure that the
assignments have no effect. ::

    #define COMPATLVALUE(lv1, lv2) \
      ((void)sizeof((lv1) = (lv2)), (void)sizeof((lv2) = (lv1)), TRUE)

.. c:macro:: COMPATTYPE(type1, type2)

:mps:tag:`check.types.compat.type` This macro checks that two types are
assignment-compatible and equal in size. The hack here is that it
generates an lvalue for each type by casting zero to a pointer to the
type. The use of ``sizeof`` avoids the undefined behaviour that
would otherwise result from dereferencing a null pointer. ::

    #define COMPATTYPE(t1, t2) \
      (sizeof(t1) == sizeof(t2) && \
       COMPATLVALUE(*((t1 *)0), *((t2 *)0)))

.. c:macro:: COMPATFIELDAPPROX(structure1, field1, structure2, field2)

:mps:tag:`check.types.compat.field.approx` This macro checks that the offset
and size of two fields in two structure types are the same. ::

    #define COMPATFIELDAPPROX(s1, f1, s2, f2) \
      (sizeof(((s1 *)0)->f1) == sizeof(((s2 *)0)->f2) && \
       offsetof(s1, f1) == offsetof(s2, f2))

.. c:macro:: COMPATFIELD(structure1, field1, structure2, field2)

:mps:tag:`check.types.compat.field` This macro checks the offset, size, and
assignment-compatibility of two fields in two structure types. ::

    #define COMPATFIELD(s1, f1, s2, f2) \
      (COMPATFIELDAPPROX(s1, f1, s2, f2) && \
       COMPATLVALUE(((s1 *)0)->f1, ((s2 *)0)->f2))


Binary compatibility issues
---------------------------

As in, "Enumeration types are not allowed" (see
`mail.richard.1995-09-08.09-28`_).

.. _mail.richard.1995-09-08.09-28: https://info.ravenbrook.com/project/mps/mail/1995/09/08/09-28/0.txt

:mps:tag:`compat` There are two main aspects to run-time compatibility:
binary interface and protocol.

:mps:tag:`compat.binary` The binary interface is all the information needed
to correctly use the library, and includes external symbol linkage,
calling conventions, type representation compatibility, structure
layouts, etc.

:mps:tag:`compat.binary.unneeded` Binary compatibility is not required by
the open source MPS: we expect (and indeed, recommend) that a client
program is compiled against the MPS sources. Nonetheless we try to
maintain binary compatibility in case the capability is required in
future.

:mps:tag:`compat.binary.dependencies` The binary interface is determined
completely by the header file and the target. The header file
specifies the external names and the types, and the target platform
specifies calling conventions and type representation. There is
therefore a many-to-one mapping between the header file version and
the binary interface.

:mps:tag:`compat.protocol` The protocol is how the library is actually used
by the client code -- whether this is called before that -- and
determines the semantic correctness of the client with respect to the
library.

:mps:tag:`compat.protocol.dependencies` The protocol is determined by the
implementation of the library.


Constraints
-----------

:mps:tag:`cons` The MPS C Interface constrains the MPS in order to provide
useful memory management services to a C or C++ program.

:mps:tag:`cons.addr` The interface constrains the MPS address type, Addr
(design.mps.type.addr_), to being the same as C's generic pointer type,
``void *``, so that the MPS can manage C objects in the natural way.

.. _design.mps.type.addr: type.html#design.mps.type.addr

:mps:tag:`pun.addr` We pun the type of :c:type:`mps_addr_t` (which is ``void *``)
into :c:type:`Addr` (an incomplete type, see design.mps.type.addr_). This
happens in the call to the scan state's fix function, for example.

:mps:tag:`cons.size` The interface constrains the MPS size type, :c:type:`Size`
(design.mps.type.size_), to being the same as C's size type,
``size_t``, so that the MPS can manage C objects in the natural way.

.. _design.mps.type.size: type.html#design.mps.type.size

:mps:tag:`pun.size` We pun the type of ``size_t`` in mps.h into :c:type:`Size` in
the MPM, as an argument to the format methods. We assume this works.

:mps:tag:`cons.word` The MPS assumes that :c:type:`Word` (design.mps.type.word_)
and :c:type:`Addr` (design.mps.type.addr_) are the same size, and the
interface constrains :c:type:`Word` to being the same size as C's generic
pointer type, ``void *``.

.. _design.mps.type.word: type.html#design.mps.type.word


Implementation
--------------

:mps:tag:`impl` The external interface consists of the following header
files:

:mps:tag:`impl.mps` ``mps.h`` is the main external interface, containing of
type and function declarations needed by all clients of the MPS.

:mps:tag:`impl.mpstd` ``mpstd.h`` is the MPS target detection header. It
decodes preprocessor symbols which are predefined by build
environments in order to determine the target platform (see
design.mps.config_), and then defines uniform symbols, such as
:c:macro:`MPS_ARCH_I3`, for use externally and internally by the MPS.
``mpstd.h`` is not included by any of the other external headers, as
it relies on exact set of preprocessor constants defined by compilers.

.. _design.mps.config: config.html

:mps:tag:`impl.mpsio` ``mpsio.h`` is the interface to the MPS I/O subsystem,
part of the plinth. See design.mps.io_.

.. _design.mps.io: io.html

:mps:tag:`impl.mpslib` ``mpslib.h`` is the interface to the MPS Library
Interface, part of the plinth. See design.mps.lib_.

.. _design.mps.lib: lib.html

:mps:tag:`impl.mpsa` Interfaces to arena classes are in files with names
starting ``mpsa``: for example, the interface to the Virtual Memory
arena class is in ``mpsavm.h``.

:mps:tag:`impl.mpsc` Interfaces to pool classes are in files with names
starting ``mpsc``: for example, the interface to the MVFF pool class
is in ``mpscmvff.h``.


Notes
-----

:mps:tag:`fmt.extend` ``mps_fmt_A_t`` is so called because new pool classes
might require new format methods, but these methods cannot be added to
the format structure without breaking binary compatibility. Therefore
these new pool classes would use new format structures named
``mps_fmt_B_t`` and so on.

:mps:tag:`thread-safety` Most calls through this interface lock the arena
and therefore make the MPM single-threaded. In order to do this they
must recover the arena from their parameters. Methods such as
:c:func:`FormatArena()` and :c:func:`ThreadArena()` must therefore be callable
when the arena is *not* locked. These methods are tagged with the tag
of this note.

:mps:tag:`lock-free` Certain functions inside the MPM are thread-safe and do
not need to be serialized by using locks. They are marked with the tag
of this note.

:mps:tag:`form` Almost all functions in this implementation simply cast
their arguments to the equivalent internal types, and cast results
back to the external type, where necessary. Only exceptions are noted
in comments.