Skip to content

Commit b410410

Browse files
committed
fix(lwip): Use static queue for LwIP mbox
This avoids duplicated malloc, on a congested code path. Avoids additional heap overhead, increases speed and lowers memory usage.
1 parent ecc0c38 commit b410410

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

components/lwip/port/freertos/include/arch/sys_arch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ typedef SemaphoreHandle_t sys_mutex_t;
2323
typedef TaskHandle_t sys_thread_t;
2424

2525
typedef struct sys_mbox_s {
26-
QueueHandle_t os_mbox;
26+
StaticQueue_t os_mbox;
27+
uint8_t buffer[0];
2728
}* sys_mbox_t;
2829

2930
/** This is returned by _fromisr() sys functions to tell the outermost function

components/lwip/port/freertos/sys_arch.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,21 @@ sys_sem_free(sys_sem_t *sem)
205205
err_t
206206
sys_mbox_new(sys_mbox_t *mbox, int size)
207207
{
208-
*mbox = mem_malloc(sizeof(struct sys_mbox_s));
208+
*mbox = (sys_mbox_t)mem_malloc(sizeof(struct sys_mbox_s) + (size * sizeof(void *)));
209209
if (*mbox == NULL){
210210
LWIP_DEBUGF(SYS_DEBUG, ("fail to new *mbox\n"));
211211
return ERR_MEM;
212212
}
213213

214-
(*mbox)->os_mbox = xQueueCreate(size, sizeof(void *));
214+
QueueHandle_t res = xQueueCreateStatic(size, sizeof(void *), (*mbox)->buffer, &(*mbox)->os_mbox);
215215

216-
if ((*mbox)->os_mbox == NULL) {
216+
if ((QueueHandle_t)&(*mbox)->os_mbox != res) {
217217
LWIP_DEBUGF(SYS_DEBUG, ("fail to new (*mbox)->os_mbox\n"));
218-
mem_free(*mbox);
218+
free(*mbox);
219219
return ERR_MEM;
220220
}
221221

222-
LWIP_DEBUGF(SYS_DEBUG, ("new *mbox ok mbox=%p os_mbox=%p\n", *mbox, (*mbox)->os_mbox));
222+
LWIP_DEBUGF(SYS_DEBUG, ("new *mbox ok mbox=%p os_mbox=%p\n", *mbox, (QueueHandle_t)&(*mbox)->os_mbox));
223223
return ERR_OK;
224224
}
225225

@@ -232,7 +232,7 @@ sys_mbox_new(sys_mbox_t *mbox, int size)
232232
void
233233
sys_mbox_post(sys_mbox_t *mbox, void *msg)
234234
{
235-
BaseType_t ret = xQueueSendToBack((*mbox)->os_mbox, &msg, portMAX_DELAY);
235+
BaseType_t ret = xQueueSendToBack((QueueHandle_t)&(*mbox)->os_mbox, &msg, portMAX_DELAY);
236236
LWIP_ASSERT("mbox post failed", ret == pdTRUE);
237237
(void)ret;
238238
}
@@ -249,10 +249,10 @@ sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
249249
{
250250
err_t xReturn;
251251

252-
if (xQueueSend((*mbox)->os_mbox, &msg, 0) == pdTRUE) {
252+
if (xQueueSend((QueueHandle_t)&(*mbox)->os_mbox, &msg, 0) == pdTRUE) {
253253
xReturn = ERR_OK;
254254
} else {
255-
LWIP_DEBUGF(SYS_DEBUG, ("trypost mbox=%p fail\n", (*mbox)->os_mbox));
255+
LWIP_DEBUGF(SYS_DEBUG, ("trypost mbox=%p fail\n", (QueueHandle_t)&(*mbox)->os_mbox));
256256
xReturn = ERR_MEM;
257257
}
258258

@@ -274,7 +274,7 @@ sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg)
274274
BaseType_t ret;
275275
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
276276

277-
ret = xQueueSendFromISR((*mbox)->os_mbox, &msg, &xHigherPriorityTaskWoken);
277+
ret = xQueueSendFromISR((QueueHandle_t)&(*mbox)->os_mbox, &msg, &xHigherPriorityTaskWoken);
278278
if (ret == pdTRUE) {
279279
if (xHigherPriorityTaskWoken == pdTRUE) {
280280
return ERR_NEED_SCHED;
@@ -306,11 +306,11 @@ sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
306306

307307
if (timeout == 0) {
308308
/* wait infinite */
309-
ret = xQueueReceive((*mbox)->os_mbox, &(*msg), portMAX_DELAY);
309+
ret = xQueueReceive((QueueHandle_t)&(*mbox)->os_mbox, &(*msg), portMAX_DELAY);
310310
LWIP_ASSERT("mbox fetch failed", ret == pdTRUE);
311311
} else {
312312
TickType_t timeout_ticks = timeout / portTICK_PERIOD_MS;
313-
ret = xQueueReceive((*mbox)->os_mbox, &(*msg), timeout_ticks);
313+
ret = xQueueReceive((QueueHandle_t)&(*mbox)->os_mbox, &(*msg), timeout_ticks);
314314
if (ret == errQUEUE_EMPTY) {
315315
/* timed out */
316316
*msg = NULL;
@@ -338,7 +338,7 @@ sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
338338
if (msg == NULL) {
339339
msg = &msg_dummy;
340340
}
341-
ret = xQueueReceive((*mbox)->os_mbox, &(*msg), 0);
341+
ret = xQueueReceive((QueueHandle_t)&(*mbox)->os_mbox, &(*msg), 0);
342342
if (ret == errQUEUE_EMPTY) {
343343
*msg = NULL;
344344
return SYS_MBOX_EMPTY;
@@ -359,10 +359,10 @@ sys_mbox_free(sys_mbox_t *mbox)
359359
if ((NULL == mbox) || (NULL == *mbox)) {
360360
return;
361361
}
362-
UBaseType_t msgs_waiting = uxQueueMessagesWaiting((*mbox)->os_mbox);
362+
UBaseType_t msgs_waiting = uxQueueMessagesWaiting((QueueHandle_t)&(*mbox)->os_mbox);
363363
LWIP_ASSERT("mbox quence not empty", msgs_waiting == 0);
364364

365-
vQueueDelete((*mbox)->os_mbox);
365+
vQueueDelete((QueueHandle_t)&(*mbox)->os_mbox);
366366
mem_free(*mbox);
367367
*mbox = NULL;
368368

0 commit comments

Comments
 (0)