MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
memoria_anularis — the ring

Data Structures

struct  mmgr_ring_span
 A byte region a pinned loculus retains out: the storage, its extent, and how far a reader got. More...
struct  mmgr_ring
 Storage a caller declares for one ring, whose contents belong to the implementation. More...
struct  AnularisCfg
 Arguments for every mmgr_anular_ entry, each reading only the members it needs. More...
struct  MemoriaAnularisNs
 Type of the anularis dispatch table. More...

Macros

#define MMGR_RING_WORDS
 Size of the ring storage a caller declares, counted in size_t units.
#define MMGR_RING_LOCULI_MAX
 Loculi a mask can address, which is one bit per loculus in a machine word.
#define MMGR_RING_LOCULI
 Loculi this build reserves, which a build may set before it includes this header.
#define MMGR_RING_POW2(count_)
 Asserts the loculi a build declares fit in one machine word.
#define MMGR_RING_WRAP(index_, capacity_)
 Wraps a ring offset back into a ring of capacity_ bytes.
#define MemoriaAnularis(name_, pool_)
 Declares a ring's storage and claims the pool under it.

Functions

EMBED_HOST_DEVICE embed_bool mmgr_anular_init (const AnularisCfg *args)
 Lays a fresh ring into args->ring, over the bytes at args->buf.
EMBED_HOST_DEVICE size_t mmgr_anular_available (const AnularisCfg *args)
 Returns the bytes ready for a read.
EMBED_HOST_DEVICE size_t mmgr_anular_vacant (const AnularisCfg *args)
 Returns the bytes still free to write.
EMBED_HOST_DEVICE embed_bool mmgr_anular_read_byte (const AnularisCfg *args)
 Takes one byte into args->dst and advances the tail past it.
EMBED_HOST_DEVICE size_t mmgr_anular_read (const AnularisCfg *args)
 Takes up to args->bytes into args->dst and advances the tail once at the end.
EMBED_HOST_DEVICE void mmgr_anular_peek (const AnularisCfg *args)
 Copies args->bytes from args->offset ahead of the tail into args->dst, and leaves the tail alone.
EMBED_HOST_DEVICE void mmgr_anular_consume (const AnularisCfg *args)
 Advances the tail past args->bytes.
EMBED_HOST_DEVICE embed_bool mmgr_anular_put (const AnularisCfg *args)
 Writes args->bytes of args->src into the ring, or refuses the whole span.
EMBED_HOST_DEVICE size_t mmgr_anular_seg_inflight (const AnularisCfg *args)
 Returns the segments filled and not yet released.
EMBED_HOST_DEVICE embed_bool mmgr_anular_seg_next (const AnularisCfg *args)
 Reports the index of the segment the producer fills next.
EMBED_HOST_DEVICE void mmgr_anular_seg_publish (const AnularisCfg *args)
 Makes the filled segment visible to the consumer.
EMBED_HOST_DEVICE embed_bool mmgr_anular_seg_front (const AnularisCfg *args)
 Reports the index of the segment the consumer takes next.
EMBED_HOST_DEVICE void mmgr_anular_seg_release (const AnularisCfg *args)
 Frees the front segment.
EMBED_HOST_DEVICE uint8_t * mmgr_anular_seg_at (const AnularisCfg *args)
 Returns the contiguous span of segment args->index.
EMBED_HOST_DEVICE embed_word mmgr_anular_loculus_ready (const AnularisCfg *args)
 Returns the loculi that are free and not pinned.
EMBED_HOST_DEVICE embed_iword mmgr_anular_loculus_next (const AnularisCfg *args)
 Returns the index of the lowest set bit of args->mask.
EMBED_HOST_DEVICE embed_bool mmgr_anular_loculus_hold (const AnularisCfg *args)
 Takes loculus args->index and records the args->bytes at args->src that it retains out.
EMBED_HOST_DEVICE const mmgr_ring_span * mmgr_anular_loculus_keepout (const AnularisCfg *args)
 Returns the region loculus args->index retains out.
EMBED_HOST_DEVICE void mmgr_anular_loculus_drop (const AnularisCfg *args)
 Gives loculus args->index back.
EMBED_HOST_DEVICE void mmgr_anular_loculus_mark (const AnularisCfg *args)
 Marks loculus args->index free.

Variables

static const MemoriaAnularisNs anularis
 Dispatch table instance named anularis, whose members are the mmgr_anular_ entries.

Detailed Description

Macro Definition Documentation

