Skip to content

Commit 21364d6

Browse files
committed
Kconfig: select whether to use a mem pool or kmalloc
Not automatically tied into the default global allocator yet.
1 parent d3be39f commit 21364d6

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

Kconfig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,44 @@ menuconfig RUST
44
select CPLUSPLUS
55
help
66
Rust language support.
7+
8+
if RUST
9+
choice RUST_GLOBAL_ALLOCATOR
10+
prompt "Rust global allocator"
11+
default RUST_ALLOC_POOL
12+
help
13+
The global allocator can either use k_malloc or a dedicated sys mem
14+
pool for Rust allocations. A dedicated pool is required for userspace
15+
because the pool must be placed in a shared memory region accessible
16+
to all Rust userspace threads.
17+
18+
config RUST_ALLOC_KMALLOC
19+
bool "k_malloc"
20+
depends on !USERSPACE
21+
help
22+
Adjust the pool size with CONFIG_HEAP_MEM_POOL_SIZE. Must be non-zero.
23+
24+
config RUST_ALLOC_POOL
25+
bool "Dedicated memory pool"
26+
help
27+
Required for userspace.
28+
endchoice
29+
30+
if RUST_ALLOC_POOL
31+
config RUST_HEAP_MEM_POOL_SIZE
32+
int "Rust heap memory pool size (in bytes)"
33+
default 1024
34+
help
35+
This option specifies the size of the heap memory pool created
36+
specifically to act as the Rust global allocator. Must be a power of
37+
2. A size of zero means that no heap memory pool is defined.
38+
39+
config RUST_HEAP_MEM_POOL_MIN_SIZE
40+
int "The smallest blocks in the Rust heap memory pool (in bytes)"
41+
default 16
42+
help
43+
This option specifies the size of the smallest block in the pool.
44+
Option must be a power of 2 and lower than or equal to the size
45+
of the entire pool.
46+
endif
47+
endif

rust-smem.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
* Copyright (c) 2012-2014 Wind River Systems, Inc.
3-
*
4-
* SPDX-License-Identifier: Apache-2.0
5-
*/
6-
71
#include <zephyr.h>
82
#include <init.h>
93
#include <misc/mempool.h>
@@ -18,10 +12,14 @@ K_APPMEM_PARTITION_DEFINE(rust_std_partition);
1812
#define RUST_STD_SECTION .data
1913
#endif
2014

21-
#define RUST_STD_MEM_POOL_SIZE 1024
22-
SYS_MEM_POOL_DEFINE(rust_std_mem_pool, NULL, 16,
23-
RUST_STD_MEM_POOL_SIZE, 1, 4, RUST_STD_SECTION);
15+
#if defined(CONFIG_RUST_ALLOC_POOL)
16+
SYS_MEM_POOL_DEFINE(rust_std_mem_pool, NULL, CONFIG_RUST_HEAP_MEM_POOL_MIN_SIZE,
17+
CONFIG_RUST_HEAP_MEM_POOL_SIZE, 1, 4, RUST_STD_SECTION);
18+
#elif CONFIG_HEAP_MEM_POOL_SIZE == 0
19+
#error CONFIG_HEAP_MEM_POOL_SIZE (k_malloc) must be non-zero if not using a Rust sys mem pool.
20+
#endif
2421

22+
#if defined(CONFIG_USERSPACE) || defined(CONFIG_RUST_ALLOC_POOL)
2523
static int rust_std_init(struct device *arg)
2624
{
2725
ARG_UNUSED(arg);
@@ -30,9 +28,12 @@ static int rust_std_init(struct device *arg)
3028
k_mem_domain_init(&rust_std_domain, 0, NULL);
3129
k_mem_domain_add_partition(&rust_std_domain, &rust_std_partition);
3230
#endif
31+
#ifdef CONFIG_RUST_ALLOC_POOL
3332
sys_mem_pool_init(&rust_std_mem_pool);
33+
#endif
3434

3535
return 0;
3636
}
3737

3838
SYS_INIT(rust_std_init, PRE_KERNEL_2, CONFIG_APPLICATION_INIT_PRIORITY);
39+
#endif

samples/rust-app/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CONFIG_RUST=y
2+
CONFIG_RUST_ALLOC_POOL=y
23
CONFIG_SERIAL=y
34
CONFIG_UART_NATIVE_POSIX=y
45
CONFIG_NATIVE_UART_0_ON_OWN_PTY=y

0 commit comments

Comments
 (0)