Skip to content

Commit 6587d8a

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 12aca40 commit 6587d8a

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;
@@ -1129,7 +1129,7 @@ static size_t cmd_rx_handler(struct sm_at_host_ctx *ctx, uint8_t c)
11291129
rsp_send_error();
11301130
goto cmd_finnish_or_fail;
11311131
}
1132-
uint8_t *new_buf = k_realloc(ctx->at_buf, new_size);
1132+
uint8_t *new_buf = realloc(ctx->at_buf, new_size);
11331133

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

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

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

17121712
/* Allocate new instance from heap */
1713-
ctx = k_malloc(sizeof(*ctx));
1713+
ctx = malloc(sizeof(*ctx));
17141714
if (!ctx) {
17151715
LOG_ERR("Failed to allocate AT host context");
17161716
return NULL;
@@ -1719,7 +1719,7 @@ static struct sm_at_host_ctx *sm_at_host_create(struct modem_pipe *pipe)
17191719
/* Initialize the context */
17201720
err = sm_at_host_ctx_init(ctx, pipe);
17211721
if (err) {
1722-
k_free(ctx);
1722+
free(ctx);
17231723
return NULL;
17241724
}
17251725

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

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

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

0 commit comments

Comments
 (0)