◆ MMGR_RING_WORDS

#define MMGR_RING_WORDS

Size of the ring storage a caller declares, counted in size_t units.

Note
The implementation asserts its state fits inside this. A change there fails a build and does not run past a caller's object.
The majority of the state is the keepout array, and so a raise of MMGR_RING_LOCULI grows it and may leave this short. The assertion below RingState in memoria_anularis.c is the single one that fails then.
A build sets this before it includes this header, the same way it sets the loculus count.

Definition at line 56 of file memoria_anularis.h.

◆ MMGR_RING_LOCULI_MAX

#define MMGR_RING_LOCULI_MAX

Loculi a mask can address, which is one bit per loculus in a machine word.

Warning
A build that declares more loculi than this has no bit left to name them. Wider masks, or a scan in their place, is what raises the ceiling. The assertion below fails until one of those happens.

Definition at line 66 of file memoria_anularis.h.

◆ MMGR_RING_LOCULI

#define MMGR_RING_LOCULI

Loculi this build reserves, which a build may set before it includes this header.

Note
The keepout spans are the majority of the ring's state. A build with no use for the loculus view sets this to 0 and can then lower MMGR_RING_WORDS. This alone shrinks the state. The storage a caller declares follows MMGR_RING_WORDS alone.
At 0, mmgr_anular_loculus_ready reports an empty mask, mmgr_anular_loculus_hold refuses every index, and mmgr_anular_loculus_keepout returns NULL. mmgr_anular_loculus_drop and mmgr_anular_loculus_mark leave both masks unchanged.

Definition at line 80 of file memoria_anularis.h.

◆ MMGR_RING_POW2

#define MMGR_RING_POW2 ( count_)

Asserts the loculi a build declares fit in one machine word.

Note
The free and pinned masks are each one embed_word, one bit per loculus. A count past MMGR_RING_LOCULI_MAX has no bit to sit in. ring_loculus_bit in memoria_anularis.c builds that bit with a shift, and this bound retains the shift inside the word.

Reports whether count_ is a power of two.

Parameters
[in]count_Count to test, either a byte capacity or a segment count.
Returns
Non-zero when count_ has at max one bit set.
Warning
Also reports non-zero for a count_ of 0. mmgr_anular_init tests for 0 on a line of its own.
count_ appears twice in the expansion. An argument with a side effect runs twice.

Definition at line 102 of file memoria_anularis.h.

◆ MMGR_RING_WRAP

#define MMGR_RING_WRAP ( index_,
capacity_ )

Wraps a ring offset back into a ring of capacity_ bytes.

Parameters
[in]index_Offset to wrap, which can lie past the end of the buffer.
[in]capacity_Ring size in bytes, a power of two.
Returns
index_ masked into the range 0 through capacity_ minus one.
Warning
Correct only because capacity_ is a power of two, which mmgr_anular_init enforces.

Definition at line 112 of file memoria_anularis.h.

◆ MemoriaAnularis

#define MemoriaAnularis ( name_,
pool_ )

Declares a ring's storage and claims the pool under it.

Parameters
[in]name_Name of the ring.
[in]pool_Pool the ring covers, as ParsMemoriaeInternae or ParsMemoriaeExternum declared it [BORROWS].
Note
The claim retains one pool from two sets of records. A pool with a cellblock on it and then a ring arranged over it carries two sets of records over the same bytes. Each writes with no regard to the other, and nothing at run time reports it.
A declaration, because MMGR_PARS_CLAIMED_ONCE emits an enumerator and an enumerator inside a function body is block scoped. Put at the mmgr_anular_init call it would shadow the cellblock's and never collide.
Emits the storage and nothing in it. mmgr_anular_init positions the state into those bytes and still has to run, since it can refuse the sizes it receives.
EMBED_PASS_MEMORY gives each pass of nvcc its own ring, as it gives each pass its own pool. A kernel reaches the ring in device memory, and host code reaches the one in host memory.
Warning
The pool appears here and again at mmgr_anular_init. Nothing holds those two to the same pool. A ring declared over one pool and initialized over another compiles.

Definition at line 148 of file memoria_anularis.h.

Function Documentation

◆ mmgr_anular_init()

EMBED_HOST_DEVICE embed_bool mmgr_anular_init ( const AnularisCfg * args)

Lays a fresh ring into args->ring, over the bytes at args->buf.

