97#if defined(__cplusplus) && defined(_WIN32)
99using std::atomic_size_t;
101#include <stdatomic.h>
407 uint32_t client_id,
bool copy_data);
🗃️ Lock-Free Unified Memory Buffer Pool with Lazy Allocation
unsigned long long uint64_t
_Atomic size_t count
Number of packets currently in queue - atomic for lock-free access.
void packet_queue_shutdown(packet_queue_t *queue)
Signal queue shutdown (causes dequeue to return NULL)
size_t max_size
Maximum queue size (0 = unlimited)
size_t used_count
Number of nodes currently in use.
void packet_queue_clear(packet_queue_t *queue)
Clear all packets from queue.
_Atomic(packet_node_t *) head
Front of queue (dequeue from here) - atomic for lock-free access.
_Atomic uint64_t packets_dropped
Total packets dropped due to queue full (statistics) - atomic for lock-free access.
buffer_pool_t * buffer_pool
Optional memory pool for data buffers (NULL = use malloc/free)
size_t pool_size
Total number of nodes in pool.
packet_queue_t * packet_queue_create(size_t max_size)
Create a new packet queue.
packet_node_t * node_pool_get(node_pool_t *pool)
Get a free node from the pool.
_Atomic uint64_t packets_dequeued
Total packets dequeued (statistics) - atomic for lock-free access.
packet_queue_t * packet_queue_create_with_pools(size_t max_size, size_t node_pool_size, bool use_buffer_pool)
Create a packet queue with both node and buffer pools.
void packet_queue_get_stats(packet_queue_t *queue, uint64_t *enqueued, uint64_t *dequeued, uint64_t *dropped)
Get queue statistics.
bool owns_data
If true, free data when packet is freed.
bool packet_queue_is_empty(packet_queue_t *queue)
Check if queue is empty.
size_t data_len
Length of payload data in bytes.
void packet_queue_free_packet(queued_packet_t *packet)
Free a dequeued packet.
_Atomic size_t bytes_queued
Total bytes of data queued (for monitoring) - atomic for lock-free access.
packet_node_t * nodes
Pre-allocated array of all nodes.
int packet_queue_enqueue_packet(packet_queue_t *queue, const queued_packet_t *packet)
Enqueue a pre-built packet (for special cases like compressed frames)
packet_queue_t * packet_queue_create_with_pool(size_t max_size, size_t pool_size)
Create a packet queue with node pool.
int packet_queue_enqueue(packet_queue_t *queue, packet_type_t type, const void *data, size_t data_len, uint32_t client_id, bool copy_data)
Enqueue a packet into the queue.
_Atomic bool shutdown
Shutdown flag (true = dequeue returns NULL) - atomic for lock-free access.
packet_header_t header
Complete packet header (already in network byte order)
bool packet_queue_is_full(packet_queue_t *queue)
Check if queue is full.
queued_packet_t * packet_queue_try_dequeue(packet_queue_t *queue)
Try to dequeue a packet without blocking.
void node_pool_put(node_pool_t *pool, packet_node_t *node)
Return a node to the pool.
buffer_pool_t * buffer_pool
Pool that allocated the data (NULL if malloc'd)
mutex_t pool_mutex
Mutex protecting free list access.
void node_pool_destroy(node_pool_t *pool)
Destroy a node pool and free all memory.
_Atomic(packet_node_t *) next
Pointer to next node in linked list (NULL for tail) - atomic for lock-free operations.
void packet_queue_destroy(packet_queue_t *queue)
Destroy a packet queue and free all resources.
_Atomic uint64_t packets_enqueued
Total packets enqueued (statistics) - atomic for lock-free access.
struct node_pool node_pool_t
Memory pool for packet nodes to reduce malloc/free overhead.
queued_packet_t packet
The queued packet data.
bool packet_queue_validate_packet(const queued_packet_t *packet)
Validate packet integrity.
queued_packet_t * packet_queue_dequeue(packet_queue_t *queue)
Dequeue a packet from the queue (non-blocking)
size_t packet_queue_size(packet_queue_t *queue)
Get current number of packets in queue.
packet_node_t * free_list
Stack of free nodes (LIFO for cache locality)
void * data
Packet payload data (can be NULL for header-only packets)
node_pool_t * node_pool
Optional memory pool for nodes (NULL = use malloc/free)
node_pool_t * node_pool_create(size_t pool_size)
Create a memory pool for packet queue nodes.
packet_type_t
Network protocol packet type enumeration.
Packet protocol implementation with encryption and compression support.
Unified buffer pool with lock-free fast path.
Memory pool for packet nodes to reduce malloc/free overhead.
Node in the packet queue linked list.
Thread-safe packet queue for producer-consumer communication.
_Atomic(packet_node_t *) tail
Back of queue (enqueue here) - atomic for lock-free access.
Single packet ready to send (header already in network byte order)