diff --git a/photon/libc/sys/magenta/__heap.h b/photon/libc/sys/magenta/__heap.h new file mode 100644 index 0000000..5b49cdf --- /dev/null +++ b/photon/libc/sys/magenta/__heap.h @@ -0,0 +1,8 @@ +#ifndef SYS_MAGENTA_HEAP_H_ +#define SYS_MAGENTA_HEAP_H_ + +#include "__init.h" + +extern int __heap_init(struct __proc_start_info *info); + +#endif diff --git a/photon/libc/sys/magenta/init.h b/photon/libc/sys/magenta/__init.h similarity index 82% rename from photon/libc/sys/magenta/init.h rename to photon/libc/sys/magenta/__init.h index 4fc3596..2b959ca 100644 --- a/photon/libc/sys/magenta/init.h +++ b/photon/libc/sys/magenta/__init.h @@ -9,6 +9,8 @@ struct __proc_start_info { mx_handle_t err; int argc; const char **argv; + mx_handle_t heap; + void *heap_map_base; }; #endif diff --git a/photon/libc/sys/magenta/config.cmake b/photon/libc/sys/magenta/config.cmake index 383e863..f6289f8 100644 --- a/photon/libc/sys/magenta/config.cmake +++ b/photon/libc/sys/magenta/config.cmake @@ -1,3 +1,5 @@ set(photon_platform_extra_source_dirs libmagenta/libmagenta) -set(photon_platform_extra_include_dirs libmagenta/libmagenta/arch/${machine}) +set(photon_platform_extra_include_dirs + libmagenta/libmagenta + libmagenta/libmagenta/arch/${machine}) set(photon_platform_libs magenta) diff --git a/photon/libc/sys/magenta/heap.c b/photon/libc/sys/magenta/heap.c new file mode 100644 index 0000000..d82d77f --- /dev/null +++ b/photon/libc/sys/magenta/heap.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include "__init.h" +#include "__heap.h" + +#define ROUND_UP(x, b) x += (b) - ((x) % (b)) + +static uintptr_t heap_start = 0; +static uintptr_t heap_end = 0; +static size_t heap_sz = 0; +static mx_handle_t heap_segment = MX_NULL_HANDLE; + +int __heap_init(struct __proc_start_info *info) +{ + heap_start = (uintptr_t)info->heap_map_base; + heap_end = (uintptr_t)info + sizeof(*info); + ROUND_UP(heap_end, 0x1000); + heap_segment = info->heap; + + unsigned long sz = 0; + mx_segment_get_size(heap_segment, &sz); + heap_sz = sz; + + return 0; +} + +void *__extend_heap(size_t sz) +{ + if (sz == 0) { + return (void *)(heap_end + 1); + } + + void *start = (void *)heap_start; + + uintptr_t ret = heap_end; + mx_segment_set_size(heap_segment, heap_sz + sz); + mx_segment_map(heap_segment, 0, heap_sz + sz, &start); + heap_end += sz; + return (void *)ret; +} diff --git a/photon/libc/sys/magenta/init.c b/photon/libc/sys/magenta/init.c index ff26ba7..e300fb0 100644 --- a/photon/libc/sys/magenta/init.c +++ b/photon/libc/sys/magenta/init.c @@ -1,4 +1,5 @@ -#include "init.h" +#include "__init.h" +#include "__heap.h" #include "__fio.h" extern int main(int, const char **); @@ -6,5 +7,6 @@ extern int main(int, const char **); int __crt_init(struct __proc_start_info *info) { __fio_init(info->in, info->out, info->err); + __heap_init(info); return main(info->argc, info->argv); } diff --git a/photon/libc/sys/magenta/libmagenta b/photon/libc/sys/magenta/libmagenta index 04ccced..51e77c9 160000 --- a/photon/libc/sys/magenta/libmagenta +++ b/photon/libc/sys/magenta/libmagenta @@ -1 +1 @@ -Subproject commit 04ccced9d4c2e1251e2fce89d084c93ce8ac3527 +Subproject commit 51e77c9cfdb9988e55df4f56257c8ab0dadda1b1