Skip to content

EntropyEngine::Networking::SharedMemory

Name
structEntropyEngine::Networking::SharedMemory::SharedMemoryControlBlock
Control block structure placed at the start of each shared memory region.
structEntropyEngine::Networking::SharedMemory::PlatformOps
Platform-specific shared memory operations.
structEntropyEngine::Networking::SharedMemory::DiscoveryControlBlock
Control block for the discovery/handshake region.
Name
using intNativeHandle
Name
size_tringBufferWriteAvailable(uint64_t writePos, uint64_t readPos, size_t bufferSize)
Calculate available space in a ring buffer for writing.
size_tringBufferReadAvailable(uint64_t writePos, uint64_t readPos, size_t bufferSize)
Calculate available data in a ring buffer for reading.
uint64_tringBufferAdvance(uint64_t pos, size_t amount, size_t bufferSize)
Advance a ring buffer position.
uint32_tgetCurrentProcessId()
Get current process ID (cross-platform).
uint64_tgenerateSecurityToken()
Generate a cryptographically random 64-bit token.
std::stringgenerateRandomHex(size_t length =16)
Generate a random hex string for region names.
Name
uint32_tSHM_VERSION
Current protocol version.
uint32_tSHM_MAGIC
Magic number for control block validation (“ENTR” in little-endian).
size_tMAX_REGION_NAME_LENGTH
Maximum region name length.
NativeHandleINVALID_HANDLE_VALUE_SHM
intDEFAULT_WAIT_TIMEOUT_MS
Timeout for wake/wait operations (ms).
size_tDEFAULT_REGION_SIZE
Default shared memory region size (4 MiB).
size_tCONTROL_BLOCK_SIZE
Control block size (256 bytes, cache-aligned).
using EntropyEngine::Networking::SharedMemory::NativeHandle = int;
inline size_t ringBufferWriteAvailable(
uint64_t writePos,
uint64_t readPos,
size_t bufferSize
)

Calculate available space in a ring buffer for writing.

Parameters:

  • writePos Current write position (monotonic counter)
  • readPos Current read position (monotonic counter)
  • bufferSize Total buffer size

Return: Number of bytes available for writing

Uses monotonic 64-bit counters that wrap via natural overflow. The actual ring buffer position is computed as (counter % bufferSize) when accessing memory. This eliminates wrap-around edge case logic in availability calculations.

inline size_t ringBufferReadAvailable(
uint64_t writePos,
uint64_t readPos,
size_t bufferSize
)

Calculate available data in a ring buffer for reading.

Parameters:

  • writePos Current write position (monotonic counter)
  • readPos Current read position (monotonic counter)
  • bufferSize Total buffer size (unused, kept for API consistency)

Return: Number of bytes available for reading

Uses monotonic 64-bit counters. The difference between write and read positions gives the number of unread bytes directly.

inline uint64_t ringBufferAdvance(
uint64_t pos,
size_t amount,
size_t bufferSize
)

Advance a ring buffer position.

Parameters:

  • pos Current position (monotonic counter)
  • amount Amount to advance
  • bufferSize Total buffer size (unused, kept for API consistency)

Return: New position (monotonic counter)

Simply adds the amount to the monotonic counter. Actual memory access should use (pos % bufferSize) to get the ring buffer index.

uint32_t getCurrentProcessId()

Get current process ID (cross-platform).

Return: Process ID as uint32_t

inline uint64_t generateSecurityToken()

Generate a cryptographically random 64-bit token.

Return: Random token for connection validation

inline std::string generateRandomHex(
size_t length =16
)

Generate a random hex string for region names.

Parameters:

  • length Number of hex characters (default 16 = 64 bits)

Return: Random hex string

static uint32_t SHM_VERSION = 1;

Current protocol version.

static uint32_t SHM_MAGIC = 0x52544E45;

Magic number for control block validation (“ENTR” in little-endian).

static size_t MAX_REGION_NAME_LENGTH = 256;

Maximum region name length.

static NativeHandle INVALID_HANDLE_VALUE_SHM = -1;
static int DEFAULT_WAIT_TIMEOUT_MS = 100;

Timeout for wake/wait operations (ms).

static size_t DEFAULT_REGION_SIZE = 4ull * 1024ull * 1024ull;

Default shared memory region size (4 MiB).

static size_t CONTROL_BLOCK_SIZE = 256;

Control block size (256 bytes, cache-aligned).


Updated on 2026-01-26 at 17:14:35 -0500