|
In order to use the private memory allocator, the pointer to the pre-allocated memory block, which will act as a private memory pool, should be passed to the agent's initialization function.
The pre-allocated can be placed anywhere, not necessarily on the physical heap - in particular, it might have a static storage duration, as in the following example:
#include <agent.h> using namespace yami::core; // ... const std::size_t one_megabyte = 1024 * 1024; char private_memory[one_megabyte]; { agent my_agent; result res = my_agent.init(..., &private_memory[0], one_megabyte); if (res == ok) { // ... } }
Above, the private memory buffer of 1 MB is passed to the init()
function of the agent object.
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:
#include <parameters.h> using namespace yami::core; // ... { const size_t one_kilobyte = 1024; char private_memory[one_kilobyte]; parameters content(&private_memory[0], one_kilobyte); // ... }
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.