Skip to content

Commit

Permalink
bodge: sbrk impl stolen from newlib to get minimal libc working w/ …
Browse files Browse the repository at this point in the history
…microblaze

Signed-off-by: Alp Sayin <[email protected]>
  • Loading branch information
alpsayin committed Oct 18, 2024
1 parent 4f0f6c9 commit 9dab8d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions arch/microblaze/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ zephyr_library_sources(
thread.c
)

if (CONFIG_MINIMAL_LIBC)
zephyr_library_sources(sbrk.c)
endif()

zephyr_library_sources_ifdef(CONFIG_IRQ_OFFLOAD irq_offload.c)
32 changes: 32 additions & 0 deletions arch/microblaze/core/sbrk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#include <zephyr/devicetree.h>
#include <zephyr/linker/linker-defs.h>

#define _DDR_NODE DT_CHOSEN(zephyr_sram)
#define _LAYOUT_DDR_LOC DT_REG_ADDR(_DDR_NODE)
#define _LAYOUT_DDR_SIZE DT_REG_SIZE(_DDR_NODE)

/* Current offset from HEAP_BASE of unused memory */
__attribute__((section(".bss"), used)) static size_t heap_sz;

#define HEAP_BASE ((uintptr_t) (&_end))
#define MAX_HEAP_SIZE (_LAYOUT_DDR_LOC + _LAYOUT_DDR_SIZE - HEAP_BASE)

/* Implementation stolen from newlib/libc-hooks.c */
void *_sbrk(intptr_t count)
{
void *ret, *ptr;

ptr = ((char *)HEAP_BASE) + heap_sz;

if ((heap_sz + count) < MAX_HEAP_SIZE) {
heap_sz += count;
ret = ptr;

} else {
ret = (void *)-1;
}

return ret;
}
__weak FUNC_ALIAS(_sbrk, sbrk, void *);

0 comments on commit 9dab8d0

Please sign in to comment.