summaryrefslogtreecommitdiff
path: root/include/memory.h
blob: cbed3634cb173fc61e8c69a48a61f3a2c7d14540 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once

#include <stddef.h>
#include <stdint.h>
#include <shim.h>

/**
 * Initalize system memory allocator
 */
extern void memory_init(struct memory_map *map);

/**
 * Disabled cpu interupts to not interfere with
 * current memory allocations.
 */
extern void memory_lock(void);

/**
 * Reenabled cpu interupts
 */
extern void memory_unlock(void);

/**
 * @returns how much memory the system has
 */
extern uint64_t memory_total(void);

/**
 * @returns how much memory is free
 */
extern uint64_t memory_free(void);

/**
 * @returns how much memory is used
 */
extern uint64_t memory_used(void);

/**
 * Allocates a single page in memory.
 * @returns the page if allocated or NULL on failure
 */
extern void *alloc_page(void);

/**
 * Allocats count pages in memory
 * @param count - the number of continious pages to allocate
 * @returns the pages if allocated or NULL on failure
 */
extern void *alloc_pages(int count);

/**
 * Frees a signle page in memory.
 * Must be a page aligned allocated vitural pointer.
 * Freeing in the middle of a block is allowed.
 * @param page - the pointer to the page
 */
extern void free_page(void *page);
// TODO: implement free_page

/**
 * Frees block of pages in memory.
 * Must be a page aligned allocated vitural pointer.
 * Freeing int he middle of a block is allowed,
 * free_pages will from *page to end of block allocated.
 * @param page - the pointer to the page
 */
extern void free_pages(void *page);
// TODO: implement freeing in middle of block

/**
 * Allocates at least len bytes of memory starting at
 * physical address addr. Returned address can be
 * any virtural address.
 * @param addr - the physical address to map
 * @param len - the minimum length to map
 * @param writable - if this memory should be writable
 * @param user - if this memory should be user writable
 */
extern void *mmap(void *addr, size_t len);

/**
 * Unmaps mapped address from the mmap function
 * @param addr - the address returned from mmap
 * @param len - the length allocated
 */
extern void unmap(void *addr);

/**
 * Allocates size_t bytes in memory
 * @param size - the amount of bytes to allocate
 * @retruns the address allocated or NULL on failure
 */
extern void *kalloc(size_t size);

/**
 * Reallocates a given allocated ptr to a new size of bytes in memory.
 * If ptr is NULL it will allocate new memory.
 * @param ptr - the pointer to reallocate
 * @param size - the amount of bytes to set the pointer to
 * @returns the address allocated or NULL on failure
 */
extern void *krealloc(void *ptr, size_t size);

/**
 * Frees a allocated pointer in memory
 * @param ptr - the pointer to free
 */
extern void kfree(void *ptr);