- if (size == 0) {
- return heap->brk;
- }
-
- void* current_brk = heap->brk;
-
- // The upper bound of our next brk of heap given the size.
- // This will be used to calculate the page we need to allocate.
- void* next = current_brk + ROUNDUP(size, BOUNDARY);
-
- // any invalid situations
- if (next >= heap->max_addr || next < current_brk) {
- __current->k_status = LXINVLDPTR;
- }
-
- uintptr_t diff = PG_ALIGN(next) - PG_ALIGN(current_brk);
- if (diff) {
- // if next do require new pages to be allocated
- if (!vmm_alloc_pages(__current->pid, (void*)(PG_ALIGN(current_brk) + PG_SIZE),
- diff,
- PG_PREM_RW, 0)) {
- __current->k_status = LXHEAPFULL;
- return NULL;
- }
- }