aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/memory.c
blob: c1c6ee1bfbc246a1a66ade7c62874e1c06165ffe (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stddef.h>
#include <stdint.h>
#include <kernel/paging/paging.h>

void *memcpy(void *dest, const void *src, size_t count)
{
    const char *sp = (const char *) src;
    char *dp = (char *) dest;
    for (; count != 0; count--) *dp++ = *sp++;
    return dest;
}

void *memset(void *dest, char val, size_t count)
{
    char *temp = (char *) dest;
    for (; count != 0; count--) *temp++ = val;
    return dest;
}

int memcmp(const void *a_ptr, const void *b_ptr, size_t size)
{
    const unsigned char *a = (const unsigned char *) a_ptr;
    const unsigned char *b = (const unsigned char *) b_ptr;
    for (size_t i = 0; i < size; i++) {
        if (a[i] < b[i])
            return -1;
        else if (b[i] < a[i])
            return 1;
    }
    return 0;
}

uint32_t total_memory;

struct memory_entry {
    uint64_t base;
    uint64_t length;
    uint32_t type;
} __attribute__((packed));

struct memory_entry *memory_get_entries()
{
    return (struct memory_entry *) 0xA000;
}

void memory_init()
{
    uint64_t max_base = 0;
    uint64_t max_length = 0;
    for (struct memory_entry *i = memory_get_entries(); i->type; i++) {
        if (i->type == 1 && i->base > max_base) {
            max_base = i->base;
            max_length = i->length;
        }
    }
    total_memory = max_base + max_length;
    total_memory /= 1024;
}

uint32_t memory_get_free()
{
    return total_memory - paging_get_used_pages() * 4;
}

uint32_t memory_get_all()
{
    return total_memory;
}