Skip to content

Commit aee2846

Browse files
committed
app: Use C library malloc() instead of System heap.
When following Kconfig variables are set: CONFIG_COMMON_LIBC_MALLOC=y CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1 It uses all remaining RAM for heap that is used by C library. This is better for us, than trying to guess a static size beforehand. We still need some amount of system heap for k_malloc(), as at least nRF Cloud libraries seem to be using that. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent 94db5ca commit aee2846

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

app/Kconfig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ config SM_NRF_CLOUD
250250
default y
251251
select EXPERIMENTAL
252252

253+
if SM_NRF_CLOUD
254+
config HEAP_MEM_POOL_ADD_SIZE_SM_NRF_CLOUD
255+
int "Additional heap memory pool size for nRF Cloud support"
256+
default 2048
257+
help
258+
Some libraries used by nRF Cloud use System Heap (k_malloc() / k_free()) for
259+
dynamic memory allocation.
260+
endif # SM_NRF_CLOUD
261+
253262
config SM_MQTTC
254263
bool "MQTT client support"
255264
default y
@@ -298,15 +307,6 @@ config SM_MODEM_PIPE_TIMEOUT
298307

299308
endif # SM_CMUX || SM_PPP
300309

301-
config HEAP_MEM_POOL_ADD_SIZE_SM_AT
302-
int "Additional heap memory pool size for AT Host"
303-
default 16384 if SM_CMUX
304-
default 8192
305-
help
306-
Additional heap memory pool size required for AT Host.
307-
This value is added to the total heap memory pool size defined in
308-
CONFIG_HEAP_MEM_POOL_SIZE.
309-
310310
if SM_MQTTC
311311

312312
config SM_MQTTC_MESSAGE_BUFFER_LEN

app/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ CONFIG_UART_INTERRUPT_DRIVEN=n
6464
CONFIG_MAIN_STACK_SIZE=4096
6565
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
6666
CONFIG_AT_MONITOR_HEAP_SIZE=4096
67+
CONFIG_COMMON_LIBC_MALLOC=y
68+
CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1
6769

6870
# Device power management
6971
CONFIG_PM_DEVICE=y

app/src/sm_at_host.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ static bool set_sm_mode(struct sm_at_host_ctx *ctx, enum sm_operation_mode mode)
534534
if (ctx->data_rb_buf == NULL) {
535535
LOG_DBG("Allocating datamode buffer of size %d",
536536
CONFIG_SM_DATAMODE_BUF_SIZE);
537-
ctx->data_rb_buf = k_malloc(CONFIG_SM_DATAMODE_BUF_SIZE);
537+
ctx->data_rb_buf = malloc(CONFIG_SM_DATAMODE_BUF_SIZE);
538538
if (ctx->data_rb_buf == NULL) {
539539
LOG_ERR("Failed to allocate datamode buffer");
540540
return false;
@@ -1128,7 +1128,7 @@ static size_t cmd_rx_handler(struct sm_at_host_ctx *ctx, uint8_t c)
11281128
rsp_send_error();
11291129
goto cmd_finnish_or_fail;
11301130
}
1131-
uint8_t *new_buf = k_realloc(ctx->at_buf, new_size);
1131+
uint8_t *new_buf = realloc(ctx->at_buf, new_size);
11321132

11331133
if (!new_buf) {
11341134
LOG_ERR("Failed to expand AT command buffer");
@@ -1261,7 +1261,7 @@ static size_t cmd_rx_handler(struct sm_at_host_ctx *ctx, uint8_t c)
12611261
ctx->echo_len = 0;
12621262
/* Release extra AT buffer */
12631263
if (ctx->at_buf_size > AT_BUF_MIN_SIZE) {
1264-
uint8_t *new_buf = k_realloc(ctx->at_buf, AT_BUF_MIN_SIZE);
1264+
uint8_t *new_buf = realloc(ctx->at_buf, AT_BUF_MIN_SIZE);
12651265

12661266
if (new_buf) {
12671267
ctx->at_buf = new_buf;
@@ -1660,7 +1660,7 @@ static int sm_at_host_ctx_init(struct sm_at_host_ctx *ctx, struct modem_pipe *pi
16601660
/* Initialize context structure */
16611661
memset(ctx, 0, sizeof(*ctx));
16621662

1663-
ctx->at_buf = k_malloc(AT_BUF_MIN_SIZE);
1663+
ctx->at_buf = malloc(AT_BUF_MIN_SIZE);
16641664
if (!ctx->at_buf) {
16651665
LOG_ERR("Failed to allocate AT command buffer");
16661666
return -ENOMEM;
@@ -1709,7 +1709,7 @@ static struct sm_at_host_ctx *sm_at_host_create(struct modem_pipe *pipe)
17091709
}
17101710

17111711
/* Allocate new instance from heap */
1712-
ctx = k_malloc(sizeof(*ctx));
1712+
ctx = malloc(sizeof(*ctx));
17131713
if (!ctx) {
17141714
LOG_ERR("Failed to allocate AT host context");
17151715
return NULL;
@@ -1718,7 +1718,7 @@ static struct sm_at_host_ctx *sm_at_host_create(struct modem_pipe *pipe)
17181718
/* Initialize the context */
17191719
err = sm_at_host_ctx_init(ctx, pipe);
17201720
if (err) {
1721-
k_free(ctx);
1721+
free(ctx);
17221722
return NULL;
17231723
}
17241724

@@ -1876,12 +1876,13 @@ static int sm_at_host_destroy(struct sm_at_host_ctx *ctx)
18761876
/* Remove from instance list */
18771877
sys_slist_find_and_remove(&instance_list, &ctx->node);
18781878

1879+
LOG_INF("Destroyed AT host instance %p", (void *)ctx);
1880+
18791881
/* Free the context */
1880-
k_free(ctx->data_rb_buf);
1881-
k_free(ctx->at_buf);
1882-
k_free(ctx);
1882+
free(ctx->data_rb_buf);
1883+
free(ctx->at_buf);
1884+
free(ctx);
18831885

1884-
LOG_INF("Destroyed AT host instance %p", (void *)ctx);
18851886
return 0;
18861887
}
18871888

0 commit comments

Comments
 (0)