Parameters
[in,out]argsStorage, bytes, capacity and segment count [BORROWS].
Returns
EMBED_TRUE when the ring is ready, EMBED_FALSE when it refused a size.
Note
Refuses a capacity or a segment_count that is 0 or not a power of two, and a segment_count above capacity.
Given args->segment_bytes, the capacity may be any size that holds segment_count segments of it. The ring then has segments and loculi and no byte stream: the vacant bytes read 0, a put fails and a read takes nothing. The segments' counters wrap by segment_count, and a segment's place is its index times its size. So no size needs to be a power of two.
Marks every loculus free and none pinned, and clears the recorded keepouts.
Warning
args->ring and args->buf are both the caller's, and the ring retains args->buf [BORROWS].
Neither may be null, and nothing holds them there outside a MMGR_DEBUG_CHECKS build. A write goes through a null ring as soon as the sizes pass. A null buf goes in with no notice until the first move.
args->buf must carry the alignment the caller's own accesses need. This call does not align it.

References mmgr_anular_init().

Referenced by mmgr_anular_init().

◆ mmgr_anular_available()

EMBED_HOST_DEVICE size_t mmgr_anular_available ( const AnularisCfg * args)

Returns the bytes ready for a read.

Parameters
[in]argsRing to inspect [BORROWS].
Returns
Distance from the tail to the head, wrapped into the ring.
Warning
The answer is only a snapshot. A concurrent producer may add more before the caller acts on it.

References mmgr_anular_available().

Referenced by mmgr_anular_available().

◆ mmgr_anular_vacant()

EMBED_HOST_DEVICE size_t mmgr_anular_vacant ( const AnularisCfg * args)

Returns the bytes still free to write.

Parameters
[in]argsRing to inspect [BORROWS].
Returns
The capacity mmgr_anular_init received, less one, less the readable bytes.
Note
One byte always stays back, which retains a full ring apart from an empty one.
Warning
The answer is only a snapshot. A concurrent consumer may free more before the caller acts on it.

References mmgr_anular_vacant().

Referenced by mmgr_anular_vacant().

◆ mmgr_anular_read_byte()

EMBED_HOST_DEVICE embed_bool mmgr_anular_read_byte ( const AnularisCfg * args)

Takes one byte into args->dst and advances the tail past it.

Parameters
[in,out]argsRing and the destination byte [BORROWS].
Returns
EMBED_TRUE when a byte came out, EMBED_FALSE when the ring was empty.
Note
Writes through args->dst only when it returns EMBED_TRUE.
One byte per call, and so the cost is the call. A caller that draws a run of bytes wants mmgr_anular_read, which moves them a word at a time under one tail store.
Warning
args->dst must be writable for one byte.

References mmgr_anular_read_byte().

Referenced by mmgr_anular_read_byte().

◆ mmgr_anular_read()

EMBED_HOST_DEVICE size_t mmgr_anular_read ( const AnularisCfg * args)

Takes up to args->bytes into args->dst and advances the tail once at the end.

Parameters
[in,out]argsRing, destination and the max to take [BORROWS].
Returns
Bytes actually taken, which is 0 when the ring was empty.
Note
Moves the bytes in at max two runs, and so the wrap costs one extra move and not one per byte.
Reads the tail once and publishes it once and not per byte.
Warning
args->dst must be writable for args->bytes.

References mmgr_anular_read().

Referenced by mmgr_anular_read().

◆ mmgr_anular_peek()

EMBED_HOST_DEVICE void mmgr_anular_peek ( const AnularisCfg * args)

Copies args->bytes from args->offset ahead of the tail into args->dst, and leaves the tail alone.

Parameters
[in,out]argsRing, destination, byte count and start offset [BORROWS].
Note
Moves the bytes in at max two runs, the same way mmgr_anular_read does.
Warning
Copies args->bytes whether or not that many arrived. Read mmgr_anular_available first.
An args->bytes above the ring's capacity stops there. Two runs cover one lap at max.
args->dst must be writable for args->bytes.

References mmgr_anular_peek().

Referenced by mmgr_anular_peek().

◆ mmgr_anular_consume()

EMBED_HOST_DEVICE void mmgr_anular_consume ( const AnularisCfg * args)

Advances the tail past args->bytes.

Parameters
[in,out]argsRing and the byte count to drop [BORROWS].
Warning
Advances whether or not that many arrived. Read mmgr_anular_available first.

References mmgr_anular_consume().

Referenced by mmgr_anular_consume().

◆ mmgr_anular_put()

EMBED_HOST_DEVICE embed_bool mmgr_anular_put ( const AnularisCfg * args)

