Move to memory pool instead of new+delete calls for AVRC metadata producer and user functions (processAVRCTask and avrc_metadata_callback)
#define POOL_SIZE 15
#define BUFFER_SIZE 255
// The actual storage
static uint8_t metadataBufferPool[POOL_SIZE][BUFFER_SIZE];
// The two queues
QueueHandle_t dataQueue;
QueueHandle_t freeBlockQueue;
// Snippet for memory pool queues creation
void initMemoryPool() {
dataQueue = xQueueCreate(POOL_SIZE, sizeof(avrcMetadata));
freeBlockQueue = xQueueCreate(POOL_SIZE, sizeof(uint8_t*));
// Fill the "Free" queue with pointers to our static buffers
for (int i = 0; i < POOL_SIZE; i++) {
uint8_t* p = metadataBufferPool[i];
xQueueSend(freeBlockQueue, &p, portMAX_DELAY);
}
}
// Producer callback
void avrc_metadata_callback(uint8_t id, const uint8_t *text) {
uint8_t* availableBuffer = nullptr;
// Try to get a free buffer (non-blocking)
if (xQueueReceive(freeBlockQueue, &availableBuffer, 0) == pdTRUE) {
avrcMetadata msg;
msg.id = id;
msg.payload = availableBuffer;
memcpy(msg.payload, text, BUFFER_SIZE);
if (xQueueSend(dataQueue, &msg, 0) != pdTRUE) {
// If data queue is full, return buffer to free pool
xQueueSend(freeBlockQueue, &availableBuffer, 0);
}
} else {
ESP_LOGE("POOL", "No free buffers available for burst!");
}
}
// Consumer
static void processAVRCTask(void *pvParameters) {
avrcMetadata incMetadata;
while (true) {
// Block indefinitely until data arrives
if (xQueueReceive(dataQueue, &incMetadata, portMAX_DELAY) == pdTRUE) {
// Do your processing logic...
espod.updateAlbumName((char *)incMetadata.payload);
// Instead of delete[], return the buffer to the pool
xQueueSend(freeBlockQueue, &incMetadata.payload, 0);
}
}
}
Move to memory pool instead of new+delete calls for AVRC metadata producer and user functions (
processAVRCTaskandavrc_metadata_callback)Example :