Inspirel banner

Programming Distributed Systems with YAMI4

11.2.3 Ada

In order to use the private memory allocator, the address of the pre-allocated memory block, which will act as a private memory pool, should be passed to the agent's constructor function.

The pre-allocated can be placed anywhere, not necessarily on the physical heap - in particular, it might be allocated at the library level:

with YAMI.Core.Agents;

package body My_System is

   type Byte is mod 256;
   for Byte'Size use 8;

   type Byte_Buffer is array (Positive range <>) of Byte;

   One_Megabyte : constant := 1024 * 1024;
   Private_Memory : Byte_Buffer (1 .. One_Megabyte);

   My_Agent : YAMI.Core.Agents.Agent :=
      (..., Private_Memory'Address, Private_Memory'Size / 8);

   --  ...
end My_System;

Above, the private memory buffer of 1 MB is passed to the Make_Agent constructor function.

Similar considerations apply to the parameters object, but with very important note: each parameters object has its own memory management policy, which might be different from that of the agent that is used to send or receive messages with that parameters object.

The reason for having a separate memory policy for parameters objects is that each such object can be used with many different agents and there is no provision for having uniform policy for all agents that are used in the program. This also means that a typical program that needs to use isolated memory partitions, will use at least two buffers: one for the agent and one for the parameters object that is used to send messages (unless, of course, the messages are sent without parameters, which might be a reasonable approach for simple event signaling).

The parameters object can be created with its private memory partition as shown in the following example:

with YAMI.Parameters;

--  with Byte_Buffer as above
   One_Kilobyte : constant := 1024;
   Private_Memory : Byte_Buffer (1 .. One_Kilobyte);

   Content : YAMI.Parameters.Parameters_Collection :=
      (Private_Memory'Address, Private_Memory'Size / 8);

   --  ...

Above, the parameters object is created with its own isolated memory partition and for the sake of example this memory partition is created on the stack. It should be noted that a memory partition for the parameters object is not needed after the parameters object itself is destroyed.