Skip to content

Commit 88162d1

Browse files
committed
feat(mdns): Support user defined allocators
Defines mem-alloc function optionally weak, so users can override them and implement their own allocation, or a static/bss memory for the mdns task stack.
1 parent e65f22a commit 88162d1

File tree

7 files changed

+109
-62
lines changed

7 files changed

+109
-62
lines changed

components/mdns/Kconfig

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,47 @@ menu "mDNS"
6161
default 0x0 if MDNS_TASK_AFFINITY_CPU0
6262
default 0x1 if MDNS_TASK_AFFINITY_CPU1
6363

64-
choice MDNS_TASK_MEMORY_ALLOC_FROM
65-
prompt "Select mDNS task create on which type of memory"
66-
default MDNS_TASK_CREATE_FROM_INTERNAL
67-
config MDNS_TASK_CREATE_FROM_SPIRAM
68-
bool "mDNS task creates on the SPIRAM"
69-
depends on (SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC)
70-
config MDNS_TASK_CREATE_FROM_INTERNAL
71-
bool "mDNS task creates on the internal RAM"
72-
endchoice
64+
menu "MDNS Memory Configuration"
7365

74-
choice MDNS_MEMORY_ALLOC_FROM
75-
prompt "Select mDNS memory allocation type"
76-
default MDNS_MEMORY_ALLOC_INTERNAL
66+
choice MDNS_TASK_MEMORY_ALLOC_FROM
67+
prompt "Select mDNS task create on which type of memory"
68+
default MDNS_TASK_CREATE_FROM_INTERNAL
69+
config MDNS_TASK_CREATE_FROM_SPIRAM
70+
bool "mDNS task creates on the SPIRAM (READ HELP)"
71+
depends on (SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC)
72+
help
73+
mDNS task creates on the SPIRAM.
74+
This option requires FreeRTOS component to allow creating
75+
tasks on the external memory.
76+
Please read the documentation about FREERTOS_TASK_CREATE_ALLOW_EXT_MEM
7777

78-
config MDNS_MEMORY_ALLOC_SPIRAM
79-
bool "Allocate mDNS memory from SPIRAM"
80-
depends on (SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC)
78+
config MDNS_TASK_CREATE_FROM_INTERNAL
79+
bool "mDNS task creates on the internal RAM"
8180

82-
config MDNS_MEMORY_ALLOC_INTERNAL
83-
bool "Allocate mDNS memory from internal RAM"
84-
endchoice
81+
endchoice
82+
83+
choice MDNS_MEMORY_ALLOC_FROM
84+
prompt "Select mDNS memory allocation type"
85+
default MDNS_MEMORY_ALLOC_INTERNAL
86+
87+
config MDNS_MEMORY_ALLOC_SPIRAM
88+
bool "Allocate mDNS memory from SPIRAM"
89+
depends on (SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC)
90+
91+
config MDNS_MEMORY_ALLOC_INTERNAL
92+
bool "Allocate mDNS memory from internal RAM"
93+
94+
endchoice
95+
96+
config MDNS_MEMORY_CUSTOM_IMPL
97+
bool "Implement custom memory functions"
98+
default n
99+
help
100+
Enable to implement custom memory functions for mDNS library.
101+
This option is useful when the application wants to use custom
102+
memory allocation functions for mDNS library.
103+
104+
endmenu # MDNS Memory Configuration
85105

86106
config MDNS_SERVICE_ADD_TIMEOUT_MS
87107
int "mDNS adding service timeout (ms)"

components/mdns/mdns.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5466,12 +5466,10 @@ static esp_err_t _mdns_stop_timer(void)
54665466

54675467
static esp_err_t _mdns_task_create_with_caps(void)
54685468
{
5469-
ESP_LOGI(TAG, "mDNS task will be created from %s", MDNS_TASK_MEMORY_LOG);
54705469
esp_err_t ret = ESP_OK;
54715470
static StaticTask_t mdns_task_buffer;
54725471

5473-
// Allocate memory for the mDNS task's stack using the MDNS_TASK_MEMORY_CAPS
5474-
_mdns_stack_buffer = heap_caps_malloc(MDNS_SERVICE_STACK_DEPTH, MDNS_TASK_MEMORY_CAPS);
5472+
_mdns_stack_buffer = mdns_mem_task_malloc(MDNS_SERVICE_STACK_DEPTH);
54755473
ESP_GOTO_ON_FALSE(_mdns_stack_buffer != NULL, ESP_FAIL, err, TAG, "failed to allocate memory for the mDNS task's stack");
54765474

54775475
_mdns_service_task_handle = xTaskCreateStaticPinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY, _mdns_stack_buffer, &mdns_task_buffer, MDNS_TASK_AFFINITY);
@@ -5480,7 +5478,7 @@ static esp_err_t _mdns_task_create_with_caps(void)
54805478
return ret;
54815479

