From 55bf4af3bd3416e746a5e629604ec775b2142969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Tue, 9 Nov 2021 14:59:00 +0100 Subject: [PATCH] use pointer for events but keep single allocation --- esp_mqtt.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/esp_mqtt.c b/esp_mqtt.c index 6eac193..d6d191a 100644 --- a/esp_mqtt.c +++ b/esp_mqtt.c @@ -97,7 +97,7 @@ void esp_mqtt_init(esp_mqtt_status_callback_t scb, esp_mqtt_message_callback_t m esp_mqtt_select_mutex = xSemaphoreCreateMutex(); // create queue - esp_mqtt_event_queue = xQueueCreate(CONFIG_ESP_MQTT_EVENT_QUEUE_SIZE, sizeof(esp_mqtt_event_t)); + esp_mqtt_event_queue = xQueueCreate(CONFIG_ESP_MQTT_EVENT_QUEUE_SIZE, sizeof(esp_mqtt_event_t *)); } #if defined(CONFIG_ESP_MQTT_TLS_ENABLE) @@ -136,44 +136,44 @@ bool esp_mqtt_tls(bool enable, bool verify, const uint8_t *ca_buf, size_t ca_len #endif static void esp_mqtt_message_handler(lwmqtt_client_t *client, void *ref, lwmqtt_string_t topic, lwmqtt_message_t msg) { - // create message - esp_mqtt_event_t evt = {0}; - // allocate buffer - evt.buffer = malloc((size_t)topic.len + 1 + msg.payload_len + 1); + void *buffer = malloc(sizeof(esp_mqtt_event_t) + (size_t)topic.len + 1 + msg.payload_len + 1); + + // prepare message + esp_mqtt_event_t *evt = buffer; // copy topic with additional null termination - evt.topic.len = topic.len; - evt.topic.data = evt.buffer; - memcpy(evt.topic.data, topic.data, (size_t)topic.len); - evt.topic.data[topic.len] = 0; + evt->topic.len = topic.len; + evt->topic.data = buffer + sizeof(esp_mqtt_event_t); + memcpy(evt->topic.data, topic.data, (size_t)topic.len); + evt->topic.data[topic.len] = 0; // copy message with additional null termination - evt.message.retained = msg.retained; - evt.message.qos = msg.qos; - evt.message.payload_len = msg.payload_len; - evt.message.payload = evt.buffer + topic.len + 1; - memcpy(evt.message.payload, msg.payload, (size_t)msg.payload_len); - evt.message.payload[msg.payload_len] = 0; + evt->message.retained = msg.retained; + evt->message.qos = msg.qos; + evt->message.payload_len = msg.payload_len; + evt->message.payload = buffer + sizeof(esp_mqtt_event_t) + (size_t)topic.len + 1; + memcpy(evt->message.payload, msg.payload, (size_t)msg.payload_len); + evt->message.payload[msg.payload_len] = 0; // queue event if (xQueueSend(esp_mqtt_event_queue, &evt, 0) != pdTRUE) { ESP_LOGE(ESP_MQTT_LOG_TAG, "xQueueSend: queue is full, dropping message"); - free(evt.buffer); + free(evt); } } static void esp_mqtt_dispatch_events() { // receive next event - esp_mqtt_event_t evt = {0}; + esp_mqtt_event_t *evt = 0; while (xQueueReceive(esp_mqtt_event_queue, &evt, 0) == pdTRUE) { // call callback if existing if (esp_mqtt_message_callback) { - esp_mqtt_message_callback(evt.topic.data, evt.message.payload, evt.message.payload_len); + esp_mqtt_message_callback(evt->topic.data, evt->message.payload, evt->message.payload_len); } - // free buffer - free(evt.buffer); + // free event + free(evt); } }