Skip to content

Commit 6f55218

Browse files
committed
feat(esp_event): Allow an event carry more data without malloc
This is both a feature and an optimization. Feature: Adjustable size of the internal storage in esp_event queue, currently used by ISR posting, as they wont be able to make a malloc. Optimization: When non-isr is posting an event, use the inernal storage in the struct instead of always allocating a new heap for the data. Most events in esp-idf only contains a few bytes event information, and we have that allocation payed for anyway. This solved in a big part our memory fragmentation issue, as events happens freqvently and used to create small memory allocations for just 4 bytes, and then in the event handler we usually allocated a bigger chunk of heap for our feature. When returning from the event handler, the 4 byte allocation was freed, leaveing a hole in the heap.
1 parent ff97953 commit 6f55218

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

components/esp_event/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ menu "Event Loop Library"
1414
help
1515
Enable posting events from interrupt handlers.
1616

17+
config ESP_EVENT_POST_FROM_ISR_SIZE
18+
int "Max size of data from events"
19+
default 4
20+
depends on ESP_EVENT_POST_FROM_ISR
21+
help
22+
Enable posting events from interrupt handlers.
23+
24+
1725
config ESP_EVENT_POST_FROM_IRAM_ISR
1826
bool "Support posting events from ISRs placed in IRAM"
1927
default y

components/esp_event/esp_event.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,22 @@ esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop, esp_event_base_t
924924
memset((void*)(&post), 0, sizeof(post));
925925

926926
if (event_data != NULL && event_data_size != 0) {
927+
#if CONFIG_ESP_EVENT_POST_FROM_ISR
928+
929+
if (event_data_size > sizeof(post.data.val)) {
930+
post.data.ptr = calloc(1, event_data_size);
931+
932+
if (post.data.ptr == NULL) {
933+
return ESP_ERR_NO_MEM;
934+
}
935+
post.data_allocated = true;
936+
memcpy(post.data.ptr, event_data, event_data_size);
937+
} else {
938+
memcpy((void*)(&(post.data.val)), event_data, event_data_size);
939+
}
940+
941+
post.data_set = true;
942+
#else
927943
// Make persistent copy of event data on heap.
928944
void* event_data_copy = calloc(1, event_data_size);
929945

@@ -933,9 +949,6 @@ esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop, esp_event_base_t
933949

934950
memcpy(event_data_copy, event_data, event_data_size);
935951
post.data.ptr = event_data_copy;
936-
#if CONFIG_ESP_EVENT_POST_FROM_ISR
937-
post.data_allocated = true;
938-
post.data_set = true;
939952
#endif
940953
}
941954
post.base = event_base;

components/esp_event/private_include/esp_event_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ typedef struct esp_event_remove_handler_context_t {
9292
} esp_event_remove_handler_context_t;
9393

9494
typedef union esp_event_post_data {
95-
uint32_t val;
95+
uint8_t val[CONFIG_ESP_EVENT_POST_FROM_ISR_SIZE];
9696
void *ptr;
9797
} esp_event_post_data_t;
9898

0 commit comments

Comments
 (0)