1. Revision History
This paper is the initial revision.
2. Introduction
P0260 proposes
as communication mechanism for
concurrent systems.
Such queues are often used im small embedded systems to communicate
data from one task to another. P0260 proposes a constructor for
that takes
the maximum number of elements that the queue can hold and optionally
an allocator. The storage for the queue is allocated at construction time.
But many small embedded systems prefer to layout the complete memory usage at build time without support for dynamically allocated memory. This cannot be achieved even by providing a special allocator, as the amount of required memory is unknown. And even if it could be done by a special allocator, the programmatical overhead is not small.
To make statically allocated memory for
possible,
this paper proposes the
addition of a constructor to
that takes the number
of elements of the queue and a pointer to memory to be used for the storage.
3. Design
The additional constructor we propose here is pretty simple:
bounded_queue ( size_t max_elems , void * storage );
The requirement is that
points to enough memory for
. However, how much memory is required for holding
in a
is generally not known.
So we propose another addition to
:
static consteval size_t required_size ( size_t max_elems );
This function returns at compile time an upper bound to the size of
the memory required to hold
elements.
And we also need to know the required alignment:
static consteval size_t required_alignment ();
With this, a program could look like this:
typedef std :: bounded_queue < uint32_t > MyQueueT ; alignas ( MyQueueT :: required_alignment ) std :: array < std :: byte , MyQueueT :: required_size ( 8 ) > myQStorage ; MyQueueT myQ { 8 , myQStorage . data ()}; int main () { // start tasks and use myQ }