Writes args->bytes of args->src into the ring, or refuses the whole span.

Parameters
[in,out]argsRing, the bytes to write and their count [BORROWS].
Returns
EMBED_TRUE when the span went in, EMBED_FALSE when it would not fit.
Note
Checks the whole span against the vacant bytes first. A partial write never happens.
Advances a local head across the wrap and publishes it once, and so no half span is ever visible.
Warning
args->src must be readable for args->bytes.

References mmgr_anular_put().

Referenced by mmgr_anular_put().

◆ mmgr_anular_seg_inflight()

EMBED_HOST_DEVICE size_t mmgr_anular_seg_inflight ( const AnularisCfg * args)

Returns the segments filled and not yet released.

Parameters
[in]argsRing to inspect [BORROWS].
Returns
The distance between the claim and release counters.
Warning
The reads of the two counters come one after the other, and so the answer is only a snapshot. A publish or a release may land between the reads, or after both.

References mmgr_anular_seg_inflight().

Referenced by mmgr_anular_seg_inflight().

◆ mmgr_anular_seg_next()

EMBED_HOST_DEVICE embed_bool mmgr_anular_seg_next ( const AnularisCfg * args)

Reports the index of the segment the producer fills next.

Parameters
[in,out]argsRing, and where to write the index [BORROWS].
Returns
EMBED_TRUE with the index in args->out_index, EMBED_FALSE when every segment is in flight.
Note
Writes through args->out_index only when it returns EMBED_TRUE.
The publish is separate. A half-filled segment is never visible to the consumer.
A EMBED_FALSE can go stale the moment the consumer releases a segment. A EMBED_TRUE cannot, since releases only make room.
Warning
args->out_index must be writable.

References mmgr_anular_seg_next().

Referenced by mmgr_anular_seg_next().

◆ mmgr_anular_seg_publish()

EMBED_HOST_DEVICE void mmgr_anular_seg_publish ( const AnularisCfg * args)

Makes the filled segment visible to the consumer.

Parameters
[in,out]argsRing to advance [BORROWS].
Note
Only the producer calls this. The read and the write of the counter are two steps, which needs no atomicity between them only because one side owns it.
Warning
Advances whether or not a segment filled. A publish with no mmgr_anular_seg_next to match puts more in flight than the ring holds, and that call then refuses until releases catch up.

References mmgr_anular_seg_publish().

Referenced by mmgr_anular_seg_publish().

◆ mmgr_anular_seg_front()

EMBED_HOST_DEVICE embed_bool mmgr_anular_seg_front ( const AnularisCfg * args)

Reports the index of the segment the consumer takes next.

Parameters
[in,out]argsRing, and where to write the index [BORROWS].
Returns
EMBED_TRUE with the index in args->out_index, EMBED_FALSE when none is in flight.
Note
Writes through args->out_index only when it returns EMBED_TRUE.
A EMBED_FALSE can go stale the moment the producer publishes a segment. A EMBED_TRUE cannot, since publishes only add.
Warning
args->out_index must be writable.

References mmgr_anular_seg_front().

Referenced by mmgr_anular_seg_front().

◆ mmgr_anular_seg_release()

EMBED_HOST_DEVICE void mmgr_anular_seg_release ( const AnularisCfg * args)

Frees the front segment.

Parameters
[in,out]argsRing to advance [BORROWS].
Note
Segments release in the order of their claims.
Only the consumer calls this. The read and the write of the counter are two steps, which needs no atomicity between them only because one side owns it.
Warning
Advances whether or not a segment was in flight. A release past the published count wraps the in-flight count and leaves mmgr_anular_seg_front with a segment on offer that never filled.

References mmgr_anular_seg_release().

Referenced by mmgr_anular_seg_release().

◆ mmgr_anular_seg_at()

EMBED_HOST_DEVICE uint8_t * mmgr_anular_seg_at ( const AnularisCfg * args)

Returns the contiguous span of segment args->index.

Parameters
[in]argsRing and the segment index [BORROWS].
Returns
Its first byte inside the ring buffer [BORROWS].
Note
The span runs for the segment size mmgr_anular_init received, or else for the capacity divided by the segment_count it received.
Warning
Nothing checks args->index against the segment count, and no assertion covers it. An index at or past that count returns a pointer outside the ring's bytes.

References mmgr_anular_seg_at().

Referenced by mmgr_anular_seg_at().

◆ mmgr_anular_loculus_ready()

