summaryrefslogtreecommitdiff
path: root/kernel/kmalloc.c
blob: 25c2006b9931e707c2b0f6f57a099d2698d3350d (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
46
47
48
49
50
51
52
53
54
55

#include "kmalloc.h"

#include "kernel.h"

static uint8_t    data[KMALLOC_MEM_SIZE]; // bytes

static uint32_t   next;
static uint32_t   first;
static uint8_t    init=0;

// will be initialized on first call to kballoc() //
static void kmallocinit()
{
    next=&(data[0]);
    first=next;

    //TODO: optionally tell gcc to align this itself.
    if(next%4096) 
    {
        next+=4096; 
        next/=4096;
        next*=4096;
    }
    //

    log(FOOLOS_MODULE_NAME,5,"kmalloc_init: 0x%08X",next);
    init=1;
}

// kernel block memory allocation //
uint32_t kballoc(uint32_t size)
{
    size*=4096;

    if(!init)kmallocinit();

    uint32_t old=next;
    next+=size;

    if(next>=first+KMALLOC_MEM_SIZE)
    {
        panic(FOOLOS_MODULE_NAME,"kballoc ran out of memory! maybe increase KMALLOC_MEM_SIZE in kmalloc.c?");
    }

    log(FOOLOS_MODULE_NAME,5,"(%d) : 0x%08X (~%dKB left)",size,old,(KMALLOC_MEM_SIZE-next+first)/1024);
    return old;
}

//TODO: allow freeing memory!!
uint32_t kbfree(uint32_t pos)
{
        panic(FOOLOS_MODULE_NAME,"kbfree NOT IMPLEMENTED YET");
}