[usb_host] Fix EventPool/LockFreeQueue sizing off-by-one (#14896)

This commit is contained in:
J. Nick Koston
2026-03-17 12:17:59 -10:00
committed by GitHub
parent 97382ed814
commit c19c75220b
2 changed files with 6 additions and 2 deletions

View File

@@ -144,7 +144,10 @@ class USBClient : public Component {
// Lock-free event queue and pool for USB task to main loop communication
// Must be public for access from static callbacks
LockFreeQueue<UsbEvent, USB_EVENT_QUEUE_SIZE> event_queue;
EventPool<UsbEvent, USB_EVENT_QUEUE_SIZE> event_pool;
// Pool sized to queue capacity (SIZE-1) because LockFreeQueue<T,N> is a ring
// buffer that holds N-1 elements. This guarantees allocate() returns nullptr
// before push() can fail, preventing a pool slot leak.
EventPool<UsbEvent, USB_EVENT_QUEUE_SIZE - 1> event_pool;
protected:
// Process USB events from the queue. Returns true if any work was done.

View File

@@ -193,7 +193,8 @@ static void client_event_cb(const usb_host_client_event_msg_t *event_msg, void *
return;
}
// Push to lock-free queue (always succeeds since pool size == queue size)
// Push always succeeds: pool is sized to queue capacity (SIZE-1), so if
// allocate() returned non-null, the queue cannot be full.
client->event_queue.push(event);
// Re-enable component loop to process the queued event