Skip to content

Commit 9dab8d0

Browse files
committed
bodge: sbrk impl stolen from newlib to get minimal libc working w/ microblaze
Signed-off-by: Alp Sayin <alpsayin@gmail.com>
1 parent 4f0f6c9 commit 9dab8d0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

arch/microblaze/core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ zephyr_library_sources(
2929
thread.c
3030
)
3131

32+
if (CONFIG_MINIMAL_LIBC)
33+
zephyr_library_sources(sbrk.c)
34+
endif()
35+
3236
zephyr_library_sources_ifdef(CONFIG_IRQ_OFFLOAD irq_offload.c)

arch/microblaze/core/sbrk.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#include <zephyr/devicetree.h>
3+
#include <zephyr/linker/linker-defs.h>
4+
5+
#define _DDR_NODE DT_CHOSEN(zephyr_sram)
6+
#define _LAYOUT_DDR_LOC DT_REG_ADDR(_DDR_NODE)
7+
#define _LAYOUT_DDR_SIZE DT_REG_SIZE(_DDR_NODE)
8+
9+
/* Current offset from HEAP_BASE of unused memory */
10+
__attribute__((section(".bss"), used)) static size_t heap_sz;
11+
12+
#define HEAP_BASE ((uintptr_t) (&_end))
13+
#define MAX_HEAP_SIZE (_LAYOUT_DDR_LOC + _LAYOUT_DDR_SIZE - HEAP_BASE)
14+
15+
/* Implementation stolen from newlib/libc-hooks.c */
16+
void *_sbrk(intptr_t count)
17+
{
18+
void *ret, *ptr;
19+
20+
ptr = ((char *)HEAP_BASE) + heap_sz;
21+
22+
if ((heap_sz + count) < MAX_HEAP_SIZE) {
23+
heap_sz += count;
24+
ret = ptr;
25+
26+
} else {
27+
ret = (void *)-1;
28+
}
29+
30+
return ret;
31+
}
32+
__weak FUNC_ALIAS(_sbrk, sbrk, void *);

0 commit comments

Comments
 (0)