Skip to content

Commit 90952df

Browse files
shubhamkulkarni97sylvioalves
authored andcommitted
hal: Add wrappers for k_aligned_alloc and k_calloc.
Add heap_caps.c for multi heap implementation Signed-off-by: Shubham Kulkarni <shubham.kulkarni@espressif.com>
1 parent 0030783 commit 90952df

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

zephyr/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ if(CONFIG_SOC_ESP32)
6161
adapter/src/hal/windowspill_asm.S
6262
)
6363

64+
zephyr_link_libraries_ifdef(
65+
CONFIG_HEAP_MEM_POOL_SIZE
66+
gcc
67+
"-Wl,--wrap=k_calloc"
68+
"-Wl,--wrap=k_aligned_alloc"
69+
)
70+
6471
zephyr_sources_ifdef(
6572
CONFIG_SOC_FLASH_ESP32
6673
../components/spi_flash/flash_mmap.c
@@ -99,6 +106,7 @@ if(CONFIG_SOC_ESP32)
99106
../components/log/log.c
100107
adapter/src/common/dport_access.c
101108
adapter/src/stubs.c
109+
adapter/src/heap_caps.c
102110
)
103111

104112
## shared WIFI/BT resources

zephyr/adapter/src/heap_caps.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <string.h>
9+
#include <sys/math_extras.h>
10+
#include <esp32/spiram.h>
11+
#include <esp_attr.h>
12+
13+
#if (CONFIG_ESP_SPIRAM || (CONFIG_HEAP_MEM_POOL_SIZE > 0) || (CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE > 0))
14+
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
15+
void *__real_k_aligned_alloc(size_t align, size_t size);
16+
void *__real_k_calloc(size_t nmemb, size_t size);
17+
#endif
18+
19+
#if (CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE > 0)
20+
char __aligned(sizeof(void *)) __NOINIT_ATTR dram0_seg_1_heap[CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE];
21+
Z_STRUCT_SECTION_ITERABLE(k_heap, _internal_heap_1) = {
22+
.heap = {
23+
.init_mem = dram0_seg_1_heap,
24+
.init_bytes = CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE,
25+
}
26+
};
27+
#endif
28+
29+
#if defined(CONFIG_ESP_SPIRAM)
30+
EXT_RAM_ATTR int _spiram_data_start;
31+
Z_STRUCT_SECTION_ITERABLE(k_heap, _spiram_heap) = {
32+
.heap = {
33+
.init_mem = &_spiram_data_start,
34+
#if (CONFIG_ESP_SPIRAM_SIZE <= 0x400000)
35+
.init_bytes = CONFIG_ESP_SPIRAM_SIZE,
36+
#else
37+
.init_bytes = 0x400000,
38+
#endif
39+
},
40+
};
41+
#endif
42+
43+
#if (CONFIG_ESP_SPIRAM || (CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE > 0))
44+
static void *z_esp_aligned_alloc(struct k_heap *heap, size_t align, size_t size)
45+
{
46+
void *mem;
47+
struct k_heap **heap_ref;
48+
size_t __align;
49+
50+
/*
51+
* Adjust the size to make room for our heap reference.
52+
* Merge a rewind bit with align value (see sys_heap_aligned_alloc()).
53+
* This allows for storing the heap pointer right below the aligned
54+
* boundary without wasting any memory.
55+
*/
56+
if (size_add_overflow(size, sizeof(heap_ref), &size)) {
57+
return NULL;
58+
}
59+
__align = align | sizeof(heap_ref);
60+
61+
mem = k_heap_aligned_alloc(heap, __align, size, K_NO_WAIT);
62+
if (mem == NULL) {
63+
return NULL;
64+
}
65+
66+
heap_ref = mem;
67+
*heap_ref = heap;
68+
mem = ++heap_ref;
69+
__ASSERT(align == 0 || ((uintptr_t)mem & (align - 1)) == 0,
70+
"misaligned memory at %p (align = %zu)", mem, align);
71+
72+
return mem;
73+
}
74+
75+
static void *z_esp_aligned_calloc(struct k_heap *heap, size_t nmemb, size_t size)
76+
{
77+
void *ret;
78+
size_t bounds;
79+
if (size_mul_overflow(nmemb, size, &bounds)) {
80+
return NULL;
81+
}
82+
ret = z_esp_aligned_alloc(heap, sizeof(void *), bounds);
83+
if (ret != NULL) {
84+
(void)memset(ret, 0, bounds);
85+
}
86+
return ret;
87+
}
88+
#endif
89+
90+
static void *z_esp_alloc_internal(size_t align, size_t size)
91+
{
92+
void *ptr = NULL;
93+
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
94+
ptr = __real_k_aligned_alloc(align, size);
95+
#endif
96+
#if (CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE > 0)
97+
if (ptr == NULL) {
98+
ptr = z_esp_aligned_alloc(&_internal_heap_1, align, size);
99+
}
100+
#endif
101+
return ptr;
102+
}
103+
104+
static void *z_esp_calloc_internal(size_t nmemb, size_t size)
105+
{
106+
void *ptr = NULL;
107+
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
108+
ptr = __real_k_calloc(nmemb, size);
109+
#endif
110+
#if (CONFIG_ESP_HEAP_MEM_POOL_REGION_1_SIZE > 0)
111+
if (ptr == NULL) {
112+
ptr = z_esp_aligned_calloc(&_internal_heap_1, nmemb, size);
113+
}
114+
#endif
115+
return ptr;
116+
}
117+
118+
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
119+
void *__wrap_k_aligned_alloc(size_t align, size_t size)
120+
#else
121+
void *k_aligned_alloc(size_t align, size_t size)
122+
#endif
123+
{
124+
void *ptr = NULL;
125+
#if defined(CONFIG_ESP_SPIRAM)
126+
if (size < CONFIG_ESP_HEAP_MIN_EXTRAM_THRESHOLD) {
127+
#endif
128+
ptr = z_esp_alloc_internal(align, size);
129+
#if defined(CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS)
130+
if (ptr == NULL) {
131+
ptr = z_esp_aligned_alloc(&_spiram_heap, align, size);
132+
}
133+
#endif
134+
#if defined(CONFIG_ESP_SPIRAM)
135+
} else {
136+
ptr = z_esp_aligned_alloc(&_spiram_heap, align, size);
137+
#if defined(CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS)
138+
if (ptr == NULL) {
139+
ptr = z_esp_alloc_internal(align, size);
140+
}
141+
#endif
142+
}
143+
#endif
144+
return ptr;
145+
}
146+
147+
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
148+
void *__wrap_k_calloc(size_t nmemb, size_t size)
149+
#else
150+
void *k_calloc(size_t nmemb, size_t size)
151+
#endif
152+
{
153+
void *ptr = NULL;
154+
#if defined(CONFIG_ESP_SPIRAM)
155+
if (size < CONFIG_ESP_HEAP_MIN_EXTRAM_THRESHOLD) {
156+
#endif
157+
ptr = z_esp_calloc_internal(nmemb, size);
158+
#if defined(CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS)
159+
if (ptr == NULL) {
160+
ptr = z_esp_aligned_calloc(&_spiram_heap, nmemb, size);
161+
}
162+
#endif
163+
#if defined(CONFIG_ESP_SPIRAM)
164+
} else {
165+
ptr = z_esp_aligned_calloc(&_spiram_heap, nmemb, size);
166+
#if defined(CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS)
167+
if (ptr == NULL) {
168+
ptr = z_esp_calloc_internal(nmemb, size);
169+
}
170+
#endif
171+
}
172+
#endif
173+
return ptr;
174+
}
175+
#endif

0 commit comments

Comments
 (0)