54825480
err:
5483-
heap_caps_free(_mdns_stack_buffer);
5481+
mdns_mem_task_free(_mdns_stack_buffer);
54845482
return ret;
54855483
}
54865484

@@ -5769,7 +5767,7 @@ void mdns_free(void)
57695767
free_delegated_hostnames();
57705768
_mdns_service_task_stop();
57715769
// at this point, the service task is deleted, so we can destroy the stack size
5772-
heap_caps_free(_mdns_stack_buffer);
5770+
mdns_mem_task_free(_mdns_stack_buffer);
57735771
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
57745772
for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
57755773
mdns_pcb_deinit_local(i, j);

components/mdns/mdns_mem_caps.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,60 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <string.h>
7+
#include "sdkconfig.h"
78
#include "mdns_private.h"
89
#include "mdns_mem_caps.h"
910
#include "esp_heap_caps.h"
11+
#include "esp_log.h"
1012

13+
#if CONFIG_MDNS_MEMORY_CUSTOM_IMPL
14+
#define ALLOW_WEAK __attribute__((weak))
15+
#else
16+
#define ALLOW_WEAK
17+
#endif
18+
19+
#if CONFIG_MDNS_TASK_CREATE_FROM_SPIRAM
20+
#define MDNS_TASK_MEMORY_CAPS (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)
21+
#define MDNS_TASK_MEMORY_LOG "SPIRAM"
22+
#endif
23+
#if CONFIG_MDNS_TASK_CREATE_FROM_INTERNAL
24+
#define MDNS_TASK_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
25+
#define MDNS_TASK_MEMORY_LOG "internal RAM"
26+
#endif
27+
28+
#if CONFIG_MDNS_MEMORY_ALLOC_SPIRAM
29+
#define MDNS_MEMORY_CAPS (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)
30+
#endif
31+
#if CONFIG_MDNS_MEMORY_ALLOC_INTERNAL
32+
#define MDNS_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
33+
#endif
34+
35+
// Allocate memory from internal heap as default.
1136
#ifndef MDNS_MEMORY_CAPS
1237
#warning "No memory allocation method defined, using internal memory"
1338
#define MDNS_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
1439
#endif
40+
#ifndef MDNS_TASK_MEMORY_CAPS
41+
#define MDNS_TASK_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
42+
#define MDNS_TASK_MEMORY_LOG "internal RAM"
43+
#endif
1544

16-
void *mdns_mem_malloc(size_t size)
45+
void ALLOW_WEAK *mdns_mem_malloc(size_t size)
1746
{
1847
return heap_caps_malloc(size, MDNS_MEMORY_CAPS);
1948
}
2049

21-
void *mdns_mem_calloc(size_t num, size_t size)
50+
void ALLOW_WEAK *mdns_mem_calloc(size_t num, size_t size)
2251
{
2352
return heap_caps_calloc(num, size, MDNS_MEMORY_CAPS);
2453
}
2554

26-
void mdns_mem_free(void *ptr)
55+
void ALLOW_WEAK mdns_mem_free(void *ptr)
2756
{
2857
heap_caps_free(ptr);
2958
}
3059