EMBED_HOST_DEVICE embed_word mmgr_anular_loculus_ready ( const AnularisCfg * args)

Returns the loculi that are free and not pinned.

Parameters
[in]argsRing to inspect [BORROWS].
Returns
The free mask with the pinned ones cleared, bounded to MMGR_RING_LOCULI.
Note
A loculus is takeable only when it is free and not pinned, and that leaves reuse safe.
Warning
The answer is only a snapshot. The reads of the two masks come one after the other, and a hold or a drop may land before the caller acts on it. mmgr_anular_loculus_hold settles that race, and refuses a loculus another caller took first.

References mmgr_anular_loculus_ready().

Referenced by mmgr_anular_loculus_ready().

◆ mmgr_anular_loculus_next()

EMBED_HOST_DEVICE embed_iword mmgr_anular_loculus_next ( const AnularisCfg * args)

Returns the index of the lowest set bit of args->mask.

Parameters
[in]argsThe mask to pick from [BORROWS].
Returns
The index, or -1 when args->mask is empty.
Note
Counts and not scans, and branches on nothing but the empty mask.
This reads only args->mask. This is the single one entry that never reaches the ring.
Warning
The mask passes as given. A bit at or above MMGR_RING_LOCULI comes back as its index, and that index names no loculus, and so mmgr_anular_loculus_hold refuses it.

References mmgr_anular_loculus_next().

Referenced by mmgr_anular_loculus_next().

◆ mmgr_anular_loculus_hold()

EMBED_HOST_DEVICE embed_bool mmgr_anular_loculus_hold ( const AnularisCfg * args)

Takes loculus args->index and records the args->bytes at args->src that it retains out.

Parameters
[in,out]argsRing, the loculus, and the region to record [BORROWS].
Returns
EMBED_TRUE when this caller took it, EMBED_FALSE when args->index names none or another caller already holds it.
Note
The recorded region stays valid until mmgr_anular_loculus_drop. A reader walks it in place.
Warning
An out-of-range args->index names nothing, and so it reads as pinned and is never passed out.
The ring retains args->src and mmgr_anular_loculus_keepout passes it back, and so it must outlive the hold [BORROWS].

References mmgr_anular_loculus_hold().

Referenced by mmgr_anular_loculus_hold().

◆ mmgr_anular_loculus_keepout()

EMBED_HOST_DEVICE const mmgr_ring_span * mmgr_anular_loculus_keepout ( const AnularisCfg * args)

Returns the region loculus args->index retains out.

Parameters
[in]argsRing and the loculus [BORROWS].
Returns
The recorded span, or NULL when args->index is out of range [BORROWS].
Note
Passed back const. A reader walks it and leaves the ring's own record in place.
Warning
The const covers the span and not the bytes it names. buf is reachable as a writable pointer, though the region reached mmgr_anular_loculus_hold as const.
The record comes back for any loculus in range, pinned or not. A drop leaves it in place, and so afterwards it still names the last region that loculus recorded [BORROWS].

References mmgr_anular_loculus_keepout().

Referenced by mmgr_anular_loculus_keepout().

◆ mmgr_anular_loculus_drop()

EMBED_HOST_DEVICE void mmgr_anular_loculus_drop ( const AnularisCfg * args)

Gives loculus args->index back.

Parameters
[in,out]argsRing and the loculus [BORROWS].
Note
Leaves the recorded span and the bytes alone. A restream can run again.
An out-of-range args->index names no bit and clears nothing, and a drop of a loculus with no pin does nothing.

References mmgr_anular_loculus_drop().

Referenced by mmgr_anular_loculus_drop().

◆ mmgr_anular_loculus_mark()

EMBED_HOST_DEVICE void mmgr_anular_loculus_mark ( const AnularisCfg * args)

Marks loculus args->index free.

Parameters
[in,out]argsRing and the loculus [BORROWS].
Note
Sets the free bit. The pinned bit is mmgr_anular_loculus_drop's to clear, and a loculus is takeable only when it is free and not pinned.
mmgr_anular_init sets the free bit for every loculus and no entry clears it, and so on a ring that call positioned down this changes nothing.
An out-of-range args->index names no bit and sets nothing.

References mmgr_anular_loculus_mark().

Referenced by mmgr_anular_loculus_mark().

Variable Documentation

◆ anularis

const MemoriaAnularisNs anularis
static

Dispatch table instance named anularis, whose members are the mmgr_anular_ entries.

Definition at line 455 of file memoria_anularis.h.