1 /*
2 * Copyright (c) 2025 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 */
7
8 #include <zephyr/spinlock.h>
9 #include <zephyr/sys/sys_heap.h>
10 #include <libmctp.h>
11
12 static uint8_t MCTP_MEM[CONFIG_MCTP_HEAP_SIZE];
13
14 static struct {
15 struct k_spinlock lock;
16 struct sys_heap heap;
17 } mctp_heap;
18
mctp_heap_alloc(size_t bytes)19 static void *mctp_heap_alloc(size_t bytes)
20 {
21 k_spinlock_key_t key = k_spin_lock(&mctp_heap.lock);
22
23 void *ptr = sys_heap_alloc(&mctp_heap.heap, bytes);
24
25 k_spin_unlock(&mctp_heap.lock, key);
26
27 return ptr;
28 }
29
mctp_heap_free(void * ptr)30 static void mctp_heap_free(void *ptr)
31 {
32 k_spinlock_key_t key = k_spin_lock(&mctp_heap.lock);
33
34 sys_heap_free(&mctp_heap.heap, ptr);
35
36 k_spin_unlock(&mctp_heap.lock, key);
37 }
38
mctp_heap_realloc(void * ptr,size_t bytes)39 static void *mctp_heap_realloc(void *ptr, size_t bytes)
40 {
41 k_spinlock_key_t key = k_spin_lock(&mctp_heap.lock);
42
43 void *new_ptr = sys_heap_realloc(&mctp_heap.heap, ptr, bytes);
44
45 k_spin_unlock(&mctp_heap.lock, key);
46
47 return new_ptr;
48 }
49
50
mctp_heap_init(void)51 static int mctp_heap_init(void)
52 {
53 sys_heap_init(&mctp_heap.heap, MCTP_MEM, sizeof(MCTP_MEM));
54 mctp_set_alloc_ops(mctp_heap_alloc, mctp_heap_free, mctp_heap_realloc);
55 return 0;
56 }
57
58 SYS_INIT_NAMED(mctp_memory, mctp_heap_init, POST_KERNEL, 0);
59