Files
photon/libc/sys/horizon/heap.c

102 lines
2.6 KiB
C
Raw Normal View History

2020-05-15 19:30:22 +01:00
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
2021-01-16 17:26:12 +00:00
#include <string.h>
#include <stdio.h>
2021-01-16 17:26:12 +00:00
#include <magenta/vmo.h>
2021-08-15 16:25:15 +01:00
#include <magenta/bootstrap.h>
#include <magenta/errors.h>
#include <magenta/misc.h>
2021-01-16 17:26:12 +00:00
#include <magenta/vmar.h>
2020-05-15 19:30:22 +01:00
/* 1 GiB */
#define HEAP_REGION_SZ 0x40000000
2020-05-15 19:30:22 +01:00
#define ROUND_UP(x, b) x += (b) - ((x) % (b))
static uintptr_t heap_start = 0;
static uintptr_t heap_end = 0;
2021-01-16 17:26:12 +00:00
static uintptr_t heap_alloc_point = 0;
2020-05-15 19:30:22 +01:00
static size_t heap_sz = 0;
static mx_handle_t heap_vmo = MX_NULL_HANDLE;
static mx_handle_t heap_vmar = MX_NULL_HANDLE;
2020-05-15 19:30:22 +01:00
2021-03-11 10:12:33 +00:00
void __crt_heap_init(size_t sz)
2020-05-15 19:30:22 +01:00
{
mx_handle_t root_vmar = mx_bootstrap_handle_get(MX_B_VMAR_ROOT);
mx_vaddr_t heap = 0;
mx_status_t status = mx_vmar_allocate(root_vmar,
MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC,
0, HEAP_REGION_SZ, &heap_vmar, &heap);
if (status != MX_OK) {
fprintf(stderr, "fatal: cannot allocate heap virtual region (%s)\n", mx_status_to_string(status));
abort();
}
status = mx_vmo_create(
2021-03-11 10:12:33 +00:00
sz,
2021-01-16 17:26:12 +00:00
MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC,
&heap_vmo);
if (status != MX_OK) {
fprintf(stderr, "fatal: cannot allocate heap (%s)\n", mx_status_to_string(status));
abort();
}
2021-01-16 17:26:12 +00:00
status = mx_vmar_map(heap_vmar,
MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC,
2021-03-11 10:12:33 +00:00
0, heap_vmo, 0, sz,
2021-01-16 17:26:12 +00:00
&heap);
if (status != MX_OK) {
fprintf(stderr, "fatal: cannot map heap (%s)\n", mx_status_to_string(status));
abort();
}
2021-01-16 17:26:12 +00:00
2021-03-11 10:12:33 +00:00
heap_sz = sz;
2021-01-16 17:26:12 +00:00
heap_start = heap;
heap_end = heap + heap_sz;
heap_alloc_point = heap_start;
2021-02-19 13:54:28 +00:00
memset((void *)heap, 0x0, heap_sz);
2020-05-15 19:30:22 +01:00
}
2021-01-16 17:26:12 +00:00
void *__crt_heap_extend(size_t sz)
2020-05-15 19:30:22 +01:00
{
2021-02-19 13:54:28 +00:00
if (sz % MX_PAGE_SIZE) {
sz &= (MX_PAGE_SIZE - 1);
sz += MX_PAGE_SIZE;
}
2021-03-11 10:12:33 +00:00
if (!sz) {
return (void *)heap_end;
}
2021-01-16 17:26:12 +00:00
if (!heap_start) {
__crt_heap_init(sz);
return (void *)heap_start;
2020-05-15 19:30:22 +01:00
}
2021-01-16 17:26:12 +00:00
mx_vmo_set_size(heap_vmo, heap_sz + sz);
mx_vaddr_t vmar_base, alloc_base;
mx_vmar_bounds(heap_vmar, &vmar_base, NULL);
2021-03-11 10:12:33 +00:00
size_t offset = heap_end - vmar_base;
2020-05-15 19:30:22 +01:00
mx_status_t err = mx_vmar_map(heap_vmar,
2021-01-16 17:26:12 +00:00
MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC,
offset, heap_vmo, heap_sz, sz, &alloc_base);
if (err != MX_OK) {
fprintf(stderr, "fatal: cannot map extended heap (%s)\n", mx_status_to_string(err));
abort();
}
2020-08-09 15:21:57 +01:00
heap_sz += sz;
2021-01-16 17:26:12 +00:00
void *out = (void *)heap_end;
2021-02-19 13:54:28 +00:00
memset(out, 0x0, sz);
2020-05-15 19:30:22 +01:00
heap_end += sz;
2021-01-16 17:26:12 +00:00
return out;
2020-05-15 19:30:22 +01:00
}