A memory pool is a block of shared memory that is set aside for storing shared data structures. The pool needs to be created before the worker processes are forked off that are going to use the pool. The worker processes do not map the pool to some random address, but rather the processes inherit the pool from the common master process which ensures that all processes will see the pool at the same address.
In order to allow inheritance, the function
for starting workers needs to get an additional argument
~inherit_resources. The resource ID of the pool must be put
into this list - otherwise the worker does not get access to
It is not possible to enlarge the pool later (because of the
inheritance method for making the pool accessible). It is advised
to make the pool large enough for all possible data cases, and
to let the user configure this size.
val create_mempool :
?alloc_really:bool -> int -> Netmcore.res_id
Note that the process calling this function cannot use the pool,
but only worker processes that are forked later. It is possible
create_mempool before running
alloc_really: On some operating systems (namely Linux)
shared memory is not fully included into the memory bookkeeping
as long as nothing is written into it (so-called
overcommitment). This means that the memory is not reserved, and
when something is written for the first time, it might happen
that the system cannot grant the request. The consequence is a
bus error. By setting
alloc_really to true, all allocated
memory pages are immediately written to, and thus the problem is
avoided (or better, if memory is really tight, you get the bus
error now immediately, at least).
Note that memory pools have kernel persistence! They are not
automatically deleted when the process finishes. Call
to delete memory pools.
val alloc_mem :
Netmcore.res_id -> int -> Netsys_mem.memory
memoryobject. The size is rounded up to the next multiple of pages.
Blocks are actually allocated in units of pages.
Out_of_pool_memory if there is not enough contiguous
space in the pool.
val size_mem :
Netmcore.res_id -> Netsys_mem.memory -> int
val size_mem_at_addr :
Netmcore.res_id -> nativeint -> int
val free_mem :
Netmcore.res_id -> Netsys_mem.memory -> unit
val stats :
Netmcore.res_id -> int * int * int
(total, free, contiguous)where
totalis the total size of the pool
freeis the number of free bytes
contiguousis the size of the largest contiguous free block
val debug_info :
Netmcore.res_id -> string
val shm_name :
Netmcore.res_id -> string
val sem_container :
Netmcore.res_id -> Netsys_sem.container
val unlink_mempool :
Netmcore.res_id -> unit