forked from tylerwhall/zephyr-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrust-smem.c
66 lines (56 loc) · 1.87 KB
/
rust-smem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <zephyr.h>
#include <init.h>
#include <version.h>
#if ZEPHYR_VERSION_CODE >= ZEPHYR_VERSION(2, 2, 0)
#include <sys/mempool.h>
#else
#include <misc/mempool.h>
#endif
#include <app_memory/app_memdomain.h>
#ifdef CONFIG_USERSPACE
struct k_mem_domain rust_std_domain;
K_APPMEM_PARTITION_DEFINE(rust_std_partition);
#define RUST_STD_SECTION K_APP_DMEM_SECTION(rust_std_partition)
#else
#define RUST_STD_SECTION .data
#endif
#if defined(CONFIG_RUST_ALLOC_POOL)
SYS_MEM_POOL_DEFINE(rust_std_mem_pool, NULL, CONFIG_RUST_HEAP_MEM_POOL_MIN_SIZE,
CONFIG_RUST_HEAP_MEM_POOL_MAX_SIZE, CONFIG_RUST_HEAP_MEM_POOL_NMAX, 8, RUST_STD_SECTION);
#elif CONFIG_HEAP_MEM_POOL_SIZE == 0
#error CONFIG_HEAP_MEM_POOL_SIZE (k_malloc) must be non-zero if not using a Rust sys mem pool.
#endif
#if defined(CONFIG_USERSPACE) && defined(CONFIG_RUST_MUTEX_POOL)
static void mutex_pool_access_grant(void)
{
extern struct k_mutex rust_mutex_pool[CONFIG_RUST_MUTEX_POOL_SIZE];
for (size_t i = 0; i < ARRAY_SIZE(rust_mutex_pool); i++) {
k_object_access_all_grant(&rust_mutex_pool[i]);
}
}
#else
static inline void mutex_pool_access_grant(void) {}
#endif
#if defined(CONFIG_USERSPACE) || defined(CONFIG_RUST_ALLOC_POOL)
/* Harmless API difference that generates a warning */
#if ZEPHYR_VERSION_CODE >= ZEPHYR_VERSION(2, 4, 0)
static int rust_std_init(const struct device *arg)
#else
static int rust_std_init(struct device *arg)
#endif
{
ARG_UNUSED(arg);
#ifdef CONFIG_USERSPACE
struct k_mem_partition *rust_std_parts[] = { &rust_std_partition };
k_mem_domain_init(&rust_std_domain, ARRAY_SIZE(rust_std_parts), rust_std_parts);
#ifdef CONFIG_RUST_MUTEX_POOL
#endif
#endif
#ifdef CONFIG_RUST_ALLOC_POOL
sys_mem_pool_init(&rust_std_mem_pool);
#endif
mutex_pool_access_grant();
return 0;
}
SYS_INIT(rust_std_init, PRE_KERNEL_2, CONFIG_APPLICATION_INIT_PRIORITY);
#endif