31-
char *mdns_mem_strdup(const char *s)
60+
char ALLOW_WEAK *mdns_mem_strdup(const char *s)
3261
{
3362
if (!s) {
3463
return NULL;
@@ -41,7 +70,7 @@ char *mdns_mem_strdup(const char *s)
4170
return copy;
4271
}
4372

44-
char *mdns_mem_strndup(const char *s, size_t n)
73+
char ALLOW_WEAK *mdns_mem_strndup(const char *s, size_t n)
4574
{
4675
if (!s) {
4776
return NULL;
@@ -54,3 +83,14 @@ char *mdns_mem_strndup(const char *s, size_t n)
5483
}
5584
return copy;
5685
}
86+
87+
void ALLOW_WEAK *mdns_mem_task_malloc(size_t size)
88+
{
89+
ESP_LOGI("mdns_mem", "mDNS task will be created from %s", MDNS_TASK_MEMORY_LOG);
90+
return heap_caps_malloc(size, MDNS_TASK_MEMORY_CAPS);
91+
}
92+
93+
void ALLOW_WEAK mdns_mem_task_free(void *ptr)
94+
{
95+
heap_caps_free(ptr);
96+
}

components/mdns/private_include/mdns_mem_caps.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ char *mdns_mem_strdup(const char *s);
4747
*/
4848
char *mdns_mem_strndup(const char *s, size_t n);
4949

50+
/**
51+
* @brief Allocate memory for mDNS task.
52+
* @param size Number of bytes to allocate.
53+
* @return Pointer to allocated memory, or NULL on failure.
54+
*/
55+
void *mdns_mem_task_malloc(size_t size);
56+
57+
/**
58+
* @brief Free allocated memory for mDNS task.
59+
* @param ptr Pointer to memory to free.
60+
*/
61+
void mdns_mem_task_free(void *ptr);
62+
5063
#ifdef __cplusplus
5164
}
5265
#endif

components/mdns/private_include/mdns_private.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,6 @@
2121
#define _mdns_dbg_printf(...) printf(__VA_ARGS__)
2222
#endif
2323

24-
#if CONFIG_MDNS_TASK_CREATE_FROM_SPIRAM
25-
#define MDNS_TASK_MEMORY_CAPS (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)
26-
#define MDNS_TASK_MEMORY_LOG "SPIRAM"
27-
#endif
28-
#if CONFIG_MDNS_TASK_CREATE_FROM_INTERNAL
29-
#define MDNS_TASK_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
30-
#define MDNS_TASK_MEMORY_LOG "internal RAM"
31-
#endif
32-
33-
// Allocate memory from internal heap as default.
34-
#ifndef MDNS_TASK_MEMORY_CAPS
35-
#define MDNS_TASK_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
36-
#define MDNS_TASK_MEMORY_LOG "internal RAM"
37-
#endif
38-
39-
#if CONFIG_MDNS_MEMORY_ALLOC_SPIRAM
40-
#define MDNS_MEMORY_CAPS (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)
41-
#endif
42-
#if CONFIG_MDNS_MEMORY_ALLOC_INTERNAL
43-
#define MDNS_MEMORY_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
44-
#endif
45-
4624
/** Number of predefined interfaces */
4725
#ifndef CONFIG_MDNS_PREDEF_NETIF_STA
4826
#define CONFIG_MDNS_PREDEF_NETIF_STA 0

components/mdns/tests/test_afl_fuzz_host/esp32_mock.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,6 @@ uint32_t esp_log_timestamp(void)
126126
return 0;
127127
}
128128

129-
void *heap_caps_malloc(size_t size, uint32_t caps)
130-
{
131-
return malloc(size);
132-
}
133-
134-
void heap_caps_free(void *ptr)
135-
{
136-
free(ptr);
137-
}
138-
139129
void *mdns_mem_malloc(size_t size)
140130
{
141131
return malloc(size);
@@ -160,3 +150,13 @@ char *mdns_mem_strndup(const char *s, size_t n)
160150
{
161151
return strndup(s, n);
162152
}
153+
154+
void *mdns_mem_task_malloc(size_t size)
155+
{
156+
return malloc(size);
157+
}
158+
159+
void mdns_mem_task_free(void *ptr)
160+
{
161+
free(ptr);
162+
}

components/mdns/tests/test_afl_fuzz_host/esp32_mock.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,5 @@ esp_err_t esp_event_handler_unregister(const char *event_base, int32_t event_id,
138138
TaskHandle_t xTaskGetCurrentTaskHandle(void);
139139
void xTaskNotifyGive(TaskHandle_t task);
140140
BaseType_t xTaskNotifyWait(uint32_t bits_entry_clear, uint32_t bits_exit_clear, uint32_t *value, TickType_t wait_time);
141-
void *heap_caps_malloc(size_t size, uint32_t caps);
142-
void heap_caps_free(void *ptr);
143141

144142
#endif //_ESP32_COMPAT_H_

0 commit comments

Comments
 (0)