aboutsummaryrefslogtreecommitdiff
path: root/kernel/heap.c
blob: d1f1021ed2b967fc8c2627c5b1028972d034f6d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "paging.h"

/* a simple pointer-arithmetic-based malloc */

/* a symbol put there by the linker */
extern uint32_t link_mem_end;
uint32_t kmalloc_addr = &link_mem_end;

static void *kmalloc_int(size_t sz, int align, void **phys)
{
    if(align)
    {
        kmalloc_addr += (PAGE_SIZE - 1);
        kmalloc_addr &= PAGE_MASK;
    }
    if(phys)
        *phys = (void*)kmalloc_addr;
    void *ret = (void*)kmalloc_addr;
    kmalloc_addr += sz;
    assert(((uint32_t)ret & 0xFFF) == 0);
    return ret;
}

void *kmalloc(size_t sz)
{
    return kmalloc_int(sz, 0, NULL);
}

void *kmalloc_a(size_t sz)
{
    return kmalloc_int(sz, 1, NULL);
}

void *kmalloc_p(size_t sz, void **phys)
{
     return kmalloc_int(sz, 0, phys);
}

void *kmalloc_ap(size_t sz, void **phys)
{
    return kmalloc_int(sz, 1, phys);
}