diff --git a/photon/libc/malloc/dlmalloc/malloc.c b/photon/libc/malloc/dlmalloc/malloc.c index 4942e8b..969e6ad 100644 --- a/photon/libc/malloc/dlmalloc/malloc.c +++ b/photon/libc/malloc/dlmalloc/malloc.c @@ -242,10 +242,10 @@ #define LACKS_SYS_PARAM_H #define LACKS_SYS_MMAN_H -extern void *__extend_heap(size_t sz); +extern void *__crt_heap_extend(size_t sz); #define HAVE_MMAP 0 -#define MORECORE __extend_heap +#define MORECORE __crt_heap_extend #define MORECORE_CONTIGUOUS 1 #endif diff --git a/photon/libc/sys/magenta/heap.c b/photon/libc/sys/magenta/heap.c index 04fff00..2b91a29 100644 --- a/photon/libc/sys/magenta/heap.c +++ b/photon/libc/sys/magenta/heap.c @@ -1,47 +1,62 @@ -#include #include #include -#include "__init.h" -#include "__heap.h" +#include +#include +#include +#include +#include #define ROUND_UP(x, b) x += (b) - ((x) % (b)) static uintptr_t heap_start = 0; static uintptr_t heap_end = 0; +static uintptr_t heap_alloc_point = 0; static size_t heap_sz = 0; static mx_handle_t heap_vmo = MX_NULL_HANDLE; -int __heap_init() +void __crt_heap_init(size_t heap_sz) { -#if 0 - heap_start = (uintptr_t)info->heap_map_base; - heap_end = (uintptr_t)info + sizeof(*info); - ROUND_UP(heap_end, 0x1000); - heap_segment = info->heap; + mx_status_t status = mx_vmo_create( + heap_sz, + MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC, + &heap_vmo); - unsigned long sz = 0; - mx_segment_get_size(heap_segment, &sz); - heap_sz = sz; -#endif + mx_vaddr_t heap = 0; + mx_vmar_map(m_get_handle(M_HND_VMAR_ROOT), + MX_VM_PERM_READ | MX_VM_PERM_WRITE, + 0, heap_vmo, 0, heap_sz, + &heap); - return 0; + heap_start = heap; + heap_end = heap + heap_sz; + heap_alloc_point = heap_start; } -void *__extend_heap(size_t sz) +void *__crt_heap_extend(size_t sz) { -#if 0 - if (sz == 0) { - return (void *)(heap_end + 1); + if (!heap_start) { + __crt_heap_init(sz); + return (void *)heap_start; } - void *start = (void *)heap_start; + if (sz % MX_PAGE_SIZE) { + sz &= (MX_PAGE_SIZE - 1); + sz += MX_PAGE_SIZE; + } - uintptr_t ret = heap_end; - mx_segment_set_size(heap_segment, heap_sz + sz); - mx_segment_map(heap_segment, 0, heap_sz + sz, &start); + mx_vmo_set_size(heap_vmo, heap_sz + sz); + + mx_vaddr_t vmar_base, alloc_base; + mx_vmar_bounds(m_get_handle(M_HND_VMAR_ROOT), &vmar_base, NULL); + mx_vaddr_t offset = heap_end - vmar_base; + + mx_vmar_map(m_get_handle(M_HND_VMAR_ROOT), + MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC, + offset, heap_vmo, heap_sz, sz, &alloc_base); heap_sz += sz; + + void *out = (void *)heap_end; heap_end += sz; - return (void *)ret; -#endif - return NULL; + + return out; }